In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use the java.lang.Math library in java. It is very detailed and has a certain reference value. Friends who are interested must finish it!
The java.lang.Math library provides common mathematical calculation tools.
Constant
Final double E = 2.71828284590452354; / Natural logarithmic base final double PI = 3.14159265358979323846; / / pi final double DEGREES_TO_RADIANS = 0.017453292519943295; / / Radian final double RADIANS_TO_DEGREES = 57.29577951308232; / / Radian rotation angle
Rounding
Abs (x): absolute value floor (x): rounding down ceil (x): rounding up round (x): rounding up, if there are two (x.5), return the larger number rint (x): the nearest integer, if there are two (x.5), return the even number floorDiv (x, y): rounding down floorMod (x, y): java default fetch% gets the same result as the symbol of x The symbols of floorMod and y are the same
Double delta = 0.0000001assertEquals (Math.abs (- 6), 6); assertEquals (Math.floor (- 6.2),-7, delta); / / rounding down assertEquals (Math.floor (6.2), 6, delta); assertEquals (Math.floor (6.8,6, delta); assertEquals (Math.ceil (- 6.2),-6, delta); / / rounding assertEquals up (Math.ceil (6.2), 7, delta) AssertEquals (Math.ceil, 7, delta); assertEquals (Math.round,-6, delta); / / rounded assertEquals (Math.round, 6, delta); assertEquals (Math.round, 7, delta); assertEquals (Math.round,-6, delta); assertEquals (Math.round, 7, delta); assertEquals (Math.rint (- 6.2),-6, delta) / / nearest integer, if there are two, return even numbers assertEquals (Math.rint 6.2,6, delta); assertEquals (Math.rint 6.8,7, delta); assertEquals (Math.rint (- 6.5,6, delta); assertEquals (Math.rint (6.5,6, delta); assertEquals (Math.floorDiv (7,3), 2); assertEquals (Math.floorDiv (- 7,3),-3) AssertEquals (Math.floorMod (7, 3), 1); assertEquals (Math.floorMod (- 7,-3),-1); assertEquals (Math.floorMod (- 7, 3), 2); assertEquals (- 7%-3,-1); assertEquals (- 7% 3,-1)
Triangulation function
AssertEquals (Math.sin (Math.PI / 2), 1.0, delta); assertEquals (Math.cos (Math.PI),-1, delta); assertEquals (Math.tan (Math.PI / 4), 1.0, delta); assertEquals (Math.asin (1), Math.PI / 2, delta); assertEquals (Math.acos (- 1), Math.PI, delta); assertEquals (Math.atan (1), Math.PI / 4, delta)
Exponential logarithm
Pow (x, y): X ^ y, x to the y power sqrt (x): √ x Magi x square root cbrt (x): cubic root hypot (x, y): √ (x ²+ y ²) exp (x): e ^ x expm1 (x): e ^ x-1 log (x): ln (x) log10: lg (x) log1p (x): ln (1x)
AssertEquals (Math.pow (3,2), 9, delta); assertEquals (Math.pow (2,3), 8, delta); assertEquals (Math.sqrt (4), 2, delta); assertEquals (Math.cbrt (27), 3, delta); assertEquals (Math.hypot (3,4), 5, delta); / / √ (x ²+ y ²) assertEquals (Math.exp (2.5), Math.pow (Math.E, 2.5), delta) / / e ^ xassertEquals (Math.expm1 (2), Math.exp (2)-1, delta); / / e ^ x-1assertEquals (Math.log (Math.exp (1.5)), 1.5, delta); / / ln (x) assertEquals (Math.log10 (1000), 3, delta); / / lg (x) assertEquals (Math.log1p (Math.E-1), 1, delta); / ln (1 + x)
Hyperbolic function
Sinh (x): (e ^ x-e ^-x) / 2 cosh (x): (e ^ x + e ^-x) / 2 tanh (x): sinh (x) / cosh (x)
AssertEquals (Math.sinh (2), (Math.exp (2)-Math.exp (- 2)) / 2, delta); / / sinh (x) = (e ^ x-e ^-x) / 2assertEquals (Math.cosh (2), (Math.exp (2) + Math.exp (- 2)) / 2, delta) / / cosh (x) = (e ^ x + e ^-x) / 2assertEquals (Math.tanh (2), Math.sinh (2) / Math.cosh (2), delta); / / tanh (x) = sinh (x) / cosh (x)
Accurate calculation
Ordinary numerical calculations are not aware of overflow. For example, Long.MAX_VALUE + 1 will get the result Long.MIN_VALUE. In order to solve this irrationality, Math provides some auxiliary functions that will throw an exception when the result overflows.
AddExact (x, y): addition multiplyExact (x, y): multiplication decrementExact (x, y): decreasing incrementExact (x, y): increasing negateExact (x, y): inverse number multiplyFull (x, y): accepting two int returns one long, preventing overflow multiplyHigh (x, y): returns the high 64 bits of the product of two long
AssertEquals (Long.MAX_VALUE + 1, Long.MIN_VALUE); / / overflow assertThrows (ArithmeticException.class, ()-> Math.addExact (Long.MAX_VALUE, 1)); / / addition overflow throw exception assertThrows (ArithmeticException.class, ()-> Math.multiplyExact (Long.MAX_VALUE, 2)); / / multiplication assertThrows (ArithmeticException.class, ()-> Math.decrementExact (Long.MIN_VALUE)) / / decreasing assertThrows (ArithmeticException.class, ()-> Math.incrementExact (Long.MAX_VALUE)); / / increasing assertThrows (ArithmeticException.class, ()-> Math.negateExact (Long.MIN_VALUE)); / / inverse assertEquals (Math.addExact (1,2), 3); assertEquals (Math.multiplyExact (2,3), 6); assertEquals (Math.incrementExact (6), 7); assertEquals (Math.decrementExact (6), 5); assertEquals (Math.negateExact (- 6), 6) AssertEquals (Math.multiplyFull (1,2), 2); / / accepts two int to return a long to prevent overflow of assertEquals (Math.multiplyHigh (1,2), 0); / / returns the high 64 bits of the product of two long
Floating point number
There are countless floating-point numbers between any two floating-point numbers, so most floating-point numbers cannot be represented, only the closest one can be selected. Java provides some interfaces to get the floating-point numbers that can be represented.
System.out.println (Math.nextUp (1.1)); / / next floating point number System.out.println (Math.nextDown (1.1)); / / previous floating point number System.out.println (Math.nextAfter (1.1, Double.POSITIVE_INFINITY)); / / next floating point number System.out.println (Math.nextAfter (1.1, Double.NEGATIVE_INFINITY)); / / last floating point number
Random number
Math library random number
Random numbers between System.out.println (Math.random ()) / / 0 ~ 1
Java.lang.Random
The Random class provides a richer range of random methods that can return different types of random numbers
Random r = new Random (); System.out.println (r.nextInt ()); System.out.println (r.nextLong ()); System.out.println (r.nextFloat ()); System.out.println (r.nextDouble ())
Random also provides streaming api
Random r = new Random (); List li = r.ints (). Limit (10). Boxed (). Map ((x)-> Math.abs (x% 100)). Collectors.toList (); System.out.println (li)
Java.util.Random is thread safe, but using java.util.Random instances at the same time across threads may run into contention, resulting in performance degradation. Consider using java.util.concurrent.ThreadLocalRandom instead of java.util.Random,ThreadLocalRandom and Random to have a consistent interface in multithreaded design
The above is all the contents of the article "how to use the java.lang.Math Library in java". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.