Differentiation of Java Math

If you are interested to learn about the Java Operators

The Java Math class has many methods that allows you to perform mathematical tasks on numbers. Unlike some of the StrictMath class numeric methods, all implementations of the equivalent function of Math class can’t define to return the bit-for-bit same results. This relaxation permits implementation with better-performance where strict reproducibility is not required. If the size is int or long and the results overflow the range of value, the methods addExact(), subtractExact(), multiplyExact(), and toIntExact() throw an ArithmeticException.

For other arithmetic operations like increment, decrement, divide, absolute value, and negation overflow occur only with a specific minimum or maximum value. It should be checked against the maximum and minimum value as appropriate.

Do I need to import math in Java?

Math class allows the use of many common mathematical functions that can be used while creating programs. Since it is in the java. lang package, the Math class does not need to be imported. However, in programs extensively utilizing these functions, a static import can be used.

Math.max(x,y)

The Math.max(x,y) method can be used to find the highest value of x and y:

Example

Math.max(5, 10);

Math.min(x,y)

The Math.min(x,y) method can be used to find the lowest value of x and y:

Example

Math.min(5, 10);

Math.sqrt(x)

The Math.sqrt(x) method returns the square root of x:

Example

Math.sqrt(64);

Math.abs(x)

The Math.abs(x) method returns the absolute (positive) value of x:

Example

Math.abs(-4.7);

Random Numbers

Math.random() returns a random number between 0.0 (inclusive), and 1.0 (exclusive):

Example

Math.random();

To get more control over the random number, e.g. you only want a random number between 0 and 100, you can use the following formula:

Example

int randomNum = (int)(Math.random() * 101);  // 0 to 100

Logarithmic Math Methods

MethodDescription
Math.log()It returns the natural logarithm of a double value.
Math.log10()It is used to return the base 10 logarithm of a double value.
Math.log1p()It returns the natural logarithm of the sum of the argument and 1.
Math.exp()It returns E raised to the power of a double value, where E is Euler’s number and it is approximately equal to 2.71828.
Math.expm1()It is used to calculate the power of E and subtract one from it.

Trigonometric Math Methods

MethodDescription
Math.sin()It is used to return the trigonometric Sine value of a Given double value.
Math.cos()It is used to return the trigonometric Cosine value of a Given double value.
Math.tan()It is used to return the trigonometric Tangent value of a Given double value.
Math.asin()It is used to return the trigonometric Arc Sine value of a Given double value
Math.acos()It is used to return the trigonometric Arc Cosine value of a Given double value.
Math.atan()It is used to return the trigonometric Arc Tangent value of a Given double value.

Hyperbolic Math Methods

MethodDescription
Math.sinh()It is used to return the trigonometric Hyperbolic Cosine value of a Given double value.
Math.cosh()It is used to return the trigonometric Hyperbolic Sine value of a Given double value.
Math.tanh()It is used to return the trigonometric Hyperbolic Tangent value of a Given double value.

Angular Math Methods

MethodDescription
Math.toDegreesIt is used to convert the specified Radians angle to equivalent angle measured in Degrees.
Math.toRadiansIt is used to convert the specified Degrees angle to equivalent angle measured in Radians.
Differentiation of Java Math
Show Buttons
Hide Buttons