Java Math Class

We’ll cover the following

  • Math class
  • Math class constants
  • Java mathematical functions
  • Examples

Math class

Java’s built-in operators are useful, but they cannot provide all the mathematical needs of most Java programmers.

We can use Java Math class to do more complicated math equation.

Java Math class includes methods that perform a wide variety of mathematical calculations, from basic functions such as calculating an absolute value or a square root to trigonometry functions such as sin and cos (sine and cosine), to practical functions such as rounding numbers or generating random numbers.

All the methods of the Math class are declared as static methods, which means you can use them by specifying the class name Math followed by a period and a method name.

Here’s a statement that calculates the square root of a number stored in a variable named y:

double x = Math.sqrt(y);

The Math class is contained in the java.lang package, which is automatically available to all Java programs. As a result, you don’t have to provide an import statement to use the Math class.

Math class constants

The Math class defines two constants that are useful for many mathematical calculations.

ConstantWhat It IsValue
PIThe constant pi, the ratio of a circle’s radius and
diameter
3.141592653589793
EThe base of natural logarithms2.718281828459045

Note that these constants are only approximate values, because both PI and e are irrational numbers.

The following program illustrates a typical use of the constant PI.

The program calculates the area of the circle.

package JAVA.Basic;

public class Area {
    public static void main(String[] args) {
        double r, area;
        r = 10.8; // radius of circle
        area = Math.PI * r * r; // compute area
        System.out.println("Area of circle is " + area);
    }
}

The Math class’s PI constant has a precision of just 15 digits.

Java mathematical functions

The following table lists the basic mathematical functions that are provided by the Math class.

MethodExplanation
abs(argument)Returns the absolute value of the argument.
The argument can be an int, long, float, or double.
The return value is the same type as the argument.
cbrt(argument)Returns the cube root of the argument.
The argument and return value are doubles.
exp(argument)Returns e raised to the power of the argument.
The argument and the return value are doubles.
hypot(arg1, arg2)Returns the hypotenuse of a right triangle calculated according to
the Pythagorean theorem – square root of (xx + yy).
The argument and the return values are doubles.
log(argument)Returns the natural logarithm (base e) of the argument.
The argument and the return value are doubles.
log10(argument)Returns the base 10 logarithm of the argument.
The argument and the return value are doubles.
max(arg1, arg2)Returns the larger of the two arguments.
The arguments can be int, long, float, or double,
but both must be of the same type.
The return type is the same type as the arguments.
min(arg1, arg2)Returns the smaller of the two arguments.
The arguments can be int, long, float, or double,
but both must be of the same type.
The return type is the same type as the arguments.
pow(arg1, arg2)Returns the value of the first argument raised to the power of the second argument.
Both arguments and the return value are doubles.
random()Returns a random number that’s greater than or equal to 0.0 but less than 1.0.
This method doesn’t accept an argument, but the
return value is a double.
signum(argument)Returns a number that represents the sign of the argument: -1.0 if
the argument is negative, 0.0 if the argument is zero, and 1.0 if the
argument is positive.
The argument can be a double or a float.
The return value is the same type as the argument.
sqrt(argument)Returns the square root of the argument.
The argument and return value are doubles.

The following code uses the Mathematical Methods of the Math Class

package JAVA.Basic;

public class MathsClass {
    public static void main(String[] args) {
        int a = 10;
        int b = -5;
        int c = 3;
        double x = 25.0;
        double y = 3.0;
        double z = 4.0;

        System.out.println("abs(b) = " + Math.abs(b));
        System.out.println("cbrt(x) = " + Math.cbrt(x));
        System.out.println("exp(y) = " + Math.exp(z));
        System.out.println("hypot(y,z)= " + Math.hypot(y, z));
        System.out.println("log(y) = " + Math.log(y));
        System.out.println("log10(y) = " + Math.log10(y));
        System.out.println("max(a, b) = " + Math.max(a, b));
        System.out.println("min(a, b) = " + Math.min(a, b));
        System.out.println("pow(a, c) = " + Math.pow(a, c));
        System.out.println("random() = " + Math.random());
        System.out.println("signum(b) = " + Math.signum(b));
        System.out.println("sqrt(x) = " + Math.sqrt(y));

    }
}

Output of the following code is :

More Examples

You can use the abs and signnum methods to force the sign of one variable to match the sign of another, like this:

int a = 27; 
int b = -32; 

a = Math.abs(a) * Math.signum(b); // a is now -27;

You can use the pow method to square a number, like this:

double x = 4.0; 

double y = Math.pow(x, 2); // a is now 16;

Simply multiplying the number by itself, however, is often just as easy and just as readable:

double x = 4.0; 

double y = x * x; // a is now 16;

That’s it!
You have successfully completed the post. Do Share : )

Peace Out!

Also Read – Java String and String Literals


Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , Swiggy


3 Comments

Leave a Reply

Your email address will not be published.