Java Math Class (Methods With Examples)

The Java Math class is an essential component of the Java programming language's standard library, providing a diverse set of mathematical functionalities. Within this class, developers can access static functions for executing fundamental arithmetic computations like min, max, random, round, and others. Moreover, it includes functionalities for advanced calculations such as determining logarithmic values and trigonometric operations.

The Java Math class offers a variety of functions for performing mathematical operations such as finding the minimum and maximum values, calculating averages, determining trigonometric functions like sine, cosine, and tangent, rounding numbers, and obtaining absolute values, among others.

In contrast to certain numeric methods in the StrictMath class, all versions of the corresponding function in the Math class are not mandated to produce identical results bit by bit. This flexibility allows for implementations that prioritize performance over strict reproducibility.

Java Math Methods Example

Example

Example

public class Main    

{    

    public static void main(String[] args)     

    {    

        // abs() returns the absolute number

        System.out.println("Absolute number is: " +Math.abs(-10));   

        // min() returns the minimum of two numbers  

        System.out.println("Minimum number is: " +Math.min(10, 20));   

        // max() returns the maximum of two numbers  

        System.out.println("Maximum number is: " +Math.max(10, 20));   

        // sqrt() returns the square root of given number  

        System.out.println("Square root of y is: " + Math.sqrt(49));   

        // pow() returns 10 power of 4 i.e. 10*10*10*10 

        System.out.println("Power of x and z is: " + Math.pow(10, 4));      

        // random() returns a random number   

        System.out.println("Random number is: " +(int)(Math.random()*10));    

    }    

}

Output:

Output

Absolute number is: 10

Minimum number is: 10

Maximum number is: 20

Square root of y is: 7.0

Power of x and z is: 10000.0

Random number is: 7

Java Math Class Methods

A significant capability of the Math class is its functionality for manipulating floating-point values. These functions are designed to manage a range of mathematical operations related to decimals, fractions, as well as extremely large or small values. For instance, the Math.round function can round a floating-point value to the closest whole number, and Math.random is able to produce a random decimal between 0.0 and 1.0.

The Math class offers functionalities for handling angles and trigonometric operations. It contains functions for computing sine, cosine, and tangent values, along with their corresponding inverse functions. These functionalities are valuable for tasks such as determining distances and angles in geometric problems or replicating real-world scenarios in fields like physics and engineering.

Basic Math Methods

Method Description
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

Method Description
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

Method Description
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

Method Description
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

Method Description
Math.toDegrees It is used to convert the specified Radians angle to equivalent angle measured in Degrees.
Math.toRadians It is used to convert the specified Degrees angle to equivalent angle measured in Radians.

Java Math Example 2

JavaMathExample1.java

Example

public class JavaMathExample1  

{  

    public static void main(String[] args)   

    {  

        double x = 28;  

        double y = 4;  

        

        // return the maximum of two numbers

        System.out.println("Maximum number of x and y is: " +Math.max(x, y)); 

        

        // return the square root of y 

        System.out.println("Square root of y is: " + Math.sqrt(y)); 

        

        //returns 28 power of 4 i.e. 28*28*28*28  

        System.out.println("Power of x and y is: " + Math.pow(x, y));    



        // return the logarithm of given value     

        System.out.println("Logarithm of x is: " + Math.log(x)); 

        System.out.println("Logarithm of y is: " + Math.log(y));

        

        // return the logarithm of given value when base is 10    

        System.out.println("log10 of x is: " + Math.log10(x)); 

        System.out.println("log10 of y is: " + Math.log10(y));  

        

        // return the log of x + 1

        System.out.println("log1p of x is: " +Math.log1p(x));  



        // return a power of 2  

        System.out.println("exp of a is: " +Math.exp(x));  

        

        // return (a power of 2)-1

        System.out.println("expm1 of a is: " +Math.expm1(x));

    }  

}

Output:

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

Java Math Example 3

JavaMathExample2.java

Example

public class JavaMathExample2  

{  

    public static void main(String[] args)   

