## Class Math in Java

Math class contains methods for performing basic numeric operations viz mathematical operation. The Math class is in java.lang package hence we do not require to import the same package.

The Math class contains constants for the mathematical values pi and e. The class also defines methods that compute various mathematical functions, such as trigonometric and exponential functions. All of these constants and methods are static. In other words, it is not necessary to create an instance of the Math class in order to use its constants and methods. In fact, the Math class does not define any public constructors, so it cannot be instantiated.

Math.random() never produces zero or one. It always is between 0 and 1. Math class defines constants for the mathematical values of e and PI. Math class also defines several static methods for floating-point trigonometry, exponential and other operations.

### The class structure of Math is given below:

```
public final class java.lang.Math extends java.lang.Object{
//member elements
public final static double E;//E =2.7182818284590452354(double value of E)
public final static double PI;//PI=3.14159265358979323846 (double value of PI)
//methods
public static double abs(double a);//returns the absolute value of the argument
public static native double acos(double a);//returns the acos or arc cos value of the argument passed
public static native double asin(double a);//returns the asin or arc sin value of the argument passed
public static native double atan(double a);//returns the atan or arc tan value of the argument passed
public static native double atan2(double a,double b);//returns theta after converting rectangular
//co-ordinates(a,b) to polar Double (r,theta).
public static native double ceil(double a);//returns the smallest integer less than or equal to the argument.
//This is also called rounding up.
public static native double cos(double a);//returns the cos value of the argument passed
public static native double exp(double a);//returns the base of natural log(e) to the power argument
public static int round(double a);//returns the closed int or long
public static native double floor(double a);//returns the largest integer that is less than or equal to the argument.
//This is also called rounding down.
public static native double IEEEremainder(double a,double b);
public static double toDegree(double a);//returns the degree from the specified argument.
public static double toRadian(double a);//returns the radian from the specified argument.
public static native double log(double a);//returns the natural log of the argument
public static double max(double a,double b);//returns the higher of the two argument passed
public static synchronized double random();//returns a generated random number
public static native double rint(double a);//returns the truncated value of the argument.
public static native double sin(double a);//returns the sin value of the argument passed
public static native double sqrt(double a);//returns the square root of the argument
public static native double tan(double a);//returns the tan value of the argument passed
public static native double pow(double a,double b);//returns the argument1 to the power argument2 here
//a to the power b
public static double min(double a,double b);//returns the smaller of the two argument passed
public static int abs(int a);//returns the absolute value of the argument
public static int max(int a,int b);//returns the higher of the two argument passed
public static int min(int a,int b);//returns the smaller of the two argument passed
public static int round(float a);//returns the closed int or long
public static long abs(long a);//returns the absolute value of the argument
public static long max(long a,long b);//returns the higher of the two argument passed
public static long min(long a,long b);//returns the smaller of the two argument passed
public static long round(double a);
public static float abs(float a);//returns the absolute value of the argument
public static float max(float a,float b);//returns the higher of the two argument passed
public static float min(float a,float b);//returns the smaller of the two argument passed
}
```

The details of the class structure are given as follows:

`public static final double E;`

public static final double E describes the value of this constant is e, the base for natural logarithms.E = 2.7182818284590452354

`public static final double PI;`

public static final double PI describes the value for this constant is pi. PI = 3.14159265358979323846

`public static int abs(int a);`

public static int abs(int a) method returns the absolute value of its argument. If the argument is Integer.MIN_VALUE, the method actually returns Integer.MIN_VALUE because of the true absolute value of Integer.MIN_VALUE is one greater than the largest positive value that can be represented by an int.

This method returns the absolute value of its argument.

**Parameter**

**a** – An int value.

`public static long abs(long a);`

public static long abs(long a) method returns the absolute value of its argument.If the argument is Long.MIN_VALUE, the method actually returns Long.MIN_VALUE because the true absolute value of Long.MIN_VALUE is one greater than the largest positive value represented by a long.

This method returns the absolute value of its argument.

**Parameter**

**a** – A long value.

`public static float abs(float a);`

public static float abs(float a) method returns the absolute value of its argument. If the argument to this method is negative or positive zero, the method should return positive zero. If the argument is positive or negative infinity, the method returns positive infinity. If the argument is NaN, the method returns NaN.

This method returns the absolute value of its argument

**Parameter**

**a** – A float value.

`public static double abs(double a);`

public static double abs(double a) method returns the absolute value of its argument. If the argument to this method is negative or positive zero, the method should return positive zero. If the argument is positive or negative infinity, the method returns positive infinity. If the argument is NaN, the method returns NaN.

This method returns the absolute value of its argument.

**Parameter**

**a** – A double value.

