Math Functions

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