Menu
Topics Index
...
`


Exploring java.lang >
Siva Nookala - 20 Feb 2017
Methods in Math class are used to perform basic operations such as the elementary exponential, logarithm, square root, and trigonometric functions.

List of some methods in Math class :
Method Description
T abs(T a)
This method returns the absolute value of the given data type. Here, T can be double, float, int.
double acos(double a)
This method returns the arc cosine of a value. The returned angle is in the range 0.0 through PI. Similar methods are present to find arc sine and arc tangent of a value.
double atan2(double y, double x)
This method returns the angle theta after converting the rectangular coordinates (x, y) to polar coordinates (r, theta).
double cbrt(double a)
This method returns the cube root of the given double value.
double ceil(double a)
This method rounds off the double value to a value which is greater or equal to the given value and is a mathematical integer.
double random()
This method returns a random double value, greater than or equal to 0.0 and less than 1.0
long round(double a)
This method returns the closest long to the argument.
double pow(double a, double b)
This method returns the value of the first argument raised to the power of the second argument.
double floor(double a)
This method returns the largest (closest to positive infinity) double value that is less than or equal to the argument and is equal to a mathematical integer.
double copySign(double arg, double sinarg)
Returns arg with same sign as that of sinarg.
float copySign(float arg, float sinarg)
Returns arg with same sign as that of sinarg.
double scalb(double arg, int factor)
Returns arg * 2 ^ factor
double expm1(double arg)
Returns e to the arg - 1.
NOTE : All the above methods are static.

Here is an example program :
Math class demo
import java.lang.Math;                    

class MathDemo
{
    public static void main(String arg[])
    {
        int num = 9;
        if (Math.sqrt(num) * Math.sqrt(num) == num) // LINE A
            System.out.println(num + " is a perfect square");
        else
            System.out.println(num + " is not a perfect square");
        num = -2;
        System.out.println("Value of num : " + Math.abs(num)); // LINE B
        System.out.println("value when Math.ceil is used : " + Math.ceil(Math.PI)); // LINE C
        System.out.println("Value when Math.floor is used : " + Math.floor(Math.PI)); // LINE D
        num = (int) (Math.random() * 10); // LINE E
        System.out.println("Random number between 0 and 10 : " + num);    
    }
}
OUTPUT

9 is a perfect square
Value of num : 2
value when used Math.ceil : 4.0
Value when used Math.floor : 3.0
Random number between 0 and 10 : 5

DESCRIPTION

At LINE A, using sqrt() method the square root of the number is found and is squared to check whether it is a perfect square or not.
At LINE B, the absolute value of a number is returned by abs() method.
At LINE C, the number is rounded off to ceil.
At LINE D, the number is rounded off to floor.
At LINE E, a random number is returned by random() method.

THINGS TO TRY
  • Find angle between two points using the Math.cos() method and check whether the angle is acute, right angled or obtuse.
  • Find the greater of two numbers, using Math.max
  • Convert an angle in radians to degrees using Math.toDegree()

Number Method Test2
class NumberMethodTest2
{
    public static void main(String arg[])
    {
        Float b = 5.6f;
        System.out.println("Converts to Positive Number : " + Math.abs(-7));
        System.out.println("Smallest integer greater than b : " + Math.ceil(b));
        System.out.println("Greatest integer less than b : " + Math.floor(b));
        System.out.println("Closest integer as double : " + Math.rint(b));
        System.out.println("Closest integer : " + Math.round(b));
        System.out.println("Minimum of two numbers : " + Math.min(12.3, 9.6));
        System.out.println("Maximum of two numbers : " + Math.max(8, 6));
        System.out.println("e power 100 : " + Math.exp(100));
        System.out.println("Log value : " + Math.log(2.7183));
        System.out.println("2 Power 5 : " + Math.pow(2, 5));
        System.out.println("Square root : " + Math.sqrt(81));    
    }
}
OUTPUT

Converts to Positive Number : 7
Smallest integer greater than b : 6.0
Greatest integer less than b : 5.0
Closest integer as double : 6.0
Closest integer : 6
Minimum of two numbers : 9.6
Maximum of two numbers : 8
e power 100 : 2.6881171418161356E43
Log value : 1.0000066849139877
2 Power 5 : 32.0
Square root : 9.0

DESCRIPTION

In this program, abs, ceil, floor, rint, round, min, max, exp, log, pow and sqrt methods are used.


Number Method Test3
class NumberMethodTest3
{
    public static void main(String arg[])
    {
        Double degree = 30.0;
            
        System.out.println("sin(30) : " + Math.sin(Math.toRadians(degree)));
        System.out.println("cos(30) : " + Math.cos(Math.toRadians(degree)));
        System.out.println("tan(30) : " + Math.tan(Math.toRadians(degree)));
        System.out.println("sec(30) : " + Math.asin(Math.toRadians(degree)));
        System.out.println("cosec(30) : " + Math.acos(Math.toRadians(degree)));
        System.out.println("cot(30) : " + Math.atan(Math.toRadians(degree)));
        System.out.println("Theta of rectangle co-ordinate : " + Math.atan2(45,30));    
    }
}
OUTPUT

sin(30) : 0.49999999999999994
cos(30) : 0.8660254037844387
tan(30) : 0.5773502691896257
sec(30) : 0.5510695830994463
cosec(30) : 1.0197267436954502
cot(30) : 0.48234790710102493
Theta of rectangle co-ordinate : 0.982793723247329

DESCRIPTION

In this program, Double value is converted into radians and then sin, cos, tan, asin, acos, atan and atan2 methods are used.

THINGS TO TRY
  • Change the degree value of 30.0 to 45.0, 60.0, 90.0 and see the output difference.

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App