`public static native double acos(double a);`

public static native double acos(double a) method returns the arc cosine of the given value. If the value is NaN or its absolute value is greater than 1.0, the method returns NaN.

This method returns the arc cosine measured in radians; the result is greater than or equal to 0.0 and less than or equal to pi.

**Parameter**

**a** – A double value greater than or equal to -1.0 and less than or equal to 1.0.

`public static native double asin(double a);`

public static native double asin(double a) method returns the arc sine of the given value. If the value is NaN or its absolute value is greater than 1.0, the method returns NaN. If the value is positive zero, the method returns positive zero. If the value is negative zero, the method returns negative zero.

This method returns the arcsine measured in radians; the result is greater than or equal to -pi/2 and less than or equal to pi/2.

**Parameter**

**a** – A double value greater than or equal to -1.0 and less than or equal to 1.0.

`public static native double atan(double a);`

public static native double atan(double a) method returns the principle value of the arc tangent of the given value.

If the value is NaN, the method returns NaN. If the value is positive zero, the method returns positive zero. If the value is negative zero, the method returns negative zero.

This method returns the arctangent measured in radians; the result is greater than or equal to -pi/2 and less than

or equal to pi/2.

**Parameter**

**a**– A double value greater than or equal to -1.0 and less than or equal to 1.0.

`public static native double atan2(double a, double b);`

public static native double atan2(double a, double b) method returns the theta component of the polar coordinate (r, theta) that corresponds to the cartesian coordinate (a,b). It computes theta as the principal value of the arc tangent of b/a, using the signs of both arguments to determine the quadrant (and sign) of the return value.

If either argument is NaN, the method returns NaN. If the first argument is positive zero and the second argument is positive, then the method returns positive zero. If the first argument is positive zero and the second argument is negative, then the method returns the double value closest to pi.

If the first argument is negative zero and the second argument is positive, the method returns negative zero. If the first argument is negative zero and the second argument is negative, the method returns the double value closest to -pi.

If the first argument is positive and finite and the second argument is positive infinity, the method returns positive zero. If the first argument is positive and finite and the second argument is negative infinity, the method returns the double value closest to pi.

If the first argument is negative and finite and the second argument is positive infinity, the method returns negative zero. If the first argument is negative and finite and the second argument is negative infinity, the method returns the double value closest to -pi.

If the first argument is positive and the second argument is positive zero or negative zero, the method returns the double value closest to pi/2. If the first argument is negative and the second argument is positive or negative zero, the method returns the double value closest to -pi/2.

If the first argument is positive infinity and the second argument is finite, the method returns the double value closest to pi/2. If the first argument is negative infinity and the second argument is finite, the method returns the double value closest to -pi/2.

If both arguments are positive infinity, the method returns the double value closest to pi/4. If the first argument is positive infinity and the second argument is negative infinity, the method returns the double value closest to 3pi/4. If the first argument is negative infinity and the second argument is positive infinity, the method returns the double value closest to -pi/4. If both arguments are negative infinity, the method returns the double value closest to -3pi/4.

This method returns the theta component of the polar coordinate (r,theta) that corresponds to the cartesian coordinate (a,b); the result is measured in radians and is greater than or equal to -pi and less than or equal to

pi.

**Parameter**

**a** – A double value.

**b** – A double value.

`public static native double ceil(double a);`

public static native double ceil(double a) method performs the ceiling operation. It returns the smallest integer that is greater than or equal to its argument.

If the argument is NaN, an infinity value, or a zero value, the method returns that same value. If the argument is less than zero but greater than -1.0, the method returns negative zero.

This method returns the smallest integer greater than or equal to the given value.

**Parameter**

**a** – A double value.

`public static native double cos(double a);`

public static native double cos(double a) method returns the cosine of the given angle measured in radians.If the angle is NaN or an infinity value, the method returns NaN.

This method returns the cosine of the given angle.

**Parameter**

**a** – A double value that’s an angle measured in radians.

`public static native double exp(double a);`

public static native double exp(double a) method returns the exponential function of a. In other words, e is raised to the value specified by the parameter a, where e is the base of the natural logarithms.

If the value is NaN, the method returns NaN. If the value is positive infinity, the method returns positive infinity. If the value is negative infinity, the method returns positive zero.

This method returns e^a

**Parameter**

**a** – A double value.

`public static native double floor(double a);`

public static native double floor(double a) method performs the floor operation. It returns the largest integer that is less than or equal to its argument.

If the argument is NaN, an infinity value, or a zero value, the method returns that same value.

This method returns the greatest integer less than or equal to the given value.

**Parameter**

**a** – A double value.

`public static native double IEEEremainder(double f1, double f2);`

