Ads

In this tutorial we will read about the java.lang.Math class in Java.

java.lang.Math is a final class which is created for providing the basic operations on numeric values. This class has provided the various methods and the constant fields to use instantly on the operands. Math class is created inside the java.lang package and the classes created inside the java.lang package need not to import at the time of creating the application.

**Class Declaration**

public final class Math extends Object

**Constant Fields**

**E :**The constant value with double precision, closer than the value of "e". "e" is called the base of the natural logarithms.

**Syntax :**public static final double E

**PI :**The constant value with double precision, closer than the value of "Π". "Π" is the ratio of the circumference of a circle to its diameter.

**Syntax :**public static final double PI

**Method Details**

Method Name | Method Detail |
---|---|

static double abs(double a) |
This method is used to find the absolute value of a given value. The given
value should be in double.Syntax : public static double abs(double a) |

static float abs(float a) |
This method is used to find the absolute value of a given value. The given
value should be in float.Syntax : public static float abs(float a) |

static int abs(int a) |
This method is used to find the absolute value of a given value. The value
should be in integer.Syntax : public static int abs(int a) |

static long abs(long a) |
This method is used to find the absolute value of a given value. The value
should be in long.Syntax : public static long abs(long a) |

static double acos(double a) |
This method is used to find the arc cosine (inverse function of the cosine)
of the given value. Range of the returned angle is in 0.0 through pi. This method
returns NaN if, the argument value is NaN and/or absolute value greater than 1.Syntax : public static double acos(double a) |

static double asin(double a) |
This method is used to find the arc sine (inverse function of the sine) of
the given value. Range of the returned angel is in -pi/2 through pi/2. This
method returns NaN if, the argument value is NaN and/or absolute value greater
than 1 and returns zero with the same sign that the argument contains if, the
argument value is given to zero.Syntax : public static double asin(double a) |

static double atan(double a) |
This method is used to find the arc tangent (inverse function of the
tangent) of the given value. Range of the returned angle is in -pi/2 through
pi/2. This method returns NaN if, the argument value is NaN, zero with the same
sign that the argument contains if, the argument value is given to zero.Syntax : public static double atan(double a) |

static double atan2(double y, double x) |
This method is used to find the angle theta component of the point (r,
theta) in polar. Argument y is the ordinate coordinate and argument x is the
abscissa coordinate.Syntax : public static double atan2(double y, double x) |

static double cbrt(double a) |
This method is used to find the cube root of a given value. Value should be
in double precision. This method returns NaN if, the argument value is not a
number, zero if, the argument is zero, infinity, if the argument is infinite.Syntax : public static double cbrt(double a) |

static double ceil(double a) |
This method is used to find the value greater than or equal to the value
given in the argument. The returned value is the smallest integer value but not
less than the argument. This
method returns same value as given into the parameter if, the value is given to NaN,
infinity, zero (positive/negative), argument value is equal to the mathematical
integer. It returns negative zero if, the value is given to less than zero but
greater than -1.0Syntax : public static double ceil(double a) |

static double copySign(double magnitude, double sign) |
This method is used to find out the double value as first argument followed by the sign of
the second argument.Syntax : public static double copySign(double magnitude, double sign) |

static float copySign(float magnitude, float sign) |
This method is used to find out the float value as first argument followed
by the sign of the second argument.Syntax : public static float copySign(float magnitude, float sign) |