    {  

        double a = 30;  

        

        // converting values to radian  

        double b = Math.toRadians(a); 

        

        // return the trigonometric sine of a    

        System.out.println("Sine value of a is: " +Math.sin(a));  

        

        // return the trigonometric cosine value of a

        System.out.println("Cosine value of a is: " +Math.cos(a));

        

        // return the trigonometric tangent value of a

        System.out.println("Tangent value of a is: " +Math.tan(a));

        

        // return the trigonometric arc sine of a    

        System.out.println("Sine value of a is: " +Math.asin(a));  

        

        // return the trigonometric arc cosine value of a

        System.out.println("Cosine value of a is: " +Math.acos(a));

        

        // return the trigonometric arc tangent value of a

        System.out.println("Tangent value of a is: " +Math.atan(a));



        // return the hyperbolic sine of a    

        System.out.println("Sine value of a is: " +Math.sinh(a));  

        

        // return the hyperbolic cosine value of a

        System.out.println("Cosine value of a is: " +Math.cosh(a));

        

        // return the hyperbolic tangent value of a

        System.out.println("Tangent value of a is: " +Math.tanh(a));

    }  

}

Output:

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 Example 4

Filename: MathDemo.java

Example

// Java program for demonstrating the features and functionalities of Java Math class with methods.

public class MathDemo {

    public static void main(String[] args) {

        double x = 28;

        double y = 4;

        // Basic arithmetic operations

        System.out.println("Addition: " + (x + y));

        System.out.println("Subtraction: " + (x - y));

        System.out.println("Multiplication: " + (x * y));

        System.out.println("Division: " + (x / y));

        // Square root

        System.out.println("Square root of " + x + ": " + Math.sqrt(x));

        // Cube root

        System.out.println("Cube root of " + x + ": " + Math.cbrt(x));

        // Power

        System.out.println("Power of " + x + " to " + y + ": " + Math.pow(x, y));

        // Trigonometric functions

        double angle = 45.0;

        double radian = Math.toRadians(angle);

        System.out.println("Sine of " + angle + " degrees: " + Math.sin(radian));

        System.out.println("Cosine of " + angle + " degrees: " + Math.cos(radian));

        System.out.println("Tangent of " + angle + " degrees: " + Math.tan(radian));

        // Rounding

        double value = -123.456;

        System.out.println("Absolute value of " + value + ": " + Math.abs(value));

        System.out.println("Ceil value of " + value + ": " + Math.ceil(value));

        System.out.println("Floor value of " + value + ": " + Math.floor(value));

        System.out.println("Round value of " + value + ": " + Math.round(value));

        // Random numbers

        System.out.println("Random number between 0.0 and 1.0: " + Math.random());

        System.out.println("Random number between 0 and 100: " + (int) (Math.random() * 100));

        // Maximum and minimum

        double[] numbers = {10.5, 20.7, 5.2, 30.9};

        System.out.println("Maximum value: " + Math.max(numbers[0], Math.max(numbers[1], Math.max(numbers[2], numbers[3]))));

        System.out.println("Minimum value: " + Math.min(numbers[0], Math.min(numbers[1], Math.min(numbers[2], numbers[3]))));

        // Exponential and logarithmic functions

        System.out.println("e^" + x + ": " + Math.exp(x));

        System.out.println("Logarithm base 10 of " + x + ": " + Math.log10(x));

        System.out.println("Logarithm base e of " + x + ": " + Math.log(x));

        // Hypotenuse

        double side1 = 3.0;

        double side2 = 4.0;

        System.out.println("Hypotenuse of a right triangle with sides " + side1 + " and " + side2 + ": " + Math.hypot(side1, side2));

        // Trigonometric functions (inverse)

        double sinValue = 0.5;

        System.out.println("Arcsine of " + sinValue + ": " + Math.toDegrees(Math.asin(sinValue)));

        System.out.println("Arccosine of " + sinValue + ": " + Math.toDegrees(Math.acos(sinValue)));

        System.out.println("Arctangent of " + sinValue + ": " + Math.toDegrees(Math.atan(sinValue)));

        // Constants

        System.out.println("Value of PI: " + Math.PI);

        System.out.println("Value of E: " + Math.E);

    }

}

Output:

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

The Math class provides functions for computing exponential and logarithmic operations. For instance, Math.exp computes the exponential value of e to a specified power, and Math.log determines the natural logarithm of a given number. These functionalities play a crucial role in various mathematical and scientific computations.

To sum up, the Java Math class offers a wide array of mathematical functions crucial for numerous programming assignments. Whether you are handling fundamental arithmetic operations, dealing with trigonometric calculations, or computing exponential functions, the Math class is your go-to resource. Its extensive collection of methods equips developers with a robust solution for tackling a diverse range of mathematical challenges.

Input Required

This code uses input(). Please provide values below: