Download java lang Math Class in Formats
QR Code for this Page
java lang Math Class

java lang Math Class

java.lang.Math Class

Math class in Java

The Math class contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions. Unlike some of the numeric methods of class StrictMath, all implementations of the equivalent functions of class Math are not defined to return the bit-for-bit same results. This relaxation permits better-performing implementations where strict reproducibility is not required.

In this tutorial you can learn how to use Math class. You need not to create instance for Math class. Because all the methods in Math class are static methods. All the static methods are called with the class name.

What is java.lang.Math in Java ?

The java.lang.Math is a class in Java . This java.lang.Math class is available in java.lang package . The java.lang.Math class contains methods for performing basic numeric operations such as the elementary exponential , logarithm , square root , and trigonometric functions .

What is the package name which contains Math class ?

The Java package that contains the Math class is java.lang.Math . This class is used to perform almost all basic numeric operations . It is defined as public final class Math .

Do I need to import Math in Java ?

Since Math class is in the java.lang package , the Math class does not need to be imported . However , in programs extensively utilizing these functions , a static import can be used . There is no need to explicitly import java.lang.Math as its imported implicitly . All its methods are static . This explanation also useful for how to import java.lang.Math ? .

What is the function of java.lang.Math in Java ?

The class java.lang.Math contains methods for performing basic numeric operations such as the elementary exponential , logarithm , square root , and trigonometric functions . To help ensure portability of Java programs , the definitions of many of the numeric functions in this package require that they produce the same results as certain published algorithms. The following are the methods or functions of Math class in Java .

What are the Math class methods in Java ?

java.lang.Math Class methods helps to perform the numeric operations like square, square root, cube, cube root, exponential and trigonometric operations . Following are the List of java.lang.Math Class Methods or functions .

Method Description
abs() java.lang.Math.abs() method returns the absolute value of any type of argument passed. This method can handle all the data types.
acos() java.lang.Math.acos() method returns the arc cosine value of the passed argument.
toRadians() java.lang.Math.toRadians(double deg) method converts argument (degree) to radians.
asin() java.lang.Math.asin() method returns the arc sine value of the method argument passed.
cbrt() java.lang.Math.cbrt() method returns the cube root of the passed argument.
floor() java.lang.Math.floor() method returns the floor value of an argument i.e. the closest integer value which is either less or equal to the the passed argument.
hypot() java.lang.Math.hypot(double p, double b) method returns hypotenuse of a right triangle on passing the traingle’s base and perpendicular as arguments.
IEEEremainder() java.lang.Math.IEEERemainder(double d1, double d2) method returns the remainder value by applying remainder operation on two arguments w.r.t IEEE 754 standard.
log() java.lang.Math.log() method returns the logarithmic value of the passed argument.
ceil() java.lang.Math.ceil(double a) method returns the smallest possible value which is either greater or equal to the argument passed. The returned value is a mathematical integer.
atan() java.lang.Math.atan() method returns returns the arc tangent of the method argument value.
copySign() java.lang.Math.copySign() method returns first floating-point argument but having the sign of second argument.
cosh() java.lang.Math.cosh() method returns the hyperbolic cosine of the argument passed.
decrementExact() java.lang.Math.decrementExact() method decrements the value of passed argument by one.
exp() java.lang.Math.exp(double arg) method returns the Euler’s number raised to the power of double argument.
incrementExact() java.lang.Math.incrementExact() method returns the argument by incrementing it’s value.
log10() java.lang.Math.log10() method returns the base10 logarithmic value of the passed argument.
pow() java.lang.Math.pow(double b, double e) method returns the value as be
signum() java.lang.Math.signum() method returns the signum value of the argument passed.
round() java.lang.Math.round() method round off the passed argument upto closest decimal places.
max() java.lang.Math.max(double v1, double v2) method returns the greater value out of the two passed argument values. This method just compares using magnitude without considering any sign.
log1p() java.lang.Math.log1p() method returns natural log of (passed argument + 1).
ulp() java.lang.Math.ulp() method returns Unit of least precision(ulp) i.e. the least distance between two floating point numbers.

java.lang.Math Class Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*  How to convert negative number to positive number
    How to get random number, log of a number
    Save with file name MathExample.java    */
  
public class MathExample
{
  public static void main(String args[])
  {
    //IF THE VALUE IS NEGITIVE IT RETURNS POSITIVE VALUE
    System.out.println("Absolute : " + Math.abs(-100.50));
    //IT INCREASE THE VALUE TO NEAREST INTEGER
    System.out.println("ceil : " + Math.ceil(100.55));
    //IT DECREASE THE VALUE TO NEAREST INTEGER
    System.out.println("floor : " + Math.floor(100.55));
    //IT RETURNS MAX NUMBER WITH IN THE GIVEN NUMBERS
    System.out.println("max : " + Math.max(100,200));
    //IT RETURNS MIN NUMBER WITH IN THE GIVEN NUMBERS
    System.out.println("min : " + Math.min(100,200));
    //IT RETURNS RANDOM NUMBER
    System.out.println("random : " + Math.random());
    //IT RETURNS WITHOUT FLOATING POINT
    System.out.println("round : " + Math.round(100.75));
    //IT RETURNS SQUARE ROOT
    System.out.println("sqrt : " + Math.sqrt(2));
    //IT RETURNS PI VALUE (22/7)
    System.out.println("PI : " + Math.PI);
    //RETURNS NATURAL LOGARITHM
    System.out.println("log : " + Math.log(10.55));
    //RETURNS BASE 10 LOGARITHM
    System.out.println("log10 : " + Math.log10(10.55));
    //RETURNS NATURAL LOGARITHM OF SUM OF THE ARGUMENT AND 1
    System.out.println("log1p : " + Math.log1p(10.55));
  }
}

Following Java Math class example you can learn how to use trigonometric functions in java.

How to get Trigonometric values using Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/*  How to get Trigonometric values using Java Example
    Save with file name MathExample2.java   */
  
public class MathExample2
{
  public static void main(String args[])
  {
    //RETURNS ARC COSINE
    System.out.println("acos : " + Math.acos(0.4));
    //RETURNS ARC SINE
    System.out.println("asin : " + Math.asin(0.4));
    //RETURNS ARC TANGENT VALUE
    System.out.println("atan : " + Math.atan(45));
    //RETURNS COSINE OF AN ANGLE
    System.out.println("cos : " + Math.cos(45));
    //RETURNS HYPERBOLIC COSINE
    System.out.println("cosh : " + Math.cosh(45));
    //RETURNS SINE OF AN ANGLE
    System.out.println("sin : " + Math.sin(45));
    //RETURNS HYPERBOLIC SINE
    System.out.println("sinh : " + Math.sinh(45));
    //RETURNS TANGENT OF AN ANGLE
    System.out.println("tan : " + Math.tan(45));
    //RETURNS HYPERBOLIC TANGENT
    System.out.println("tanh : " + Math.tanh(45));
    //RETURNS DEGREES
    System.out.println("toDegrees : " + Math.toDegrees(45));
    //RETURNS RADIANS
    System.out.println("toRadians : " + Math.toRadians(45));
  }
}
Download java lang Math Class in Formats