Time：2021-11-25

## Java common class library math

The math class contains methods for performing basic numeric operations, such as basic exponents, logarithms, square roots, and trigonometric functions

### 1、 Field summary

Modifier and Type Field Description
static double E Cardinality of natural logarithm
static double PI π

### 2、 Method summary

#### 2.1 common methods

Modifier and Type Field Description
static double ceil​(double a) Returns the smallest integer ≥ a
static double floor​(double a) Returns the maximum integer ≤ a
static int round​(float a) Returns the rounded value
static T max​(T a, T b) Returns the larger of two data types t
static T min​(T a, T b) Returns two data types with the smaller X in t
static double random() Returns [0.0,1.0].
static double sqrt​(double a) Returns the positive square root.
static T abs(T a) Returns an absolute value with data type T
static double log​(double a) Returns the logarithm of base E
static double log10​(double a) Returns the logarithm of base 10

#### 2.1.1 source code of some methods

``````
public static long round(double a) {
long longBits = Double.doubleToRawLongBits(a);
long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
>> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
+ DoubleConsts.EXP_BIAS) - biasedExp;
if ((shift & -64) == 0) { // shift >= 0 && shift < 64
// a is a finite number such that pow(2,-64) <= ulp(a) < 1
long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
if (longBits < 0) {
r = -r;
}
// In the comments below each Java expression evaluates to the value
// the corresponding mathematical expression:
// (r) evaluates to a / ulp(a)
// (r >> shift) evaluates to floor(a * 2)
// ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
// (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
return ((r >> shift) + 1) >> 1;
} else {
// a is either
// - a finite number with abs(a) < exp(2,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2
// - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
// - an infinity or NaN
return (long) a;
}
}
public static int max(int a, int b) {
return (a >= b) ? a : b;
}
public static int min(int a, int b) {
return (a <= b) ? a : b;
}
public static int abs(int a) {
return (a < 0) ? -a : a;
}
``````

2.1.2 specific implementation

