In java Math class is already there in java.lang.Math so we don’t need to Import it explictly.
Method | Input Parameters | Output | Description |
---|---|---|---|
Math.abs(x) |
x - int, long, float, double |
Absolute value of x |
Returns the absolute value of a number. |
Math.sqrt(x) |
x - double |
Square root of x |
Returns the square root of a number. |
Math.pow(base, exponent) |
base - double, exponent - double |
base raised to the power of exponent |
Returns the value of the first argument raised to the power of the second argument. |
Math.ceil(x) |
x - double |
Smallest double value >= x |
Returns the smallest integer that is greater than or equal to the argument. |
Math.floor(x) |
x - double |
Largest double value <= x |
Returns the largest integer that is less than or equal to the argument. |
Math.round(x) |
x - float or double |
Rounded to the nearest integer | Returns the closest long or int, as appropriate, to the argument. |
Math.max(x, y) |
x - int, long, float, double, y - int, long, float, double |
Maximum of x and y |
Returns the greater of two values. |
Math.min(x, y) |
x - int, long, float, double, y - int, long, float, double |
Minimum of x and y |
Returns the smaller of two values. |
Math.random() |
None | Random double between 0.0 and 1.0 | Returns a pseudorandom double greater than or equal to 0.0 and less than 1.0. |
Math.sin(x) |
x - double (in radians) |
Sine of x |
Returns the trigonometric sine of an angle. |
Math.cos(x) |
x - double (in radians) |
Cosine of x |
Returns the trigonometric cosine of an angle. |
Math.tan(x) |
x - double (in radians) |
Tangent of x |
Returns the trigonometric tangent of an angle. |
Math.PI |
None | π (pi) | A constant representing the mathematical constant π. |
Math.E |
None | e (Euler's number) | A constant representing the mathematical constant e. |
Remember that trigonometric functions (sin
, cos
, tan
) take angles in radians. If you have angles in degrees, you may need to convert them to radians using Math.toRadians()
.