# Java Math Class With Examples [latest]

Jun 3, 2022

Java Math class is a part of the `java.lang` package. Basically Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root and trigonometric functions. Java Math is a final class and it extends `java.lang.Object`.

## Java Math Class Fields

Java Math class has below static fields:

1. `public static final double E`: The double value that is closer than any other to `e`, the base of the natural logarithms(2.718281828459045).
2. `public static final double PI`: The double value that is closer than any other to pi, the ratio of the circumference of a circle to its diameter(3.141592653589793).

## Java Math Functions

Java Math class contains static factory method for performing basic numeric operations. Let’s have a look at the below methods with examples.

1. `abs(double a)`: This method returns the absolute value of specified double value.
2. `abs(float a)`: This method returns the absolute value of specified float value.

NOTE: For the above methods let’s have a look at the below cases:

• If the specified argument is positive zero or negative zero, the result is positive zero.
• If the specified argument is not negative, then the argument is returned.
• If the specified argument is negative, then the negation of the argument is returned.
• If the specified argument is infinite, the result is positive infinity.
• If the specified argument is NaN, the result is NaN.
• `(int a)`: This method returns the absolute value of specified int value. If the specified argument is equal to the value of `Integer.MIN_VALUE(-231)` or the most negative representable int value, then the result is that same value, which is negative.
• `abs(long a)` method returns the absolute value of specified long value. If the specified argument is equal to the value of Long.MIN_VALUE(-263) or the most negative representable long value, then the result is that same value, which is negative.

NOTE: For the above methods let’s have a look at the below cases:

• If the specified argument is not negative, then the argument is returned.
• If the specified argument is negative, then the negation of the argument is returned.

Let’s have look at the below example program.

Output of the above program is below:

• `acos(double a)`: This method returns the arc cosine of the specified value of double. The value of the returned angle of cosine is in the range 0.0 through pi. If the specified argument is NaN or its absolute value is greater than 1, then the result is NaN.
• `(double a)`: This method returns the arc sine of the specified value of double. The value of the returned angle of sine is in the range –pi/2 through pi/2.
• If the specified argument is NaN or its absolute value is greater than 1, then the result is NaN.
• If the argument is zero, then the result is a zero with the same sign as the argument.
• `atan(double a)`: This method returns the arctangent of the specified value of double. The value of the returned angle of the tangent is in the range –pi/2 through pi/2.
• If the specified argument is NaN, then the result is NaN.
• If the argument is zero, then the result is a zero with the same sign as the argument.
• `atan2(double y, double x)`: This method returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta). This method computes the phase theta by computing an arc tangent of y/x in the range of -pi to pi.
• If either argument is NaN, then the result is NaN.
• If the first argument is positive zero and the second argument is positive, or the first argument is positive and finite and the second argument is positive infinity, then the result is positive zero.
• If the first argument is negative zero and the second argument is positive, or the first argument is negative and finite and the second argument is positive infinity, then the result is negative zero.
• If the first argument is positive zero and the second argument is negative, or the first argument is positive and finite and the second argument is negative infinity, then the result is the double value closest to pi.
• If the first argument is negative zero and the second argument is negative, or the first argument is negative and finite and the second argument is negative infinity, then the result is the double value closest to -pi.
• If the first argument is positive and the second argument is positive zero or negative zero, or the first argument is positive infinity and the second argument is finite, then the result is the double value closest to pi/2.
• If the first argument is negative and the second argument is positive zero or negative zero, or the first argument is negative infinity and the second argument is finite, then the result is the double value closest to -pi/2.
• If both arguments are positive infinity, then the result is the double value closest to pi/4.
• If the first argument is positive infinity and the second argument is negative infinity, then the result is the double value closest to 3*pi/4.
• If the first argument is negative infinity and the second argument is positive infinity, then the result is the double value closest to -pi/4.
• If both arguments are negative infinity, then the result is the double value closest to -3*pi/4.

Let’s have look at the below example program.

Output of the above program is below:

• `cbrt(double a)` This method returns the cube root of the specified value of double. For positive finite x, `cbrt(-x) == -cbrt(x);` that is, the cube root of a negative value is the negative of the cube root of that value’s magnitude.
• If the argument is NaN, then the result is NaN.
• If the argument is infinite, then the result is infinity with the same sign as the argument.
• If the argument is zero, then the result is a zero with the same sign as the argument.
• `ceil(double a)`: This method returns the smallest (closest to negative infinity) double value that is greater than or equal to the specified argument and is equal to a mathematical integer.
• If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
• If the argument is NaN or infinity or positive zero or negative zero, then the result is the same as the argument.
• If the argument value is less than zero but greater than -1.0, then the result is negative zero.

Note: value of Math.ceil(x) is exactly the value of -Math.floor(-x).

Let’s have look at the below example program.

Output of the above program is below:

