The math class is a powerful class of methods for performing mathematical computations. An example of how the math class can be applied is illustrated below. The example below computes the square root of 17. The result is assigned to the double variable p because square roots typically do not result in integer values.
double p = Math.square(17);
In the above example,
Java provides an extensive library of Math operations. Below is a description of some of them. Notice that each method
has a corresponding signature. The signature of a method can be interpreted as follows,
Method | Signature | Description |
---|---|---|
abs | int abs(int x) | Returns the absolute value of x |
abs | double abs(double x) | Returns the absolute value of x |
pow | double pow(double d, double e) | Returns d raised to the e power |
sqrt | double sqrt(double x) | Returns the square root of x |
ceil | double ceil(double x) | Returns next highest whole number from x |
floor | double floor(double x) | Returns next lowest whole number from x |
min | double min(double a, double b) | Returns the smaller of a and b |
max | double max(double a, double b) | Returns the larger of a and b |
min | int min(int a, int b) | Returns the smaller of a and b |
max | int max(int a, int b) | Returns the larger of a and b |
random | double random() | Returns a random double (range 0 < r < 1) |
round | long round(double x) | Returns x rounded to the nearest whole number |
PI | double PI | Returns 3.1459625… |
Below are examples of each of the methods described above,
double d = -379.22;
System.out.println(Math.abs(d));//379.22
double d = 42.01;
double e = 3.728;
System.out.println(Math.pow(a,e));//1126831.027
double d = 2034.56;
System.out.println(Math.sqrt(d));//45.10609715
double d = 1.4;
System.out.println(Math.ceil(d));//2.0
double d = -1.6;
System.out.println(Math.ceil(d));//-1.0
double d = 1.4;
System.out.println(Math.floor(d));//1.0
double d = -1.6;
System.out.println(Math.floor(d));//-2.0
double d = 7.89;
System.out.println(Math.log(d));//2.065596135 *log is base e
double x = 2038.5;
double y = -8999.0;
System.out.println(Math.min(x, y));//-8999.0
double x = 2038.5;
double y = -8999.0;
System.out.println(Math.max(x, y));//2038.5
double x = 148.2;
System.out.println(Math.round(x));//148
double x = 148.7;
System.out.println(Math.round(x));//149
double x = -148.2;
System.out.println(Math.round(x));//-148
double x = -148.7;
System.out.println(Math.round(x));//-149
System.out.println(Math.PI);//3.14159265...
System.out.println(Math.random());//prints a random number between 0 and 1 where 0 is inclusive, but 1 is not
Many applications you will create will require a random number. For example, what if you needed to write a program to generate a number that represented a face from a 6-sided die, or a card from a 52 card deck?
The random() method in the Math class generates a random double type number between 0 and 1, where 0 is inclusive, but 1 is not. The below code is illustrative,
System.out.println(Math.random());//prints a random double from 0 up to 1
To create a number in a different range, say 0 up to 10, simply multiply the result of Math.random() by the desired range. An example is shown below,
System.out.println(Math.random()*10);//prints a random double from 0 up to 10
Recall, however that the random() method returns a double. The below code illustrates how to generate a random int from 0 up to 10,
int randomNumber = (int)(Math.random()*10);
System.out.println(randomNumber);//prints a random int from 0 up to 10
The above examples, illustrate how to scale the random() method to a specified range. The example below illustrates how to shift the range of the random number.
int randomNumber = (int)(Math.random()*10)+100;
System.out.println(randomNumber);//prints a random int from 100 up to 110
Method | Signature | Description |
---|---|---|
log | double log(double x) | Returns log base e of x |
sin | double sin(double a) | Returns the sin of angle a in radians |
cos | double cos(double a) | Returns the cos of angle a in radians |
tan | double tan(double a) | Returns the tangent of angle a in radians |
asin | double asin(double x) | Returns the arcsine of x in the range –PI/2 to PI/2 |
acos | double acos(double x) | Returns the arctan of x in the range –PI/2 to PI/2 |
toDegrees | double toDegrees(double angRad) | Converts radians to degrees |
toRadians | double toRadians(double angDeg) | Converts degrees to radians |