Java provides a built-in Math
class with various methods to perform mathematical operations. These methods are static, meaning you can use them directly without creating an object.
Basic Operations
Function | Description | Example | Output |
---|---|---|---|
Math.abs(x) | Returns absolute value | Math.abs(-10) | 10 |
Math.max(a, b) | Returns the larger of two numbers | Math.max(5, 10) | 10 |
Math.min(a, b) | Returns the smaller of two numbers | Math.min(5, 10) | 5 |
Exponential & Power Functions
Function | Description | Example | Output |
---|---|---|---|
Math.sqrt(x) | Returns square root | Math.sqrt(25) | 5.0 |
Math.cbrt(x) | Returns cube root | Math.cbrt(27) | 3.0 |
Math.pow(x, y) | Returns x raised to y | Math.pow(2, 3) | 8.0 |
Math.exp(x) | Returns e^x | Math.exp(1) | 2.718… |
Math.log(x) | Returns natural logarithm (base e) | Math.log(10) | 2.302… |
Math.log10(x) | Returns logarithm (base 10) | Math.log10(100) | 2.0 |
Rounding Functions
Function | Description | Example | Output |
---|---|---|---|
Math.round(x) | Rounds to nearest integer | Math.round(4.6) | 5 |
Math.floor(x) | Rounds down | Math.floor(4.9) | 4.0 |
Math.ceil(x) | Rounds up | Math.ceil(4.1) | 5.0 |
Trigonometric Functions
Function | Description | Example | Output |
---|---|---|---|
Math.sin(x) | Returns sine of x (in radians) | Math.sin(Math.PI/2) | 1.0 |
Math.cos(x) | Returns cosine of x (in radians) | Math.cos(0) | 1.0 |
Math.tan(x) | Returns tangent of x (in radians) | Math.tan(Math.PI/4) | 1.0 |
Hyperbolic Functions
Function | Description | Example | Output |
---|---|---|---|
Math.sinh(x) | Returns hyperbolic sine | Math.sinh(1.0) | 1.175… |
Math.cosh(x) | Returns hyperbolic cosine | Math.cosh(1.0) | 1.543… |
Math.tanh(x) | Returns hyperbolic tangent | Math.tanh(1.0) | 0.761… |
Random & Miscellaneous
Function | Description | Example | Output |
---|---|---|---|
Math.random() | Generates a random number (0.0 to 1.0) | Math.random() | 0.4356… |
Math.hypot(x, y) | Returns sqrt(x² + y²) (hypotenuse) | Math.hypot(3, 4) | 5.0 |
Math.toRadians(deg) | Converts degrees to radians | Math.toRadians(180) | 3.1415… |
Math.toDegrees(rad) | Converts radians to degrees | Math.toDegrees(Math.PI) | 180.0 |
Math.signum(x) | Returns 1.0, -1.0, or 0.0 (sign of x) | Math.signum(-5) | -1.0 |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
public class MathFunctions { public static void main(String[] args) { System.out.println("Absolute: " + Math.abs(-7)); // Output: 7 System.out.println("Square Root: " + Math.sqrt(16)); // Output: 4.0 System.out.println("Power: " + Math.pow(2, 4)); // Output: 16.0 System.out.println("Log (Base e): " + Math.log(10)); // Output: 2.302585092994046 System.out.println("Max: " + Math.max(10, 20)); // Output: 20 System.out.println("Min: " + Math.min(10, 20)); // Output: 10 System.out.println("Rounded: " + Math.round(5.7)); // Output: 6 System.out.println("Floor: " + Math.floor(5.9)); // Output: 5.0 System.out.println("Ceil: " + Math.ceil(5.1)); // Output: 6.0 System.out.println("Sin(90°): " + Math.sin(Math.toRadians(90))); // Output: 1.0 System.out.println("Cos(0°): " + Math.cos(Math.toRadians(0))); // Output: 1.0 System.out.println("Tan(45°): " + Math.tan(Math.toRadians(45))); // Output: 1.0 System.out.println("Random (0-100): " + (int)(Math.random() * 100)); // Output: (Random number between 0 and 99) } } |