Introduction to Java (Java Number Processing Class - Mathematical Operations)

1. Mathematical operations

    A Math class is provided in the Java language to perform basic mathematical operations. This class includes commonly used mathematical operation methods, such as trigonometric function method, exponential function method, logarithmic function method, square root function method, etc. Some commonly used mathematical functions, in addition to It also provides some commonly used mathematical constants, such as PI, E and so on.

    1.1 Math class

    The Math class provides many mathematical function methods, mainly including trigonometric function methods, exponential function methods, rounding function methods, maximum value, minimum value and average value function methods. These methods are defined as static forms, so in the program application is relatively simple.

    It can be called using the following form:

Math. Mathematical methods

    In addition to function methods in the Math class, there are some common mathematical constants, such as PI, E and so on. These mathematical constants appear as member variables of the Math class and are very simple to call.

    It can be called using the following form:

Math.PI
Math.E

    1.2 Commonly used mathematical operation methods

    There are many common mathematical operation methods in the Math class, which can be roughly divided into four categories, namely trigonometric function method, exponential function method, rounding function method, and maximum value, minimum value and absolute value function method.

    (1) Trigonometric function method

      The trigonometric methods contained in the Math class are as follows:

public static double sin(double a) : returns the trigonometric sine of the angle
public static double cos(double a) : returns the trigonometric cosine of the angle
public static double tan(double a) : returns the trigonometric tangent of the angle
public static double asin(double a) : returns the arc sine of a value
public static double acos(double a) : returns the arc cosine of a value
public static double atan(double a) : returns the arc tangent of a value
public static double toRadians(double angdeg) : convert angle to radian
public static double toDegrees(double anggrad) : converts radians to degrees

    The parameters and return values ​​of each of the above methods are of type double. Setting the value of the parameters of these methods to double is a certain but far away. The parameters are implemented in radians instead of angles, where 1° is equal to π/180 radians, so 180° can be represented by π radians. In addition to obtaining the sine, cosine, tangent, arcsine, arccosine, and arctangent of an angle, the Math class also provides the methods toRadians() and toDegrees() to convert between angles and radians. Note, however, that the conversion of degrees and radians is usually imprecise.

    eg : Create a class, call various trigonometric function operation methods provided by the Math class in the main method of the class, and output the operation results.

public class TrigonometricFunction {
    public static void main(String[] args) {
        System.out.println("Sine of 90 degrees: " + Math.sin(Math.PI/2));
        System.out.println("Cosine of 0 degrees: " + Math.cos(0));
        System.out.println("The tangent of 60 degrees: " + Math.tan(Math.PI/3));
        System.out.println("The square root of 2 and the arcsine of the quotient of 2: " + Math.asin(Math.sqrt(2)/2));
        System.out.println("The square root of 2 and the arc cosine of the quotient of 2: " + Math.acos(Math.sqrt(2)/2));
        System.out.println("Arctangent of 1: " + Math.atan(1));
        System.out.println("120 degrees in radians: " + Math.toRadians(120.0));
        System.out.println("The angle value of π/2: " + Math.toDegrees(Math.PI/2));
    }
}

    The running result is:

The square root of 2 and the arcsine of the quotient of 2: 0.7853981633974484
The arccosine of the square root of 2 and the quotient of 2: 0.7853981633974483
Arctangent of 1: 0.7853981633974483
Radian value for 120 degrees: 2.0943951023931953
Angle value for π/2: 90.0

    It can be seen from the running results that the sine value of 90° is 1, the cosine value of 0° is 1, and the tangent of 60° should be the same as the value of Math.sqrt(3), that is, take the square root of 3.

    (2) Exponential function method

    The function methods related to exponents in the Math class are as follows:

public static double exp(double a) : used to get e to the power of a
public static double log(double a) : used to take the natural logarithm, that is, take the value of lna
public static double log10(double a) : used to take the logarithm of base 10
public static double sqrt(double a) : used to take the square root of a, where the value of a cannot be negative
public static double cbrt(double a) : used to take the cube root of a
public static double pow(double a , double b) : used to take a to the power of b

    Exponential operations include square root, logarithm, and n-th power operations.

    eg : Create a class, call the method in the Math class in the main method of the class to implement the operation of the exponential function, and output the operation result.

public class ExponentFunction {
    public static void main(String[] args) {
        System.out.println("Square value of e: " + Math.exp(2));	
        System.out.println("Logarithmic value of base 2: " + Math.log(2));	
        System.out.println("Base 10 logarithm of 2: " + Math.log10(2));	
        System.out.println("Square root of 4: " + Math.sqrt(2));	
        System.out.println("Cube root value of 8: " + Math.cbrt(2));	
        System.out.println("2 to the power of 2: " + Math.pow(2, 2));	
    }
}

    The running result is:

Base 2 logarithm value: 0.6931471805599453
Base 10 logarithm of 2: 0.3010299956639812
Square root value of 4: 1.4142135623730951
Cubic root value of 8: 1.2599210498948732
2 to the power of 2: 4.0

    1.3 Rounding function method

    The Math class mainly includes the following rounding methods:

public static double ceil(double a) : returns the smallest integer greater than or equal to the parameter
public static double floor(double a) : returns the largest integer less than or equal to the argument
public static double rint(double a) : Returns the closest integer to the parameter. If the two are both integers and are also close, the result is an even number
public static int round(float a) : Returns the integer closest to the parameter after adding 0.5 to the parameter
public static long round(double a) : add 0.5 to the parameter and return the nearest integer to the parameter, then cast it to a long integer

    eg : Create a class, call the method in the Math class in the main method of the class to implement the operation of the rounding function, and output the operation result.

public class IntFunction {
public static void main(String[] args) {
System.out.println("Use the ceil() method to round up: " + Math.ceil(5.2));
System.out.println("Use the floor() method to round: " + Math.floor(2.5));
System.out.println("Use rint() method to round: " + Math.rint(2.7));
System.out.println("Use int round() method to round: " + Math.round(3.4f));
System.out.println("Use the long round() method to round: " + Math.round(2.5));
}
}

    The running result is:

Rounding using the ceil() method: 6.0
Rounding using the floor() method: 2.0
Rounding using the rint() method: 3.0
Rounding using the int round() method: 3
Rounding using the long round() method: 3

    1.4 The method of taking the maximum value, minimum value and absolute value of rows

    The most commonly used method in the program is to take the maximum value, minimum value, absolute value, etc. The operation method is as follows:

public static double max(double a ,double b) : take the maximum value between a and b
public static int min(int a ,int b) : take the minimum value between a and b, the parameter is an integer
public static long min(long a ,long b) : take the minimum value between a and b, the parameter is a long integer
public static float min(float a ,float b) : take the minimum value between a and b, the parameter is a floating point type
public static double min(double a ,double b) : take the minimum value between a and b, the parameter is double precision
public static int abs(int a) : returns the absolute value of the integer parameter
public static long abs(long a) : Returns the absolute value of the long parameter
public static float abs(float a) : returns the absolute value of the float parameter
public static double abs(double a) : returns the absolute value of the double parameter

    eg : Create a class, call the method in the Math class in the main method of the class to realize the operation of finding the maximum value, the minimum value and the absolute value of the two bundles, and output the operation result.

public class AnyFunction {
    public static void main(String[] args) {
        System.out.println("The greater of 4 and 8: " + Math.max(4, 8));
        System.out.println("The lesser of 4.4 and 4: " + Math.min(4.4, 4));
        System.out.println("Absolute value of -7: " + Math.abs(-7));
    }
}

    The running result is:

Greater of 4 and 8: 8
The lesser of 4.4 and 4: 4.0
Absolute value of -7: 7


    



Guess you like

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