• `exp(double a)`: This method returns the Euler’s number e raised to the power of a specified double value.
• If the argument is NaN, the result is NaN.
• If the argument is positive infinity, then the result is positive infinity.
• If the argument is negative infinity, then the result is positive zero.
• expm1(double x): This method Returns ex -1. Note that for values of x near 0, the exact sum of expm1(x) + 1 is much closer to the true result of ex than exp(x). Specified argument x is the exponent to raise e to in the computation of ex -1.
• If the argument is NaN, the result is NaN.
• If the argument is positive infinity, then the result is positive infinity.
• If the argument is negative infinity, then the result is -1.0.
• If the argument is zero, then the result is a zero with the same sign as the argument.

The result of expm1 for any finite input must be greater than or equal to -1.0.

• `floor(double a)`: This method Returns the largest (closest to positive infinity) double value that is less than or equal to the specified argument and is equal to a mathematical integer.
• If the argument value is already equal to a mathematical integer, then the result is the same as the argument.
• If the argument is NaN or infinity or positive zero or negative zero, then the result is the same as the argument.

Let’s have look at the below example program.

Output of the above program is below:

• `log(double a)`: This method returns the natural logarithm (base e) of a specified double value.
• If the argument is NaN or less than zero, then the result is NaN.
• If the argument is positive infinity, then the result is positive infinity.
• If the argument is positive zero or negative zero, then the result is negative infinity.
• `log10(double a)`: This method returns the base 10 logarithm of a specified double value.
• If the argument is NaN or less than zero, then the result is NaN.
• If the argument is positive infinity, then the result is positive infinity.
• If the argument is positive zero or negative zero, then the result is negative infinity.
• If the argument is equal to 10n for integer n, then the result is n.
• `log1p(double x)`: This method returns the natural logarithm of the sum of the argument and 1. Note that for small values x, the result of log1p(x) is much closer to the true result of ln(1 + x) than the floating-point evaluation of log(1.0+x).
• If the argument is NaN or less than -1, then the result is NaN.
• If the argument is positive infinity, then the result is positive infinity.
• If the argument is negative one, then the result is negative infinity.
• If the argument is zero, then the result is a zero with the same sign as the argument.

Let’s have look at the below example program.

Output of the above program is below:

• `max(double a, double b)`: This method returns the greater of specified two double values. The result is the argument closer to positive infinity.
• `max(float a, float b)` This method returns the greater of specified two float values. The result is the argument closer to positive infinity.

NOTE: For the above methods let’s have a look at the below cases:

• If either value of specified argument is NaN, then the result is NaN.
• If the arguments have the same value, the result is that same value.
• If one argument is positive zero and the other negative zero, the result is positive zero.
• `max(long a, long b)` This method returns the greater of specified two long values. The result is the argument closer to the value of Long.MAX_VALUE(263-1). If the arguments have the same value, the result is that same value.
• `max(int a, int b)` This method returns the greater of specified two int values. The result is the argument closer to the value of Integer.MAX_VALUE(231-1). If the arguments have the same value, the result is that same value.
• `min(double a, double b)` This method returns the smaller of specified two double values. The result is the argument closer to negative infinity.
• `min(float a, float b)` This method returns the smaller of specified two float values. The result is the argument closer to negative infinity.

NOTE: For the above methods let’s have a look at the below cases:

• If either value of specified argument is NaN, then the result is NaN.
• If the arguments have the same value, the result is that same value.
• If one argument is positive zero and the other negative zero, the result is negative zero.
• `min(long a, long b)` This method returns the smaller of specified two long values. The result is the argument closer to the value of Long.MIN_VALUE(-263). If the arguments have the same value, the result is that same value.
• `min(int a, int b)` This method returns the smaller of specified two int values. The result is the argument closer to the value of Integer.MIN_VALUE(-231). If the arguments have the same value, the result is that same value.

Let’s have look at the below example program.

Output of the above program is below:

• `pow(double a, double b)` This method returns the value of the first argument raised to the power of the second argument.
• If the second argument is positive or negative zero, then the result is 1.0.
• If the second argument is 1.0, then the result is the same as the first argument.
• If the second argument is NaN, then the result is NaN.
• If the first argument is NaN and the second argument is nonzero, then the result is NaN.
• If the absolute value of the first argument is greater than 1 and the second argument is positive infinity, or the absolute value of the first argument is less than 1 and the second argument is negative infinity, then the result is positive infinity.
• If the absolute value of the first argument is greater than 1 and the second argument is negative infinity, or the absolute value of the first argument is less than 1 and the second argument is positive infinity, then the result is positive zero.
• If the absolute value of the first argument equals 1 and the second argument is infinite, then the result is NaN.
• If the first argument is positive zero and the second argument is greater than zero, or the first argument is positive infinity and the second argument is less than zero, then the result is positive zero.
• If the first argument is positive zero and the second argument is less than zero, or the first argument is positive infinity and the second argument is greater than zero, then the result is positive infinity.
• If the first argument is negative zero and the second argument is greater than zero but not a finite odd integer, or the first argument is negative infinity and the second argument is less than zero but not a finite odd integer, then the result is positive zero.
• If the first argument is negative zero and the second argument is a positive finite odd integer, or the first argument is negative infinity and the second argument is a negative finite odd integer, then the result is negative zero.
• If the first argument is negative zero and the second argument is less than zero but not a finite odd integer, or the first argument is negative infinity and the second argument is greater than zero but not a finite odd integer, then the result is positive infinity.
• If the first argument is negative zero and the second argument is a negative finite odd integer, or the first argument is negative infinity and the second argument is a positive finite odd integer, then the result is negative infinity.
• If the first argument is finite and less than zero and
– if the second argument is a finite even integer, the result is equal to the result of raising the absolute value of the first argument to the power of the second argument
– if the second argument is a finite odd integer, the result is equal to the negative of the result of raising the absolute value of the first argument to the power of the second argument
– if the second argument is finite and not an integer, then the result is NaN.