``````public class Test {
public static void main(String[] args) {
System. Out. Println ("≥ the minimum integer of 3.2 is:" + math.ceil (3.2))// output:4
System. Out. Println ("≤ the maximum integer of 3.2 is:" + Math. Floor (3.2))// output:3
System. Out. Println ("3.2 rounded to:" + Math. Round (3.2))// output:3
System.out.println ("+ math.max (- 1,5))// output:5
System. Out. Println ("- 1,5, the smaller number is:" + Math. Min (- 1,5))// output:-1
Any random number in system. Out. Println ("number of randomly generated [0,5) range" + Math. Random() * 5); // output: [0,5)
System. Out. Println ("the square root of 25 is:" + Math. Sqrt (25))// output:5
System. Out. Println ("- 9's absolute value is:" + Math. ABS (- 9))// output:9
System.out.println ("the value of 2 ^ 3 is:" + math.pow (2,3))// output:8
System. Out. Println ("logarithm based on e is:" + Math. Log (10));
System.out.println ("logarithm based on 10 is:" + math.log10 (100))// output:2
}
}``````

#### 2.2 arithmetic operation

Modifier and Type Field Description
static T addExact​(T x, T y) If x + y is returned, an exception t (int, long) will be thrown if overflow occurs
static T multiplyExact​(A x, B y) Returns x * y, and throws exceptions int (int, int), long (long, int / long) if the result overflows
static long multiplyFull​(int x, int y) Return (long) x * (long) y
static T floorDiv​(A x, B y) Return the maximum value of ≤ X / y, y = 0, then an arithmeticexception is thrown, int (int, int), long (long, int / long
static T floorMod​(A x, B y) Return floor (x% y), i.e. X – (x / y) * y, int (int / long, int), long (long, long)

#### 2.3 trigonometric function

Modifier and Type Field Description
static double sin​(double a) Returns the triangular sine of the angle
static double cos​(double a) Returns the triangular cosine of the angle
static double tan​(double a) Returns the triangular tangent of the angle
static double asin​(double a) Returns the arcsine value of a, and the returned angle is – pi / 2 ~ pi / 2
static double acos​(double a) Returns the inverse cosine of a, and the returned angle is 0.0 ~ PI
static double atan​(double a) Returns the arctangent of a, and the returned angle is – pi / 2 ~ pi / 2

#### 2.4 other unusual methods

Modifier and Type Field Description
static double cosh​(double x) Returns the hyperbolic cosine of a double value
static double cbrt​(double a) Returns the cube root of the double value
static double copySign​(double magnitude, double sign) Returns the first floating-point parameter with the second floating-point parameter symbol
static float copySign​(float magnitude, float sign) Returns the first floating-point parameter with the second floating-point parameter symbol
static int decrementExact​(int a) Returns A-1, and throws an exception if the result overflows int
static long decrementExact​(long a) Returns A-1. If the result overflows long, an exception is thrown
static double expm1​(double x) Return e ^ X – 1
static double fma​(double a, double b, double c) Return a * B + C
static float fma​(float a, float b, float c) Return a * B + C
static int getExponent​(double d) Returns the unbiased exponent used in the double representation
static int getExponent​(float f) Returns the unbiased exponent used in the float representation
static double hypot​(double x, double y) Returns sqrt (x 2 + y 2) without intermediate overflow or underflow
static double IEEEremainder​(double f1, double f2) Calculate the remainder of two parameters according to IEEE 754 standard
static int incrementExact​(int a) Returns a parameter incremented by 1. If the result overflows int, an exception will be thrown
static long incrementExact​(long a) Returns a parameter incremented by 1. If the result overflows long, an exception will be thrown
static double log1p​(double x) Returns the natural logarithm of the sum of arguments and
static long multiplyHigh​(long x, long y) Returns long as the highest 64 bits of the 128 bit product of two 64 bit factors
static int negateExact​(int a) Returns the negative of the parameter. If the result overflows int, an exception will be thrown
static long negateExact​(long a) Returns the negative of the parameter. If the result overflows long, an exception will be thrown
static double nextAfter​(double start, double direction) Returns the floating-point number next to the first parameter in the direction of the second parameter
static float nextAfter​(float start, double direction) Returns the floating-point number next to the first parameter in the direction of the second parameter
static double nextDown​(double d) Returns a floating-point value adjacent to D in the negative infinity direction
static float nextDown​(float f) Returns a floating-point value adjacent to f in the negative infinity direction
static double nextUp​(double d) Returns a floating-point value adjacent to D in the positive infinity direction
static float nextUp​(float f) Returns a floating-point value adjacent to f in the positive infinity direction
static double rint​(double a) Returns the double value closest to the double value, which is equal to a mathematical integer
static double scalb​(double d, int scaleFactor) Return D × 2 scalefactor rounding, just like multiplying a single correctly rounded floating point by a member of a double scalefactor value set
static float scalb​(float f, int scaleFactor) Return f × 2 scalefactor rounding, just like multiplying a single correctly rounded floating-point by a member of a floating-point value set
static double signum​(double d) Return the Signum function of the parameter; Zero if the parameter is zero; 1.0 if the parameter is greater than zero; – 1.0 if the parameter is less than zero
static float signum​(float f) Return the Signum function of the parameter; Zero if the parameter is zero, 1.0F if the parameter is greater than zero, and – 1.0F if the parameter is less than zero
static double sinh​(double x) Returns the hyperbolic sine of a double value
static int subtractExact​(int x, int y) Returns the difference of parameters. If the result overflows int, an exception will be thrown
static long subtractExact​(long x, long y) Returns the difference of parameters. If the result overflows long, an exception will be thrown
static double tanh​(double x) Returns the hyperbolic tangent of the double value
static double toDegrees​(double angrad) Converts an angle measured in radians to an approximate equivalent angle measured in degrees
static int toIntExact​(long value) Returns the value of the long parameter; Throw an exception if the value overflows int
static double toRadians​(double angdeg) Converts an angle measured in degrees to an approximate equivalent angle measured in radians
static double ulp​(double d) Returns the ULP size of the parameter
static float ulp​(float f) Returns the ULP size of the parameter