Introduction to the use of java.lang.Math class

The java.lang.Math class contains methods for performing basic mathematical operations such as elementary exponentials, logarithms, square roots, and trigonometric functions.
Note: All fields and methods in the Math class are static, so you can directly use the class name when calling.

1. Field constants:

1.1 The base of the natural logarithm
public static final double E
is closer to the double value of e (the base of the natural logarithm) than any other value. Value: 2.718281828459045

1.2 PI
public static final double PI
is closer to the double value of pi (the ratio of the circumference of a circle to its diameter) than any other value. Value: 3.141592653589793

2. Method:

2.1 Absolute value
public static int abs(int a)
public static long abs(long a)
public static float abs(float a)
public static double abs(double a)
returns the absolute value of the parameter value. If the argument is non-negative, it is returned. If the argument is negative, returns the inverse of the argument.

Example:
Math.abs(-3); // return 3
Math.abs(0); // return 0
Math.abs(3); // return 3
Math.abs(-0.7F); // return 0.7F

2.2 Return a smaller value
public static int min(int a, int b)
public static long min(long a, long b)
public static float min(float a, float b)
public static double min(double a, double b)
return The smaller of the two parameter values. That is, the result is an argument closer to negative infinity. If the parameter values ​​are the same, the result is also the same value.

Example:
Math.min(0, 3); // returns 0
Math.min(3, 0); // returns 0
Math.min(-1, 3); // returns -1
Math.min(3, 3 ); // return 3

2.3 Return a larger value
public static int max(int ​​a, int b)
public static long max(long a, long b)
public static float max(float a, float b)
public static double max(double a, double b)
return The larger of the two parameter values. That is, the result is an argument closer to positive infinity. If the parameter values ​​are the same, the result is also the same value.

Example:
Math.max(0, 3); // returns 3
Math.max(3, 0); // returns 3
Math.max(-1, 3); // returns 3
Math.max(3, 3) ; // return 3

2.4 Return the adjacent integer in the direction of positive infinity
public static double floor (double d)
returns the largest (closest to positive infinity) floating-point number, which is less than or equal to the parameter and equal to a certain integer.

Example:
Math.floor(0); // returns 0.0
Math.floor(10); // returns 10.0
Math.floor(10.2); // returns 10.0
Math.floor(-10.2); // returns -11.0
Math. floor(10.9); // returns 10.0
Math. floor(-10.9); // returns -11.0

2.5 Return the adjacent integer in the direction of negative infinity
public static double ceil (double d)
returns the smallest (closest to negative infinity) floating-point number, which is greater than or equal to the parameter and equal to a certain integer.

Example:
Math.ceil(0); // returns 0.0
Math.ceil(10); // returns 10.0
Math.ceil(10.2); // returns 11.0
Math.ceil(-10.2); // returns -10.0
Math. ceil(10.9); // returns 11.0
Math. ceil(-10.9); // returns -10.0

2.6 returns the integer closest to the parameter
public static int round(float f)
public static long round(double d)
returns the integer closest to the parameter. The result is rounded to an integer: add 1/2, call floor on the result and cast the resulting result to type int. In other words, the result is equal to the value of the following expression: (int)Math.floor(a + 0.5f).

Example:
Math.round(10); // returns 10
Math.round(10.2); // returns 10
Math.round(-10.2); // returns -10
Math.round(10.9); // returns 11
Math. round(-10.9); // returns -11

2.7 Return the integer closest to the parameter by rounding.
public static double rint(double d)
returns the double value closest to the parameter and equal to a certain integer. If two double values ​​that are both integers are equally close, the result is even.

Example:
Math.rint(1); // returns 1.0
Math.rint(1.2); // returns 1.0
Math.rint(1.9); // returns 2.0
Math.rint(1.5); // returns 2.0 (when the decimal is 5, the integers 1 and 2 are equally close, but 2 is even, so the result is 2)
Math.rint(2.5); // returns 2.0 (when the decimal is 5, the integers 1 and 2 are equally close, but 2 is even, so the result is 2)

2.8 Return e to the power of d
public static double exp(double d)
returns the value of e (that is, the base of natural logarithm) to the power of d.

Example:
Math.exp(0); // returns 1.0
Math.exp(2); // returns 7.38905609893065
Math.exp(3.3); // returns 27.112638920657883
Math.exp(-3.3); // returns 0.036883167401240015

2.9 Return e to the power of d minus 1
public static double expm1(double d)
returns the value of e (that is, the base of natural logarithm) to the power of d minus 1.

Example:
Math.expm1(0); // returns 1.0
Math.expm1(2); // returns 6.38905609893065
Math.expm1(3.3); // returns 26.112638920657883
Math.expm1(-3.3); // returns -0.96311683259876

2.10 Return a to the power of b
public static double pow(double a, double b)
returns the value of the first parameter to the power of the second parameter.

Example:
Math.pow(2, 5); // returns 32.0
Math.pow(2, -5); // returns 0.03125
Math.pow(2.1, 5); // returns 40.84101000000001
Math.pow(-2, 5 ); // returns -32.0
Math.pow(10, 0); // returns 1.0
Math.pow(0, 10); // returns 0.0

