The java.lang.Math class of common Java APIs (with analysis and examples) _01

The Math class directly inherits the Object class:

public final class Math extends Object
 
 

    Math 类包含用于执行基本数学运算的方法,如初等指数、对数、平方根和三角函数。java.lang包内有一个名为StrictMath类,StrictMath类和Math类有很多同名方法。在默认情况下,很多 Math 类方法直接调用 StrictMath中的等价方法来完成。

    为了确保 Java 程序的可移植性,StrictMath类中的函数根据fdlibm(免费发布的数学库)定义,在不同平台执行都能获得完全一样的结果,多次运行也能产生一样的结果。Math类不像StrictMath类那样严格,允许浮点数最后一两个位数有误差,以此换来性能的提升。

Compare the source code of the Math class with the source code of the StrictMath class

StrictMath class:

public final class StrictMath
{
    public static native double sin(double a);//The method modified by native is called a native method, which is implemented by non-Java code (such as C language code).
}

Math class:

 
 
public final class Math
{
    public static  double sin(double a)
    {
        return StrictMath.sin(a);
    }
}

So if you want better performance, use the Math class (the Math class is more common); but in some cases, it is more important to get a completely reliable and predictable result, then you have to use the StrictMath class.


-------------------------------------Dividing line----------- ---------------------------------------------

Let's enter the topic below and list the more commonly used Math class APIs.

Note: The fields and methods in the Math class are static, that is, there is no need to create an object when calling, just call Math.xxx directly.

--------------------------------------Dividing line---------- --------------------------------------------


static domain
Math.E

e: base of natural logarithm, double type.

2.718281828459045

Math.PI 

π: The ratio of the circumference of a circle to its diameter, double type.

3.141592653589793


 

static method
static double abs(double a)

Returns the absolute value of a.

Example: abs(-4.3) = 4.3

static double cos(double a)

The argument is an angle in radians, and the cosine of that angle is returned.

例 : cos (Math.PI) = -1

static double sin(double a) 

The argument is an angle in radians, and the sine of that angle is returned .

例 : sin (Math.PI / 2) = 1

static double tan(double a) 

The argument is an angle in radians, and the tangent of that angle is returned .

Example: tan (Math.PI / 4) = 1

static double atan2(double y, double x) 

Convert rectangular coordinates (x, y) to polar coordinates (r, theta), returning the resulting angle theta. Game design uses a lot.

Example: atan2 (1,1) = 45

static double ceil(double a) 

Returns the smallest integer greater than or equal to a, ie rounded up.

Example: ceil(11.8) = 12, which can be understood as the ceiling.

static double floor(double a)  Returns the smallest integer less than or equal to a, ie rounded up.
Example: ceil(11.8) = 11, which can be understood as the floor.
static double exp(double a) 

返回自然数 e 的 a 次幂的值。

例:exp(2) = Math.E * Math.E

static double log(double a) 

返回 a 的自然对数(即底数是 e)。

例:log(Math.E * Math.E) = 2

static double log10(double a) 

返回 a 的底数为 10 的对数。

例:log10(100) = 2

static double max(double a, double b)

返回两个 a、b 中较大的一个。

例:max(5.7, 6,3) = 6.3

static double min(double a, double b) 返回两个 a、b 中较大的一个。
例:min(5.7, 6,3) = 5.7
static double pow(double a, double b) 

返回 a 的 b 次方。

例:pow(2,3) = 8

static long round(double a) 

返回最接近 a 的 整数。可用于四舍五入。

例:round(3.3) = 3

static double signum(double d) 

返回参数的符号函数;

例:signum(0) = 0;   signum(0.01) = 1;    signum(-0.01) = -1;

static double sqrt(double a) 

返回的 a 的正平方根。

例:sqrt(4) = 2

static double toDegrees(double radians) 

将用弧度表示的角转换为近似相等的用角度表示的角。

例:toDegrees(Math.Pi) = 180

static double toRadians(double degrees) 

将用角度表示的角转换为近似相等的用弧度表示的角。

例:toRadians(180) = Math.PI

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325732969&siteId=291194637