Java Math class - Math method in java with example

 

Java Math class

Java Math class

In java, there is a rich set of mathematical methods/functions provided in the Java Math class. Some of common math method of Java are sqrt(), pow(), min), max(), floor(), round(), ceil(), random() etc. some of method or function called automatically by default.

In the following program I have provided some of the common Java Math class methods with comments and outputs.

public class MathLibraryExample

{

    public static void main(String[] args)

    {

 

        double rint;

        double x = 72.5;

        double y = 79.7;

        int i, j;

        double a, b, c, num;

        a = 5.2;

        b = 5.9;

        c = -5;

       System.out.println("Math Function in Java");

       System.out.println("=====================");

       // Math.abs() will return absolute value(without any sign)

       System.out.println(c + " Absolute value is " + Math.abs(c));

      

       /*

       Math.round() method returns the value in rounded form.

       It will return the same value if the number is

       below ½ or .5 otherwise it return the next higher integer value.

       */

       

        System.out.println(x + " is approximately " + Math.round(x));

        System.out.println(y + " is approximately " + Math.round(y));

       

        /*The Math.ceil() return the rounded value to the

         * next higher integer and it return double type value */

        System.out.println("The ceiling of " + a + " is " + Math.ceil(a));

        System.out.println("The ceiling of " + b + " is " + Math.ceil(b));

        System.out.println("The ceiling of " + x + " is " + Math.ceil(x));

        System.out.println("The ceiling of " + y + " is " + Math.ceil(y));

     

        // Math.floor() returns a number down to the nearest integer

        // it can round off a floating point number

        System.out.println("The floor of " + a + " is " + Math.floor(a));

        System.out.println("The floor of " + b + " is " + Math.floor(b));

        System.out.println("The floor of " + x + " is " + Math.floor(x));

        System.out.println("The floor of " + y + " is " + Math.floor(y));

       

        // Math.rint() will returns the truncated value of the given number

        // that is, the integer part of the value by removing fractional part

        rint = 7.26;

        System.out.println("The rint of " + rint + " is " + Math.rint(rint));

        rint = 7.84;

        System.out.println("The rint of " + rint + " is " + Math.rint(rint));

       

        // Comparison operators

        // min() returns the smaller of the two numbers.

        // The value return's depending upon the number used as the

        // arguments to the method

        System.out.println("min(" + a + "," + b + ") is " + Math.min(a,b));

        System.out.println("min(" + x + "," + y + ") is " + Math.min(x,y));

        System.out.println("min(" + a + "," + b + ") is " + Math.min(a,b));

        System.out.println("min(" + y + "," + b + ") is " + Math.min(y,b));

        // There's a corresponding max() method

        // that returns the larger of two numbers

        // The value return's depending upon the number used as the

        // arguments to the method

        System.out.println("max(" + a + "," + b + ") is " + Math.max(a,b));

        System.out.println("max(" + x + "," + y + ") is " + Math.max(x,y));

        System.out.println("max(" + a + "," + x + ") is " + Math.max(a,x));

        System.out.println("max(" + a + "," + b + ") is " + Math.max(a,b));

 

        // The Math library Math.PI and Math.E defines

        // constant value

        System.out.println("Pi value is " + Math.PI);

        System.out.println("e value is " + Math.E);

       

        // Trigonometric methods in Java

        // The arguments / parameters are given in radians

        // radiant = (22/(7*180))*Degree

        // Convert a 30 degree angle to radians

        double angle = 30.0 * 2.0 * Math.PI/360.0;

        System.out.println("cos(" + angle + ") is " + Math.cos(angle));

        System.out.println("sin(" + angle + ") is " + Math.sin(angle));

 

        // Inverse Trigonometric methods

        // All values are returned as radians

 

        double radians = 1.5d;

        double sine = Math.sin(radians);

        double cosine = Math.cos(radians);

        double tan = Math.tan(radians);

 

        double asine = Math.asin(sine);

        double acosine = Math.acos(cosine);

        double atan = Math.atan(tan);

 

        System.out.println("The value Sine of       " + radians + " = " + sine);

        System.out.println("The value Cosine of     " + radians + " = " + cosine);

        System.out.println("The value Tangent of    " + radians + " = " + tan);

        System.out.println("The value Arcsine of    " + sine + " = " + asine);

        System.out.println("The value Arccosine of  " + cosine + " = " + acosine);

        System.out.println("The value Arctangent of " + tan + " = " + atan);

 

        // Exponential and Logarithmic Methods in java

 

        // Math.exp(2.0) returns e (7.38905609893065) raised

        // to the power of a.

        System.out.println("exp(2.0) is " + Math.exp(2.0));

        System.out.println("exp(10.0) is " + Math.exp(10.0));

        System.out.println("exp(0.0) is " + Math.exp(0.0));

       

        // Math.log(a) returns the natural

        // logarithm (base e) of a.

        System.out.println("log(2.0) is " + Math.log(1.0));

        System.out.println("log(10.0) is " + Math.log(10.0));

        System.out.println("log(Math.E) is " + Math.log(Math.E));

       

        // math.pow() method in java

       // Math.pow(x, y) returns the x raised

        // to the yth power.

        System.out.println("pow(10, 3) is " + Math.pow(10,3));

        System.out.println("pow(5.0, 4.5) is " + Math.pow(5.0,4.5));

        System.out.println("pow(10, -1) is " + Math.pow(10,-1));

       

        // Math.sqrt(num) returns the square root of num

        num = 25;

        System.out.println("The square root of " +  num + " is " + Math.sqrt(num));

       

        // Math.cbrt() return the cube root of number

        num = 125;

        System.out.println("The cube root of " +  num + " is " + Math.cbrt(num));

      

        // Random method Math.random() in java

        // will return pseudo-random number between 0.0 and 1.0;

         System.out.println("Random number genetated  : " + Math.random());

        System.out.println("Here's 5 random number is: " + Math.random()*5);

    }

}

