java-lang-math
A Guide to the Java Math Class
1. Introduction
2. Basic Math Functions
The first set of methods we’ll cover are the basic math functions such as the absolute value, the square root, the maximum or the minimum between two values.
2.1. abs()
Math.abs(-5); // returns 5
Likewise, of others that we’ll see next, abs() accepts as a parameter an int, long, float or double and returns the relative one.
2.3. sqrt()
Math.sqrt(25); // returns 5
2.5. max()
Math.max(5,10); // returns 10
Here again, the method accepts int, long, float or double.
2.6. min()
Math.min(5,10); // returns 5
2.7. random()
double random = Math.random()
To do this, the method creates a single instance of java.util.Random() number generator when it is called for the first time.
After that, for all calls to this method, the same instance is used. Note that, the method is synchronized, thus can be used by more than one thread.
We can find more examples of how to generate a random in this article.
2.8. signum()
Math.signum(-5) // returns -1
This method returns 1.0 if the argument is greater than zero or -1.0 otherwise. If the argument is zero positive or zero negative, the result is the same as the argument.
The input can be a float or a double.
3. Exponential and Logarithmic Functions
In addition to the basic math functions, the Math class contains methods to solve exponential and logarithmic functions.
3.1. exp()
The exp() method receives a double argument and returns Euler’s number raised to the power of the argument (ex
):
Math.exp(1); // returns 2.718281828459045
4. Trigonometric Functions
When we have to work with geometric formulas, we always need trigonometric functions; the Math class provides these for us.
4.1. sin()
Receives a single, double argument that represents an angle (in radians) and returns the trigonometric sine:
Math.sin(Math.PI/2); // returns 1
4.2. cos()
Math.cos(0); // returns 1
4.3. tan()
Math.tan(Math.PI/4); // returns 1
4.4. sinh(), cosh(), tanh()
They return respectively the hyperbolic sine, hyperbolic cosine and hyperbolic tangent of a double value:
Math.sinh(Math.PI);
Math.cosh(Math.PI);
Math.tanh(Math.PI);
4.5. asin()
Math.asin(1); // returns pi/2
The result is an angle in the range –pi/2 to pi/2.
4.6. acos()
Math.acos(0); // returns pi/2
The result is an angle in the range 0 to pi.
4.7. atan()
Math.atan(1); // returns pi/4
The result is an angle in the range –pi/2 to pi/2.
4.8. atan2()
Finally, atan2() receives the ordinate coordinate y and the abscissa coordinate x, and returns the angle ϑ from the conversion of rectangular coordinates (x,y) to polar coordinates (r, ϑ):
Math.atan2(1,1); // returns pi/4
4.9. toDegrees()
Math.toDegrees(Math.PI); // returns 180
4.10. toRadians()
Math.toRadians(180); // returns pi
Remember that most of the methods we have seen in this section accept the argument in radians, thus, when we have an angle in degrees, this method should be used before using a trigonometric method.
For more examples, have a look in here.
5. Rounding and Other Functions
5.1. ceil()
ceil() is helpful when we have to round an integer to the smallest double value that is greater than or equal to the argument:
Math.ceil(Math.PI); // returns 4
5.2. floor()
To round a number to the largest double that is less than or equal to the argument we should use floor():
Math.floor(Math.PI); // returns 3
5.3. getExponent()
The argument can be a double or a float:
Math.getExponent(333.3); // returns 8
Math.getExponent(222.2f); // returns 7
5.4. IEEEreminder()
Computes the division between the first (dividend) and the second (divisor) argument and returns the remainder as prescribed by the IEEE 754 standard:
Math.IEEEremainder(5,2); // returns 1
5.5. nextAfter()
Math.nextAfter(1.95f,1); // returns 1.9499999
Math.nextAfter(1.95f,2); // returns 1.9500002
It accepts two arguments, the first is the value of which you want to know the adjacent number and the second is the direction.
5.6. nextUp()
Likewise the previous method, but this one returns the adjacent value only in the direction of a positive infinity:
Math.nextUp(1.95f); // returns 1.9500002
5.7. rint()
Math.rint(1.95f); // returns 2.0
5.8. round()
Equally to the above method, but this one returns an int value if the argument is a float and a long value if the argument is a double:
int result = Math.round(1.95f); // returns 2
long result2 = Math.round(1.95) // returns 2
5.9. scalb()
Scalb is an abbreviation for a “scale binary”. This function executes one shift, one conversion and a double multiplication:
Math.scalb(3, 4); // returns 3*2^4
5.10. ulp()
Math.ulp(1); // returns 1.1920929E-7
Math.ulp(2); // returns 2.3841858E-7
Math.ulp(4); // returns 4.7683716E-7
Math.ulp(8); // returns 9.536743E-7
5.11. hypot()
Math.hypot(4, 3); // returns 5
The method calculates the square root without intermediate overflow or underflow.
In this article, we use this method to calculate the distance between two points.
6. Java 8 Math Functions
The Math class has been revisited in Java 8 to include new methods to perform the most common arithmetic operations.
We discussed these methods in another article.
7. Constants Fields
public static final double E
public static final double PI
Which indicate the closer value to the base of the natural logarithms, and the closer value to pi, respectively.
8. Conclusion
As usual, all code snippets presented in here are available over on GitHub.