public static native double IEEEremainder(double f1, double f2) method returns the remainder of f1 divided by f2 as defined by the IEEE 754 standard.

This operation involves first determining the mathematical quotient of f1/f2 rounded to the nearest integer. If the quotient is equally close to two integers, it is rounded to the even integer. The method then returns f1-(f2 x Q), where Q is the rounded quotient.

If either argument is NaN, the method returns NaN. If the first argument is positive or negative infinity and the second argument is positive or negative zero, the method also returns NaN. If the first argument is a finite value and the second argument is positive or negative infinity, the method returns its first argument.

This method returns The remainder of f1 divided by f2 as defined by the IEEE 754 standard.

**Parameter**

**f1** – A double value.

**f2** – A double value.

`public static native double log(double a);`

public static native double log(double a) method returns the natural logarithm (base e) of its argument. In particular, if the argument is positive infinity, the method returns positive infinity. If the argument is positive or negative zero, the method returns negative infinity. If the argument is less than zero, the method returns NaN. If the argument is NaN, the method returns NaN.

This method returns the natural logarithm of a.

**Parameter**

**a** – A double value that is greater than 0.0.

`public static int max(int a, int b);`

public static int max(int a, int b) method returns the greater of its two arguments. In other words, it returns the one that is closer to Integer.MAX_VALUE.

This method returns the greater of a and b.

**Parameters**

**a** – An int value.

**b** – An int value.

`public static long max(long a, long b);`

public static long max(long a, long b) method returns the greater of its two arguments. In other words, it returns the one that is closer to Long.MAX_VALUE.

This method returns the greater of a and b.

**Parameters**

**a** – A long value.

**b** – A long value.

`public static float max(float a, float b);`

public static float max(float a, float b) method

returns the greater of its two arguments. In other words, it returns the one that is closer to positive infinity.

If one argument is positive zero and the other is negative zero, the method returns positive zero. If either argument is NaN, the method returns NaN.

This method returns the greater of a and b.

**Parameter**

**a** – A float value.

**b** – A float value.

`public static double max(double a, double b);`

public static double max(double a, double b) method

returns the greater of its two arguments. In other words, it returns the one that is closer to positive infinity.

If one argument is positive zero and the other is negative zero, the method returns positive zero. If either argument is NaN, the method returns NaN.

This method returns the greater of a and b.

**Parameter**

**a** – A double value.

**b** – A double value.

`public static int min(int a, int b);`

public static int min(int a, int b) method returns the lesser of its two arguments. In other words, it returns the one that is closer to Integer.MIN_VALUE.

This method returns the lesser of a and b.

**Parameter**

**a** – An int value.

**b** – An int value.

`public static long min(long a, long b);`

public static long min(long a, long b) method returns the lesser of its two arguments. In other words, it returns the one that is closer to Long.MIN_VALUE.

This method returns the lesser of a and b.

**Parameter**

**a** – A long value.

**b** – A long value.

`public static float min(float a, float b);`

public static float min(float a, float b) method returns the lesser of its two arguments. In other words, it returns the one that is closer to negative infinity.

If one argument is positive zero and the other is negative zero, the method returns negative zero. If either argument is NaN, the method returns NaN.

This method returns the lesser of a and b.

**Parameter**

**a** – A float value.

**b** – A float value.

`public static double min(double a, double b);`

public static double min(double a, double b) method returns the lesser of its two arguments. In other words, it returns the one that is closer to negative infinity.

If one argument is positive zero and the other is negative zero, the method returns negative zero. If either argument is NaN, the method returns NaN.

This method returns the lesser of a and b.

**Parameter**

**a** – A double value.

**b** – A double value.

`public static native double pow(double a, double b);`

public static native double pow(double a, double b) method computes the value of raising a to the power of b.

If the second argument is positive or negative zero, the method returns 1.0. If the second

the argument is 1.0, the method returns its first argument.

If the second argument is NaN, the method returns NaN. If the first argument is NaN and the second argument is nonzero, the method returns NaN.

If the first argument is positive zero and the second argument is greater than zero, the method returns positive zero.

If the first argument is positive zero and the second argument is less than zero, the method returns positive infinity.

If the first argument is positive infinity and the second argument is less than zero, the method returns positive zero.

If the first argument is positive infinity and the second argument is greater than zero, the method returns positive infinity.

If the absolute value of the first argument is greater than 1 and the second argument is positive infinity, the method returns positive infinity.

If the absolute value of the first argument is greater than 1 and the second argument is negative infinity, the method returns positive zero.

If the absolute value of the first argument is less than 1 and the second argument is negative infinity, the method returns positive infinity.

If the absolute value of the first argument is less than 1 and the second argument is positive infinity, the method returns positive zero.

If the absolute value of the first argument is 1 and the second argument is positive or negative infinity, the method returns NaN.

If the first argument is negative zero and the second argument is greater than zero but not a finite odd integer, the method returns positive zero.

If the first argument is negative zero and the second argument is a positive finite odd integer, the method returns negative zero.

If the first argument is negative zero and the second argument is less than zero but not a finite odd integer, the method returns positive infinity.

If the first argument is negative zero and the second argument is a negative finite odd integer, the method returns negative infinity.

If the first argument is negative infinity and the second argument is less than zero but not a finite odd integer, the method returns positive zero.

If the first argument is negative infinity and the second argument is a negative finite odd integer, the method returns negative zero.

If the first argument is negative infinity and the second argument is greater than zero but not a finite odd integer, the method returns positive infinity.

If the first argument is negative infinity and the second argument is a positive finite odd integer, the method returns negative infinity.

If the first argument is less than zero and the second argument is a finite even integer, the method returns the result of the absolute value of the first argument raised to the power of the second argument.

If the first argument is less than zero and the second argument is a finite odd integer, the method returns the negative of the result of the absolute value of the first argument raised to the power of the second argument.

If the first argument is finite and less than zero and the second argument is finite and not an integer, the method returns NaN.

If both arguments are integer values, the method returns the first argument raised to the power of the second argument.

This method returns a^b

**Parameter**

**a** – A double value.

**b** – A double value.

`public static synchronized double random();`

public static synchronized double random() method returns a random number greater than or equal to 0.0 and less than 1.0. The implementation of this method uses the java.util.Random class. You may prefer to use the Random class directly, in order to gain more control over the distribution, type, and repeatability of the random numbers you are generating.

This method returns a random number between 0.0 and 1.0.

`public static native double rint(double a);`

public static native double rint(double a) method returns its argument rounded to the nearest integer; the result is returned as a double value. If the argument is equidistant from two integers (e.g., 1.5), the method returns the even integer.

If the argument is an infinity value, a zero value, or NaN, the method returns that same value

This method returns the value of its argument rounded to the nearest integer.

**Parameter**

**a** – A double value.

`public static int round(float a);`

public static int round(float a) method returns its float argument rounded to the nearest integral value and converted to an int. If the argument is equidistant from two integers, the method returns the greater of the two integers.

If the argument is positive infinity or any other value greater than the Integer.MAX_VALUE,

the method returns Integer.MAX_VALUE. If the argument is negative infinity or any other value less than Integer.MIN_VALUE, the method returns Integer.MIN_VALUE. If the argument is NaN, the method returns 0.

This method returns the value of its argument rounded to the nearest int.

**Parameter**

**a** – A float value.

`public static long round(double a);`

public static long round(double a) method returns its double argument rounded to the nearest integral value and converted to a long. If the argument is equidistant from two integers, the method returns the greater of the two integers.

If the argument is positive infinity or any other value greater than Long.MAX_VALUE, the

method returns Long.MAX_VALUE. If the argument is negative infinity or any other value less

than Long.MIN_VALUE, the method returns Long.MIN_VALUE. If the argument is NaN, the

method returns 0.

This method returns the value of its argument rounded to the nearest long.

**Parameter**

**a** – A double value.

`public static native double sin(double a);`

public static native double sin(double a) method returns the sine of the given angle measured in radians. If the angle is NaN or an infinity value, the method returns NaN.

If the angle is positive zero, the method returns positive zero. If the angle is negative zero, the method returns negative zero.

This method returns the sine of the given angle.

**Parameter**

**a** – A double value that’s an angle measured in radians.

`public static native double sqrt(double a);`

public static native double sqrt(double a) method returns the square root of its argument. If the argument is negative or NaN, the method returns NaN. If the argument is positive infinity, the method returns positive infinity. If the argument is positive or negative zero, the method returns that same value.

This method returns the square root of its argument.

**Parameter**

**a** – A double value.

`public static native double tan(double a);`

public static native double tan(double a) method returns the tangent of the given angle measured in radians. If the angle is NaN or an infinity value, the method returns NaN. If the angle is positive zero, the method returns positive zero. If the angle is negative zero, the method returns negative zero.

This method returns the tangent of the given angle.

**Parameter**

**a** – A double value that is an angle measured in radians.

Apart from these Math class also has inherited methods from class- Object. They are as follows:

- clone()
- finalize()
- hashCode()
- notifyAll()
- wait()
- wait(long, int)
- equals(Object)
- getClass()
- notify()
- toString()
- wait(long)

I’m not that much of a online reader to be honest but your

blogs really nice, keep it up! I’ll go ahead and bookmark your website to come back later on. Many thanks