Output

 

Math Function in Java

=====================

-5.0 Absolute value is 72.5

72.5 is approximately 73

79.7 is approximately 80

The ceiling of 5.2 is 6.0

The ceiling of 5.9 is 6.0

The ceiling of 72.5 is 73.0

The ceiling of 79.7 is 80.0

The floor of 5.2 is 5.0

The floor of 5.9 is 5.0

The floor of 72.5 is 72.0

The floor of 79.7 is 79.0

The rint of 7.26 is 7.0

The rint of 7.84 is 8.0

min(5.2,5.9) is 5.2

min(72.5,79.7) is 72.5

min(5.2,5.9) is 5.2

min(79.7,5.9) is 5.9

max(5.2,5.9) is 5.9

max(72.5,79.7) is 79.7

max(5.2,72.5) is 72.5

max(5.2,5.9) is 5.9

Pi value is 3.141592653589793

e value is 2.718281828459045

cos(0.5235987755982988) is 0.8660254037844387

sin(0.5235987755982988) is 0.49999999999999994

The value Sine of       1.5 = 0.9974949866040544

The value Cosine of     1.5 = 0.0707372016677029

The value Tangent of    1.5 = 14.101419947171719

The value Arcsine of    0.9974949866040544 = 1.5000000000000002

The value Arccosine of  0.0707372016677029 = 1.5

The value Arctangent of 14.101419947171719 = 1.5

exp(2.0) is 7.38905609893065

exp(10.0) is 22026.465794806718

exp(0.0) is 1.0

log(2.0) is 0.0

log(10.0) is 2.302585092994046

log(Math.E) is 1.0

pow(10, 3) is 1000.0

pow(5.0, 4.5) is 1397.5424859373686

pow(10, -1) is 0.1

The square root of 25.0 is 5.0

The cube root of 125.0 is 5.0

Random number genetated  : 0.2738161073495178

Here's 5 random number is: 2.1900603338270512


More Java program


SHARE THIS
Previous Post
Next Post