java.lang.Math Class
On this page
Definition
The class Math contains methods for performing basic numeric operations such as the elementary exponential, logarithm, square root, and trigonometric functions.
Example
| Method | Description |
|---|---|
| sin | Returns the trigonometric value of the sine of an angle. |
| cos | Returns the trigonometric value of the cosine of an angle. |
| tan | Returns the trigonometric value of the tangent of an angle. |
| asin | Returns the trigonometric value of the arc sine of an angle. |
| acos | Returns the trigonometric value of the arc cosine of an angle. |
| atan | Returns the trigonometric value of the arc tangent of an angle. |
| toRadians | Convert value in degrees to value in radians |
| toDegrees | Convert value in radians to value in degrees |
| exp | Returns Euler’s number e raised to the power of a double value |
| log | Returns the natural logarithm (base e) of a double value |
| log10 | Returns the base 10 logarithms of a double value |
| sqrt | Returns the correct rounded positive square root of a double value |
| ceil | Returns the smallest double value that is greater than or equal to the argument. It is a fixed mathematical integer |
| floor | Returns the smallest double value that is less than or equal to the argument. It is a fixed mathematical integer. |
| atan2 | Returns the angle theta from the conversion of rectangular coordinates (x, y) to polar coordinates (r, theta) |
| pow | Returns(pow(a,b)) the value of ab. |
| round | Returns the closest int to the argument, with ties rounding to positive infinity |
| random | Returns a double value with a positive sign, in the range [0.0, 1.0]. |
| abs | Return the absolute value |
| max | Returns the maximum out of the two arguments |
| min | Returns the minimum out of the two arguments |
java.lang.Math class Example
public class MathDemo
{
public static void main(String[] args)
{
System.out.println("PI Values is..."+Math.PI);
System.out.println("absolute value is..."+Math.abs(-2949));
System.out.println("max of two number is..."+Math.max(49, 4959));
System.out.println("Min of two number is..."+Math.min(456, 565));
System.out.println("Ranom number is..."+Math.round(Math.random()*10));
System.out.println("sqrt is..."+Math.sqrt(625));
}
}
Output:
PI Values is...3.141592653589793 absolute value is...2949 max of two number is...4959 Min of two number is...456 Ranom number is...8 sqrt is...25.0