Java Math Class

The Java Math class is a fundamental part of the Java language's standard library, offering a wide range of mathematical functions. It provides static methods for performing basic arithmetic operations like addition, subtraction, multiplication, and division. Additionally, it offers methods for more complex operations such as finding the maximum or minimum of two numbers, raising a number to a power, and calculating logarithms and trigonometric functions.

Java Math class provides several methods to work on math calculations like min(), max(), avg(), sin(), cos(), tan(), round(), ceil(), floor(), abs() etc.

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.

Example 1

JavaMathExample1.java

Test it Now

Output:

Maximum number of x and y is: 28.0
Square root of y is: 2.0
Power of x and y is: 614656.0
Logarithm of x is: 3.332204510175204
Logarithm of y is: 1.3862943611198906
log10 of x is: 1.4471580313422192
log10 of y is: 0.6020599913279624
log1p of x is: 3.367295829986474
exp of a is: 1.446257064291475E12
expm1 of a is: 1.446257064290475E12

Example 2

JavaMathExample2.java

Test it Now

Output:

Sine value of a is: -0.9880316240928618
Cosine value of a is: 0.15425144988758405
Tangent value of a is: -6.405331196646276
Sine value of a is: NaN
Cosine value of a is: NaN
Tangent value of a is: 1.5374753309166493
Sine value of a is: 5.343237290762231E12
Cosine value of a is: 5.343237290762231E12
Tangent value of a is: 1.0

Java Math Class Methods

One of the key features of the Math class is its support for working with floating-point numbers. These methods can handle a variety of mathematical tasks involving decimals, fractions, and very large or very small numbers. For example, the Math.round() method can round a floating-point number to the nearest integer, while Math.random() can generate a random number between 0.0 and 1.0.

The Math class also provides methods for working with angles and trigonometric functions. It includes methods for calculating sine, cosine, and tangent, as well as their inverse functions. These functions are useful for tasks like calculating distances and angles in geometry, or for simulating natural phenomena in physics and engineering.

Basic Math Methods

MethodDescription
Math.abs()It will return the Absolute value of the given value.
Math.max()It returns the Largest of two values.
Math.min()It is used to return the Smallest of two values.
Math.round()It is used to round of the decimal numbers to the nearest value.
Math.sqrt()It is used to return the square root of a number.
Math.cbrt()It is used to return the cube root of a number.
Math.pow()It returns the value of first argument raised to the power to second argument.
Math.signum()It is used to find the sign of a given value.
Math.ceil()It is used to find the smallest integer value that is greater than or equal to the argument or mathematical integer.
Math.copySign()It is used to find the Absolute value of first argument along with sign specified in second argument.
Math.nextAfter()It is used to return the floating-point number adjacent to the first argument in the direction of the second argument.
Math.nextUp()It returns the floating-point value adjacent to d in the direction of positive infinity.
Math.nextDown()It returns the floating-point value adjacent to d in the direction of negative infinity.
Math.floor()It is used to find the largest integer value which is less than or equal to the argument and is equal to the mathematical integer of a double value.
Math.floorDiv()It is used to find the largest integer value that is less than or equal to the algebraic quotient.
Math.random()It returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.
Math.rint()It returns the double value that is closest to the given argument and equal to mathematical integer.
Math.hypot()It returns sqrt(x2 +y2) without intermediate overflow or underflow.
Math.ulp()It returns the size of an ulp of the argument.
Math.getExponent()It is used to return the unbiased exponent used in the representation of a value.
Math.IEEEremainder()It is used to calculate the remainder operation on two arguments as prescribed by the IEEE 754 standard and returns value.
Math.addExact()It is used to return the sum of its arguments, throwing an exception if the result overflows an int or long.
Math.subtractExact()It returns the difference of the arguments, throwing an exception if the result overflows an int.
Math.multiplyExact()It is used to return the product of the arguments, throwing an exception if the result overflows an int or long.
Math.incrementExact()It returns the argument incremented by one, throwing an exception if the result overflows an int.
Math.decrementExact()It is used to return the argument decremented by one, throwing an exception if the result overflows an int or long.
Math.negateExact()It is used to return the negation of the argument, throwing an exception if the result overflows an int or long.
Math.toIntExact()It returns the value of the long argument, throwing an exception if the value overflows an int.

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.

Let's understand about this in detail with the help of a Java example program.

Filename: MathDemo.java

Output:

Addition: 32.0
Subtraction: 24.0
Multiplication: 112.0
Division: 7.0
Square root of 28.0: 5.291502622129181
Cube root of 28.0: 3.0365889718756627
Power of 28.0 to 4.0: 614656.0
Sine of 45.0 degrees: 0.7071067811865475
Cosine of 45.0 degrees: 0.7071067811865476
Tangent of 45.0 degrees: 0.9999999999999999
Absolute value of -123.456: 123.456
Ceil value of -123.456: -123.0
Floor value of -123.456: -124.0
Round value of -123.456: -123
Random number between 0.0 and 1.0: 0.40493356810101455
Random number between 0 and 100: 61
Maximum value: 30.9
Minimum value: 5.2
e^28.0: 1.446257064291475E12
Logarithm base 10 of 28.0: 1.4471580313422192
Logarithm base e of 28.0: 3.332204510175204
Hypotenuse of a right triangle with sides 3.0 and 4.0: 5.0
Arcsine of 0.5: 30.000000000000004
Arccosine of 0.5: 60.00000000000001
Arctangent of 0.5: 26.56505117707799
Value of PI: 3.141592653589793
Value of E: 2.718281828459045

Filename: JavaMath.java

Additionally, the Math class offers methods for calculating exponential and logarithmic functions. For example, Math.exp() can calculate the value of e raised to a power, while Math.log() can calculate the natural logarithm of a number. These functions are essential for many mathematical and scientific calculations.

In conclusion, the Math class in Java provides a comprehensive set of mathematical functions that are essential for many programming tasks. Whether you need to perform basic arithmetic, work with trigonometry, or calculate exponential functions, the Math class has you covered. Its wide range of methods makes it a powerful tool for developers working on a variety of mathematical problems.






Latest Courses