Let’s have look at the below example program.

Output of the above program is below:

• `random()` This method returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudo randomly with uniform distribution from that range.When this method is first called, it creates a single new pseudorandom-number generator, exactly as if by the expression: new java.util.Random(). This new pseudorandom-number generator is used thereafter for all calls to this method and is used nowhere else.This method is properly synchronized to allow correct use by more than one thread. However, if many threads need to generate pseudorandom numbers at a great rate, it may reduce contention for each thread to have its own pseudorandom-number generator.
• `round(double a)` This method returns the closest long to the specified double argument, with ties rounding up.
• If the argument is NaN, the result is 0.
• If the argument is negative infinity or any value less than or equal to the value of Long.MIN_VALUE(-263), the result is equal to the value of Long.MIN_VALUE.
• If the argument is positive infinity or any value greater than or equal to the value of Long.MAX_VALUE(263-1), the result is equal to the value of Long.MAX_VALUE.
• `round(float a)` This method returns the closest int to the specified float argument, with ties rounding up.
• If the argument is NaN, the result is 0.
• If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE(-231), the result is equal to the value of Integer.MIN_VALUE.
• If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE(231-1), the result is equal to the value of Integer.MAX_VALUE.

Let’s have look at the below example program.

Output of the above program is below:

• `toDegrees(double angrad)` This method converts an angle measured in radians to an approximately equivalent angle measured in degrees. The conversion from radians to degrees is generally inexact.
• `toRadians(double angred)`: This method converts an angle measured in degrees to an approximately equivalent angle measured in radians. The conversion from degrees to radians is generally inexact.

Let’s have look at the below example program.

Output of the above program is below:

## New Methods in Math Class in Java 8

Let’s have a look at the below newly added methods in Math class in java 8 with examples.

1. `addExact(long x, long y)`: This method returns the sum of its specified long arguments, it throws ArithmeticException – if the result overflows a long.
2. `addExact(int x, int y)` This method returns the sum of its specified int arguments, it throws ArithmeticException – if the result overflows a int.

Let’s have look at the below example program.

Output of the above program is below:

1. `subtractExact(long x, long y)` This method returns the difference of the specified long arguments, it throws ArithmeticException – if the result overflows a long.
2. `subtractExact(int x, int y)` This method returns the difference of the specified int arguments, it throws ArithmeticException – if the result overflows an int.

Let’s have look at the below example program.

Output of the above program is below:

1. `incrementExact(int a)` This method returns the specified argument incremented by one, it throws ArithmeticException – if the result overflows an int.
2. `incrementExact(long a)` This method returns the specified argument incremented by one, it throws ArithmeticException – if the result overflows an long.

Let’s have look at the below example program.

Output of the above program is below:

1. `decrementExact(int a)` This method returns the specified argument decremented by one, it throws ArithmeticException – if the result overflows an int.
2. `decrementExact(long a)` This method returns the specified argument decremented by one, it throws ArithmeticException – if the result overflows an long.

Let’s have look at the below example program.

Output of the above program is below:

1. `multiplyExact(int x, int y)` This method returns the multiplication of the specified arguments, it throws ArithmeticException – if the result overflows an int.
2. `multiplyExact(long x, long y)` This method returns the multiplication of the specified arguments, it throws ArithmeticException – if the result overflows an long.

Let’s have look at the below example program.

Output of the above program is below:

1. `negateExact(int a)` This method returns the negation of the specified argument, it throws ArithmeticException – if the result overflows an int.
2. `negateExact(long a)` This method returns the negation of the specified argument, it throws ArithmeticException – if the result overflows an long.

Let’s have look at the below example program.

Output of the above program is below:

1. `floorDiv(int x, int y)` This method divides the first argument by second and call the floor() method upon returned result.
2. `floorMod(int x, int y)` This method returns the floor value of modulus of the specified arguments.

Let’s have look at the below example program.

Output of the above program is below:

That’s all for Java Math class, I hope nothing important got missed here.

References: Java 7 API Doc and Java 8 API Doc.