Java Math class is included in lava.lang package with is implicitly imported in each Java class. So, you don’t have to explicitly import Java Math class.
Java Math class is very useful for mathematical operation like elementary exponential, logarithm, square root, and trigonometric functions. I will now briefly introduce each function available in Java Math class. You will find few examples as well.
Java Math – Method Summary
Following methods are available in Java Math class:
- abs(double)
Signature is : static double abs(double a). It returns the absolute value of a double value.
- abs(float)
Signature is: static float abs(float a). It returns the absolute value of a float value.
- abs(int)
Signature is static int abs(int a). It returns the absolute value of an int value.
- abs(long)
Signature is static long abs(long a). It returns the absolute value of a long value.
Example:
Output:Code:System.out.println("Math abs function: " + Math.abs(1234.59));
System.out.println("Math abs function: " + (Math.abs(-0.0)));
System.out.println(("Math abs function: " + Math.abs(Float.NEGATIVE_INFINITY)));
System.out.println(("Math abs function: " + Math.abs(Float.NaN)));
Math abs function can take int, long, float or double argument. So its really flexible.Quote:
Math abs function: 1234.59
Math abs function: 0.0
Math abs function: Infinity
Math abs function: Nan
- acos(double)
Signature is static double acos(double a). It returns the arc cosine of an angle, in the range of 0.0 through pi.
- asin(double a)
Signature is: static double asin(double a). It returns the arc sine of an angle, in the range of -pi/2 through pi/2.
- atan(double a)
Signature is: static double atan(double a). It returns the arc tangent of an angle, in the range of -pi/2 through pi/2.
- atan2(double y, double x)
Signature is: static double atan2(double y, double x). Converts rectangular coordinates (x, y) to polar (r, theta).
- ceil(double a)
Signature is: static double ceil(double a). It returns the smallest (closest to negative infinity) double value that is not less than the argument and is equal to a mathematical integer.
Few important point about ceil function are:
- It returns the argument if the argument is already an integer.
- It returns the argument if the argument is infinity or NaN.
- It returns 0 if the argument is between -1.0 and 0.
Example:
Output:Code:System.out.println("Math ceil function: " + Math.ceil( 9.01));
System.out.println("Math ceil function: " + Math.ceil(-9.01));
System.out.println("Math ceil function: " + Math.ceil(10));
System.out.println("Math ceil function: " + Math.ceil(-0.03));
System.out.println("Math ceil function: " + Math.ceil(Double.NaN));
Quote:
Math ceil function: 10.0
Math ceil function: -9.0
Math ceil function: 10.0
Math ceil function: -0.0
Math ceil function: NaN
- cos(double a)
Signature is: static double cos(double a). It returns the trigonometric cosine of an angle.
- exp(double a)
Signature is: static double exp(double a). It returns Euler's number e raised to the power of a double value.
- floor(double a)
Signature is: static double floor(double a). It returns the largest (closest to positive infinity) double value that is not greater than the argument and is equal to a mathematical integer.
Example:
Output:Code:double a = 1.55;
System.out.println("Floor of " + a + " is: " + Math.floor(a));
double b = 1.95;
System.out.println("Floor of " + b + " is: " + Math.floor(a));
The above example is obvious. We calculated floor value of 1.55 and 1.95 and results were printed on the console.Quote:
Floor of 1.55 is: 1.0
Floor of 1.95 is: 1.0
- IEEEremainder(double f1, double f2)
Signature is: static double IEEEremainder(double f1, double f2). Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard.
- log(double a)
Signature is: static double log(double a). It returns the natural logarithm (base e) of a double value.
- max(double a, double b)
Signature is: static double max(double a, double b). It returns the greater of two double values.
- max(float a, float b)
Signature is: static float max(float a, float b). It returns the greater of two float values.
- max(int a, int b)
Signature is: static int max(int a, int b). It returns the greater of two int values.
- max(long a, long b)
Signature is: static long max(long a, long b). It returns the greater of two long values.
- min(double a, double b)
Signature is: static double min(double a, double b). It returns the smaller of two double values.
- min(float a, float b)
Signature is : static float min(float a, float b). It returns the smaller of two float values.
Example:
Output:Code:int i_a = 1;
int i_b = 2;
System.out.println("Integer: Maximum of " + i_a + " and " + i_b + " is: " + Math.max(i_a,i_b));
long l_a = 1l;
long l_b = 2l;
System.out.println("Long: Maximum of " + l_a + " and " + l_b + " is: " + Math.max(l_a,l_b));
float f_a = 1.55f;
float f_b = 2.22f;
System.out.println("Float: Maximum of " + f_a + " and " + f_b + " is: " + Math.max(f_a,f_b));
double d_a = 1.55d;
double d_b = 2.22d;
System.out.println("Double : Maximum of " + d_a + " and " + d_b + " is: " + Math.max(d_a,d_b));
Java Math’s max function can be used for int, long, float or double parameters. Above example will clear your concepts.Quote:
Integer: Maximum of 1 and 2 is: 2
Long: Maximum of 1 and 2 is: 2
Float: Maximum of 1.55 and 2.22 is: 2.22
Double : Maximum of 1.55 and 2.22 is: 2.22
- min(int a, int b)
Signature is: static int min(int a, int b). It returns the smaller of two int values.
- min(long a, long b)
Signature is: static long min(long a, long b). It returns the smaller of two long values.
Example:
Output:Code:int i_a = 1;
int i_b = 2;
System.out.println("Integer: Minimum of " + i_a + " and " + i_b + " is: " + Math.min(i_a,i_b));
long l_a = 1l;
long l_b = 2l;
System.out.println("Long: Minimum of " + l_a + " and " + l_b + " is: " + Math.min(l_a,l_b));
float f_a = 1.55f;
float f_b = 2.22f;
System.out.println("Float: Minimum of " + f_a + " and " + f_b + " is: " + Math.min(f_a,f_b));
double d_a = 1.55d;
double d_b = 2.22d;
System.out.println("Double : Minimum of " + d_a + " and " + d_b + " is: " + Math.min(d_a,d_b));
Quote:
Integer: Minimum of 1 and 2 is: 1
Long: Minimum of 1 and 2 is: 1
Float: Minimum of 1.55 and 2.22 is: 1.55
Double : Minimum of 1.55 and 2.22 is: 1.55
- pow(double a, double b)
Signature is: static double pow(double a, double b). It returns the value of the first argument raised to the power of the second argument.
Example:
Output:Code:double i_a = 2;
double i_b = 3;
System.out.println("POW: " + i_a + " power " + i_b + " is " + Math.pow(i_a, i_b));
POW function simply calculates power of double value. In the above example, we calculated 2 power 3. This could be done like 2 * 2 * 2. I have seen many developers doing like this specially when power is less than 5. this makes the code ugly You should use POW function where ever need. It’s the right way of doing it.Quote:
POW: 2 power 3 is 8.0
- random()
Signature is: static double random(). It returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
Example:
Output:Code:System.out.println("RANDOM: " + Math.random());
As written before, random methof of Java Math class generates random value between 0.0 to 1.0. Output I got is 0.2430685678435166. If I run the code again, I might get another output. So output in not gurenteed while using random function but we are sure that generated value will be between 0.0 to 1.0.Quote:
RANDOM: 0.2430685678435166
- rint(double a)
Signature is: static double rint(double a). It returns the double value that is closest in value to the argument and is equal to a mathematical integer.
- round(double a)
Signature is: static long round(double a). It returns the closest long to the argument.
- round(float a)
Signature is: static int round(float a). It returns the closest int to the argument.
- sin(double a)
Signature is: static double sin(double a). returns the trigonometric sine of an angle.
- sqrt(double a)
Signature is: static double sqrt(double a). It returns the correctly rounded positive square root of a double value.
Example:
Output:Code:double d_a = 4;
System.out.println("Square Root of " + d_a + " is " + Math.sqrt(d_a));
Quote:
Square Root of 4.0 is 2.0
- tan(double a)
Signature is: static double tan(double a). It returns the trigonometric tangent of an angle.
- toDegrees(double angrad)
Signature is: static double toDegrees(double angrad), Converts an angle measured in radians to an approximately equivalent angle measured in degrees.
- toRadians(double angdeg)
Signature is: static double toRadians(double angdeg). Converts an angle measured in degrees to an approximately equivalent angle measured in radians.
Now I will give a comprehensive example that will use various Java Match class finctions.
Example:
Output:Code:// E and round()
System.out.println("e = " + Math.round(Math.E*100)/100f);
// PI
System.out.println("pi = " + Math.round(Math.PI*100)/100f);
// abs()
System.out.println("Absolute number = " + Math.abs(Math.PI));
// ceil()
System.out.println("Smallest value but greater than the argument = " + Math.ceil(Math.PI));
// exp()
System.out.println("Exponent number powered by the argument = " + Math.exp(0));
// floor()
System.out.println("Largest value but less than the argument = " + Math.floor(Math.E));
// IEEEremainder()
System.out.println("Remainder = " + Math.IEEEremainder(5.3f,2.2f));
// max()
System.out.println("Maximum Number = " + Math.max(10,10.3));
// min()
System.out.println("Minimum Number = " + Math.min(10,10.3));
// pow()
System.out.println("Power = " + Math.pow(10,3));
// random()
System.out.println("Random Number = " + Math.random());
// rint()
System.out.println("Closest to the Argument = " + Math.rint(30));
// round()
System.out.println("Round = " + Math.round(Math.E));
// sqrt()
System.out.println("Square Root = " + Math.sqrt(400));
Quote:
e = 2.72
pi = 3.14
Absolute number = 3.141592653589793
Smallest value but greater than the argument = 4.0
Exponent number powered by the argument = 1.0
Largest value but less than the argument = 2.0
Remainder = 0.9000000953674316
Maximum Number = 10.3
Minimum Number = 10.0
Power = 1000.0
Random Number = 0.16520223136283352
Closest to the Argument = 30.0
Round = 3
Square Root = 20.0
