Some methods commonly used in Math functions
- Class
Math
contains methods for performing basic number operations
Arithmetic operations
Math.abs(a)
: Take the absolute value of aMath.sqrt(a)
: Take the square root of aMath.cbrt(a)
: Take the cube root of aMath.max(a,b)
: Take the maximum value between a and bMath.min(a,b)
: Take the minimum value between a and bMath.pow(a,b)
: Take b square of a
Math.abs(a): Take the absolute value
-
method describe abs(double a)
Returns double
the absolute value of the value.
/*
Math.abs() 取绝对值
*/
System.out.println(7);//7
System.out.println(-7);//-7
System.out.println(Math.abs(10.3));//10.3
System.out.println(Math.abs(-10.3));//10.3
Math.sqrt(a): Take the square root
-
method describe sqrt(double a)
Returns double
the correctly rounded positive square root of the value.
/*
Math.sqrt();开平方根
*/
System.out.println(Math.sqrt(9));//3.0
System.out.println(Math.sqrt(16));//4.0
System.out.println(Math.sqrt(-16));//NaN
注:
The argument for the square root cannot be negative.
Math.cbrt(a): Take the cube root
-
method describe cbrt(double a)
Returns double
the cube root of the value.
/*
Math.cbrt();开立方根
*/
System.out.println(Math.cbrt(8));//2.0
System.out.println(Math.cbrt(27));//3.0
System.out.println(Math.cbrt(-27));//-3.0
Math.max(a,b): take the maximum value
-
method describe max(double a, double b)
Returns double
the larger of the two valuesdouble
.
System.out.println(Math.max(2,3));//3
System.out.println(Math.max(5.3,4.6));//5.3
System.out.println(Math.max(-2.7,4));//4.0
Math.min(a,b): Take the minimum value
-
method describe min(double a, double b)
Returns double
the smaller of the two valuesdouble
.
/*
Math.min();取最小值
*/
System.out.println(Math.min(-1.8,6));//-1.8
System.out.println(Math.min(0.7,10));//0.7
System.out.println(Math.min(19,6));//6
Math.pow(a,b): Calculate b square of a
-
method describe pow(double a, double b)
Returns the value of the first argument raised to the power of the second argument.
/*
Math.pow()
*/
System.out.println(Math.pow(2,0));//1.0
System.out.println(Math.pow(3,1));//3.0
System.out.println(Math.pow(2,2));//4.0
arithmetic carry
Math.ceil()
:Fengyu JinyiMath.floor()
:Every time there is room for oneMath.rint()
:roundingMath.round()
:rounding
Math.ceil(a): Take the smallest integer greater than or equal to a
-
method describe ceil(double a)
Returns the smallest value that is greater than or equal to the argument and equal to an integer double
.
/*
Math.ceil()
*/
System.out.println(Math.ceil(10.2));//11.0
System.out.println(Math.ceil(9.8));//10.0
System.out.println(Math.ceil(-10.2));//-10.0
Math.floor(a): Take the largest integer less than or equal to a
-
method describe floor(double a)
Returns the largest value less than or equal to the argument and equal to an integer double
.
/*
Math.floor()
*/
System.out.println(Math.floor(1.3));//1.0
System.out.println(Math.floor(0.8));//0.0
System.out.println(Math.floor(10.5));//10.0
System.out.println(Math.floor(-100.9));//-101.0
Math.rint(): Round, return double value
-
method describe rint(double a)
Returns double
the closest value todouble
a value that is equal to a mathematical integer.
System.out.println(Math.rint(10.1));//10.0
System.out.println(Math.rint(10.5));//10.0
System.out.println(Math.rint(10.8));//11.0
System.out.println(Math.rint(0.2));//0.0
System.out.println(Math.rint(0.5));//0.0
System.out.println(Math.rint(0.8));//1.0
System.out.println(Math.rint(-0.2));//-0.0
System.out.println(Math.rint(-0.5));//-0.0
System.out.println(Math.rint(-0.8));//-1.0
System.out.println(Math.rint(-10.2));//-10.0
System.out.println(Math.rint(-10.8));//-11.0
System.out.println(Math.rint(-10.5));//-10.0
注
: Take an even number when 0.5
Math.round(): Rounding, long value is returned when double, int value is returned when float
-
method describe round(double a)
Returns the closest argument to the argument long
, rounding the relationship to positive infinity.round(float a)
Returns the closest argument to the argument int
, rounding the relationship to positive infinity.
System.out.println(Math.round(10.1));//10
System.out.println(Math.round(10.5));//11
System.out.println(Math.round(10.53));//11
System.out.println(Math.round(10.8));//11
System.out.println(Math.round(-10.1));//-10
System.out.println(Math.round(-10.5));//-10
System.out.println(Math.round(-10.53));//-11
System.out.println(Math.round(-10.9));//-11
注
: Rounding, int value is returned when float is used, long value is returned when double is used.
random number
Math.random() random number, randomly taking a value within the range [0.0,1.0)
-
method describe random()
Returns a value with a positive sign double
, greater than or equal to and0.0
less than1.0
.
System.out.println(Math.random());//[0.0,1.0)
System.out.println(Math.random()+1);//[1.0,2.0)
System.out.println(Math.random()*10);//[0.0,10.0)
System.out.println(Math.random()*10+1);//[1.0,11.0)
System.out.println(Math.random()*100+0.5);//[0.5,100.5)
注
: The return type is double type.
Trigonometric functions
Math.sin()
: sineMath.cos()
: cosineMath.ten()
:tangent
sin(): sine
public static double sin(double a)
Returns the trigonometric sine of the angle. Special case:
- If the argument is NaN or infinity, the result is NaN.
- If the argument is zero, the result is zero with the same sign as the argument.
parameter:
- a - Angle, expressed in radians.
(The number of radians in one circle is 2πr/r=2π, and 360° angle = 2π radians. Therefore, 1 radian is approximately 57.3°, which is 57°17'44.806'', and 1° is π/180 radians)
double PI = Math.PI;//double值比任何其他 pi更接近,圆的圆周与其直径的比率。
System.out.println(Math.sin(0));//0.0
System.out.println(Math.sin(6.28));//-0.0031853017931379904
System.out.println(Math.sin(9.42));//0.0047779425901285115
System.out.println(Math.sin(1.57));//0.9999996829318346
System.out.println(Math.sin(3.14));//0.0015926529164868282
注
: The input is expressed in radians, and the returned value is within the range [-1,1]
.
cos(): cosine
public static double cos(double a)
Returns the trigonometric cosine of the angle. Special case:
- If the argument is NaN or infinity, the result is NaN.
- The result must be semi-monotone
。
parameter
- a - Angle, expressed in radians.
System.out.println(Math.cos(0));//1.0
System.out.println(Math.cos(1.57));//7.963267107332633E-4
System.out.println(Math.cos(3.14));//-0.9999987317275395
System.out.println(Math.cos(4.71));//-0.0023889781122815386
System.out.println(Math.cos(6.28));//-0.9999987317275395
注
: The input is expressed in radians, and the returned value is within the range [-1,1]
.
ten(): tangent
public static double tan(double a)
Returns the trigonometric tangent of the angle. Special case:
- If the argument is NaN or infinity, the result is NaN.
- If the argument is zero, the result is zero with the same sign as the argument.
The calculated result must be within 1 ulp of the exact result. The result must be semi-monotonic.
parameter
- a - Angle, expressed in radians.
System.out.println(Math.tan(0));//0.0
System.out.println(Math.tan(1.57));//1255.7655915007897
System.out.println(Math.tan(3.14));//-0.001592654936407223
System.out.println(Math.tan(4.71));//418.58782265388515
System.out.println(Math.tan(6.28));//-0.003185317952531891
注
: ten90°
Does not exist
, that is, the input radian cannot be (π/2±kπ) and the returned value is within the range [-∞,+∞]
.
Summarize
In the Math function, there are many basic operations on numbers, but basically some of the commonly used methods are listed in detail above. These commonly used methods still need to be used skillfully to avoid encountering problems during the development process or daily tasks. By then, I was at a loss.