static double cos(double a) |
This method is used to find out the cosine of the angle as a double value.Syntax : public static double cos(double a)` |

static double cosh(double x) |
This method is used to find out the hyperbolic cosine. Value should be in
double.Syntax : public static double cosh(double x) |

static double exp(double a) |
This method is used to find out the Euler's number as e raised to the power
a. Value of a should be in double.Syntax : public static double exp(double a) |

static double expm1(double x) |
This method is used to find out the value as e^{x} -1Syntax : public static double expm1(double x) |

static double floor(double a) |
This method is used to find the value less than or equal to the value given
in the argument. The returned value is the largest value integer value but not
greater than the argument. It returns the value as argument if the argument
value is NaN, infinity, zero (positive/negative).Syntax : public static double floor(double a) |

static int getExponent(double d) |
This method is used to find out the exponent of the given argument.Syntax : public static int getExponent(double d) |

static int getExponent(float f) |
This method is used to find out the exponent of the given argument.Syntax : public static int getExponent(float f) |

static double hypot(double x, double y) |
This method is used to find out the value after calculation the given value
as sqrt(x^{2} + y^{2}).Syntax : public static double hypot(double x, double y) |

static double IEEEremainder(double f1, double f2) |
This method is used to find out the remainder on the given two arguments as
set down the rule or guide by the IEEE 754 standard.Syntax : public static double IEEEremainder(double f1, double f2) |

static double log(double a) |
This method is used to find out the natural logarithm/ logarithm of base e of the given value.Syntax : public static double log(double a) |

static double log10(double a) |
This method is used to find out the logarithm of base 10 of the given value.Syntax : public static double log10(double a) |

static double log1p(double x) |
This method is used to find out the logarithm of base e of sum of the
given value and 1.Syntax : public static double log1p(double x) |

static double max(double a, double b) |
This method is used to find out the maximum value from the given two
double value arguments.Syntax : public static double max(double a, double b) |

static float max(float a, float b) |
This method is used to find out the maximum value from the given two float
value arguments.Syntax : public static float max(float a, float b) |

static int max(int a, int b) |
This method is used to find out the maximum value from the given two integer
value arguments.Syntax : public static int max(int a, int b) |

static long max(long a, long b) |
This method is used to find out the maximum value from the given two long
value arguments.Syntax : public static long max(long a, long b) |

static double min(double a, double b) |
This method is used to find out the minimum value from the given two double
value arguments.Syntax : public static double min(double a, double b) |

static float min(float a, float b) |
This method is used to find out the minimum value from the given two float
value arguments.Syntax : public static float min(float a, float b) |

static int min(int a, int b) |
This method is used to find out the minimum value from the given two int
value arguments.Syntax : public static int min(int a, int b) |

static long min(long a, long b) |
This method is used to find out the minimum value from the given two long
value arguments.Syntax : public static long min(long a, long b) |

static double nextAfter(double start, double direction) |
This method is used to find out the floating-point adjacent number to the first argument in
the direction in which the second argument is specified. The direction argument
specifies whether the adjacent number will be increase or decrease.Syntax : public static double nextAfter(double start, double direction) |

static float nextAfter(float start, double direction) |
This method is used to find out the floating-point adjacent number to the first argument in
the direction in which the second argument is specified. The direction argument
specifies whether the adjacent number will be increase or decrease.Syntax : public static float nextAfter(float start, double direction) |

static double nextUp(double d) |
This method is used to find out the floating-point value adjacent to the
given value as an argument in a way to positive infinity.Syntax : public static double nextUp(double d) |

static float nextUp(float f) |
This method is used to find out the floating-point value adjacent to the
given value as an argument in a way to positive infinity.Syntax : public static float nextUp(float f) |

static double pow(double a, double b) |
This method is used to find out the value after calculating the first
argument raised to the power of the second argument.Syntax : public static double pow(double a, double b) |

static double random() |
This method is used to find out the double value greater than or equals to
0.0 and less than 1.0.Syntax : public static double random() |

static double rint(double a) |
This method is used to find out the closest value as double from the given
argument.Syntax : public static double rint(double a) |

static long round(double a) |
This method is used to round of the value closest to the long of double
value given as an argument.Syntax : public static long round(double a) |

static int round(float a) |
This method is used to round of the value closest to the int of float value
given as an argument.Syntax : public static int round(float a) |

static double signum(double d) |
This method is used to find out the signum function of the given argument.
This method returns zero for the argument zero, 1.0 for the argument greater
than zero, -1.0 for the argument less than zero.Syntax : public static double signum(double d) |

static float signum(float f) |
This method is used to find out the signum function of the given argument.
This method returns zero for the argument zero, 1.0 for the argument greater
than zero, -1.0 for the argument less than zero.Syntax : public static float signum(float f) |

static double sin(double a) |
This method is used to find out the sine of the angle as a double value.Syntax : public static double sin(double a)` |

static double sinh(double x) |
This method is used to find out the hyperbolic sine. Value should be in
double.Syntax : public static double sinh(double x) |

static double sqrt(double a) |
This method is used to find out the square root of a value given as an
argument which is correctly rounded off.Syntax : public static double sqrt(double a) |

static double tan(double a) |
This method is used to find out the tangent of the angle as a double value.Syntax : public static double tan(double a)` |

static double tanh(double x) |
This method is used to find out the hyperbolic tangent. Value should be in
double.Syntax : public static double tanh(double x) |

static double toDegrees(double angrad) |
This method is used to convert the angle given in radian as an argument to
degree.Syntax : public static double toDegrees(double angrad) |

static double toRadians(double angdeg) |
This method is used to convert the angle given in degree as an argument to
radian.Syntax : public static double toRadians(double angdeg) |

**Example :**

Here I am giving a simple example which will demonstrate you how to use the above mentioned function in your program. In this example I will create a Java class into which I will use some of the methods of Math class. Methods of Math class are static so they can be accessed directly by the class name e.g. Math.sqrt().

**MathClassExample.java**

public class MathClassExample { public static void main(String[] args){ //E and round() System.out.println("e = " + Math.round(Math.E*100)/100f); //PI System.out.println("pi = " + Math.round(Math.PI*100)/100f); //abs() System.out.println("Absolute number = " + Math.abs(Math.PI)); //ceil() System.out.println("Ceil Function Value = " + Math.ceil(Math.PI)); //exp() System.out.println("Exponent number powered by the argument = " + Math.exp(0)); //floor() System.out.println("Floor Function Value = " + Math.floor(Math.E)); //max() System.out.println("Maximum Number = " + Math.max(10,10.3)); //min() System.out.println("Minimum Number = " + Math.min(10,10.3)); //pow() System.out.println("Power = " + Math.pow(10,3)); //random() System.out.println("Random Number = " + Math.random()); //rint() System.out.println("Closest to the Argument = " + Math.rint(30.5)); //round() System.out.println("Round = " + Math.round(Math.E)); //sqrt() System.out.println("Square Root = " + Math.sqrt(400)); } }

**Output :**

When you will compile and execute the above example you will get the output as follows :

Advertisements

Posted on: July 12, 2013 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms. Connect Me on Google+

Comments:1

July 12, 2013

Math class in java

a=(int) (math.random()*100+50); What do you mean by this line?

Ads

- Java Tutorials
- Java Code example
- Java Programming
- Java Beginners Examples
- Applet Tutorials
- Awt Tutorials
- Java Certification
- Interview Question
- Java Servlets Tutorial
- Jsp Tutorials
- Java Swing Tutorials
- JDBC Tutorial
- EJB Tutorials
- Java Server Faces (JSF) Tutorial
- WAP Tutorial
- Struts Tutorial
- JAXB Tutorial
- Spring FrameWork Tutorial
- SOA&Web Services Tutorials
- Bioinformatics Tutorials
- MySQL Tutorials
- JAVA DOM Tutorial
- XML Tutorial
- EAI Articles
- Many Programming Tutorials Links
- Tutorials Books
**Java Script Tutorial****Ajax Tutorial****Dojo Tutorials****Programming Books****Trainings****Flex****Ant****RDF**

Discuss: Introduction To Math Class In JavaPost your Comment