In Java, the "java.lang.Math" class is a part of the standard library and provides a set of static methods for performing various mathematical operations. Here are some of the commonly used functions in the Math library in Java:
Name | Code | Description |
---|
Math.abs(x) | int absoluteValue = Math.abs(-5);
| Returns the absolute value of x. |
Math.addExact(int x, int y) | int result = Math.addExact(x, y);
| Adds two integers or long values, throwing an exception if the result overflows. |
Name | Code | Description |
---|
Math.pow(x, y) | double powerResult = Math.pow(2, 3);
| Returns the first number raised to power of the second number. |
Math.exp(x) | double exponentialResult = Math.exp(1);
| Returns the exponential value of x. |
Math.log(x) | double logResult = Math.log(10);
| Returns the natural log (base e) of x. |
Math.log10(x) | double log10Result = Math.log10(100);
| Returns the base 10 logarithm of x. |
Name | Code | Description |
---|
Math.sin(x) | double sine = Math.sin(Math.PI / 2);
| calculates sin of a number. |
Math.cos(x) | double cosine = Math.cos(Math.PI);
| calculates cos of a number. |
Math.tan(x) | double tangent = Math.tan(Math.PI / 4);
| calculates tan of a number. |
Name | Code | Description |
---|
Math.round(x) | double roundedValue = Math.round(3.75);
| Rounds x to the nearest integer. |
Math.ceil(x) | double ceilingValue = Math.ceil(4.2);
| Returns the smallest integer number greater than or equal to x. |
Math.floor(x) | double floorValue = Math.floor(4.7);
| Returns the largest number less than or equal to the given number. |
Name | Code | Description |
---|
Math.random() | double randomValue = Math.random();
| Returns a random number with decimal point between 0.0 (inclusive) and 1.0 (exclusive). |
Name | Code | Description |
---|
Math.min(a, b) | int min = Math.min(10, 15);
| Returns the smaller of the two given numbers a and b. |
Math.max(a, b) | int max = Math.max(30, 20);
| Returns the maximum of two numbers a and b. |
Name | Code | Description |
---|
Math.sqrt(x) | double squareRoot = Math.sqrt(25);
| Returns the square root of x. |
Math.cbrt(x) | double cubeRoot = Math.cbrt(8);
| Returns the cube root of x. |
Name | Code | Description |
---|
Math.toDegrees(x) | double degrees = Math.toDegrees(Math.PI / 2);
| Converts an angle measured in radians to degrees. |
Math.toRadians(x) | double radians = Math.toRadians(90);
| Converts an angle measured in degrees to radians. |