2.11 Return a pseudo-random floating-point number
public static double random()
returns a double value with a positive sign, which is greater than or equal to 0.0 and less than 1.0. The return value is a pseudo-randomly chosen number, distributed (approximately) uniformly over the range.

Example:
Math.random(); // return: 0.739333266112564 (each call will get a different return value)

2.12 Returns the positive square root
public static double sqrt(double d)
returns the positive square root of the correctly rounded double value.

Example:
Math.sqrt(0); // returns 0.0
Math.sqrt(4); // returns 2.0
Math.sqrt(-4); // returns NaN
Math.sqrt(10.8); // returns 3.286335345030997

2.13 Return the cube root
public static double cbrt(double d)
returns the cube root of the double value.

Example:
Math.cbrt(0); // returns 0.0
Math.cbrt(8); // returns 2.0
Math.cbrt(-8); // returns -2.0
Math.cbrt(10.8); // returns 2.2104188991842317

2.14 Conversion of angles and radians
public static double toRadians(double angdeg)
converts the angle expressed in angles into an approximately equal angle expressed in radians. This conversion is usually inexact.
public static double toDegrees(double angrad)
Converts an angle expressed in radians to an approximately equivalent angle expressed in degrees. This conversion is usually inexact.

Example:
Math.toRadians(30); // returns 0.5235987755982988
Math.toRadians(90); // returns 1.5707963267948966
Math.toDegrees(0.5235987755982988); // returns 29.999999999999996
Math. toDegrees(1.5707963267948966); // returns 90.0

2.15 The trigonometric function
public static double sin(double radians)
returns the trigonometric sine of an angle. The argument is the angle expressed in radians.
public static double sinh(double radians)
Returns the hyperbolic sine of a double value. The argument is the angle expressed in radians.
public static double cos(double radians)
returns the trigonometric cosine of the angle. The argument is the angle expressed in radians.
public static double cosh(double radians)
returns the hyperbolic cosine of the double value. The argument is the angle expressed in radians.
public static double tan(double radians)
Returns the trigonometric tangent of an angle. The argument is the angle expressed in radians.
public static double tanh(double radians)
Returns the hyperbolic tangent of a double value. The argument is the angle expressed in radians.
public static double asin(double d)
Returns the arcsine of a value; the returned angle ranges from -pi/2 to pi/2. The argument is the angle expressed in radians.
public static double acos(double d)
Returns the arccosine of a value; the returned angle ranges from 0.0 to pi. The argument is the angle expressed in radians.
public static double atan(double d)
Returns the arctangent of a value; the returned angles range from -pi/2 to pi/2. The argument is the angle expressed in radians.
public static double atan2(double y, double x)
converts rectangular coordinates (x, y) to polar coordinates (r, theta), and returns the resulting angle theta. This method calculates the phase angle theta from -pi to pi by computing the arctangent of y/x.

Example:
Math.sin(Math.toRadians(30)); // returns 0.49999999999999994
Math.sinh(Math.toRadians(30)); // returns 0.5478534738880397
Math.cos(Math.toRadians(30)); // returns 0.8660254037844 387
Math.cosh (math.toradians (30)); // Return 1.1402383210764286
math.tan (math.toradians (30)); // Returns 0.5773502691896257
math.tanh (30)) ; // Return 0.480472781564516
Math. asin(Math.toRadians(30)); // returns 0.5510695830994463
Math.acos(Math.toRadians(30)); // returns 1.0197267436954502
Math.atan(Math.toRadians(30)); // returns 0.48234790710102493
Math. atan2( 2, 3); // returns 0.5880026035475675

2.16 Return the sign of the parameter
public static float signum(float f)
public static double signum(double d)
return the sign function of the parameter; if the parameter is 0, return 0; if the parameter is greater than 0, return 1.0; if the parameter is less than 0, returns -1.0.

Example:
Math.signum(0); // returns 0.0
Math.signum(1); // returns 1.0
Math.signum(-1); // returns -1.0
Math.signum(10); // returns 1.0
Math. signum(10.1); // returns 1.0

2.17 Copy Sign
public static float copySign(float magnitude, float sign)
public static double copySign(double magnitude, double sign)
Returns the first floating-point argument with the sign of the second floating-point argument.

Example:
Math.copySign(-3, 0); // returns 3.0
Math.copySign(2, -3); // returns -2.0
Math.copySign(3.3, -3); // returns -3.3
Math.copySign( -3.3, 2.1); // returns 3.3

2.18 Returns the logarithm of the parameter
public static double log(double d)
returns the natural logarithm of the parameter (the base is e).
public static double log10(double d)
returns the base 10 logarithm of the argument.
public static double log1p(double d)
returns the natural logarithm (base e) of the sum of the argument and 1. Note that for small parameters, the result of log1p(d) is closer to the actual result of ln(1 + d) than the floating-point result of log(1.0+d).

Example:
Math.log(10); // returns 2.302585092994046
Math.log10(10); // returns 1.0
Math.log1p(10); // returns 2.3978952727983707
 

Guess you like

Origin blog.csdn.net/chenzhengfeng/article/details/129612074