Original address: http://www.24xuexi.com/w/2011-11-08/98206.html
First of all, let's look at the main methods provided by the math class, the following list gives the main methods of the math class, and if you want to understand all of the methods of Math, refer to Javaapi, which is listed below:
Function (method)
Describe
IEEERemainder (double, double)
The remainder operation is performed on two parameters according to the requirements of the IEEE 754 standard.
ABS (int a)
Returns the absolute value of an int
ABS (Long a)
Returns the absolute value of long
ABS (float a)
Returns the absolute value of float values
ABS (Double A)
Returns the absolute value of double values
ACOs (Double A)
Returns the inverse cosine of the angle, ranging from 0.0 to pi
ASIN (Double A)
Returns the inverse of the angle, ranging from-PI/2 to PI/2
Atan (Double A)
Returns the tangent of the angle, ranging from-PI/2 to PI/2
ATAN2 (double A, double b)
Converts the rectangle coordinates (x, y) to polar coordinates (R, Theta)
Ceil (Double A)
Returns the smallest (nearest negative infinity) Double value that is greater than or equal to the parameter and equals an integer
COS (double)
Returns the trigonometric cosine of a corner
EXP (Double A)
Returns the value of the double power of Euler's number E
Floor (Double A)
Returns the largest (closest to positive infinity) Double value, which is less than or equal to the parameter and equals an integer
Log (double A)
Returns the natural logarithm of the double value (base is e)
Max (int A, int b)
Returns the larger of two int values
Max (Long A, long B)
Returns the larger of a two long value
Max (float A, float b)
Returns the larger of two float values
Max (double A, double b)
Returns the larger of two double values
MIN (int a, int b)
Returns the smaller of a two int value
Min (Long A, long B)
Returns the smaller of a two long value
Min (float A, float b)
Returns the smaller of a two float value
Min (double A, double b)
Returns the smaller of a two double value
Pow (double A, double b)
Returns the value of the second parameter of the first argument to the power of the two
Random ()
Returns a double value with a positive sign, greater than or equal to 0.0, less than 1.0
Rint (Double)
Returns a double value whose value is closest to the parameter and is an integer
Round (float)
Returns an int with the closest parameter
Round (double)
Returns the nearest parameter of a long
Sin (double)
Returns the triangular sine of a corner
sqrt (double)
Returns the positive square root of a double value that is rounded correctly
Tan (Double)
Returns the trigonometric tangent of a corner
Todegrees (Double)
Converts the angle measured in radians to an approximate equal angle measured in degrees
Toradians (Double)
Converts the angle measured in degrees to an approximate equal angle measured in radians
As you can see, the math class provides the methods that are commonly used in our mathematical calculations and use them to do most of the math calculations. Let's try a few examples:
There is a lot of mainclass that we've seen before, and here's a general demonstration of the use of the math class as follows: Java code
/**
* Class Function Description * Mainclass.java
* * @author Zhangtao
* @version 0.1.0 * *
public class MainClass {
/** * @param args
*/public static void main (string[] args) {
Double loanamount = 0; Double top = 2 * 5/1200;
Double bot = 1-math.exp (5 * ( -12) * Math.log (1 + 7/1200));
System.out.println (Loanamount); System.out.println (top);
System.out.println (BOT); }
}
The code above shows the use of the math class, which I will not dwell on. One thing to note here is that the math class method is statically provided, that is, we can use it directly without using the New keyword to instantiate the math class.
Find the absolute value of float, int, double number in the list of methods we gave earlier, we know that the math class can calculate the absolute value of a number, and here's an example to demonstrate the use of the ABS method. ABS is the abbreviation of absolute, Absolute is the absolute meaning. Here is an example:
Java code
/**
* Class Function Description * Mainclass.java
* * @author Zhangtao
* @version 0.1.0 * *
public class MainClass {
/** * @param args
*/public static void main (string[] args) {
int i = 8; Int j =-5;
System.out.println ("Absolute value of" + i + "is:" + math.abs (i)); System.out.println ("Absolute Value of" + J + "is:" + Math.Abs (j));
float f1 = 1.40f;
float F2 = -5.28f; System.out.println ("Absolute value of" + F1 + "is:" + math.abs (F1));
System.out.println ("Absolute value of" + F2 + "is:" + math.abs (F2));
Double D1 = 3.324; Double D2 =-9.324;
System.out.println ("Absolute value of" + D1 + "is:" + math.abs (D1)); System.out.println ("Absolute value of" + D2 + "is:" + math.abs (D2));
Long L1 = 3L;
Long L2 = -4l; System.out.println ("Absolute value of" + L1 + "is:" + math.abs (L1));
System.out.println ("Absolute value of" + L2 + "is:" + math.abs (L2));}
}
In the above code, we use floating-point numbers, double numbers, long digits to test the use of ABS functions, for other functions you can do the corresponding experiment according to the API. Everybody learns to be happy! For the Java learning, I suggest you practice more, more hands-on.
Java Math function library API (GO)