Java Reference ยท java.lang
The Math class
Reference for java.lang.Math: absolute value, min and max, powers, roots, trigonometry, logarithms, clamping, floor division and edge cases with NaN.
Math is a final utility class in java.lang that groups static methods for common numeric work: absolute values, minimum and maximum, powers and roots, logarithms, trigonometry, rounding and overflow-checked arithmetic. You never create an instance; you call Math.sqrt(16) directly. It also defines two constants, Math.PI and Math.E.
Most methods work on double, so int and long arguments are widened automatically. A few (abs, min, max, floorDiv, floorMod, clamp) have overloads for int, long, and in some cases float, which keeps the result type exact. Methods that can fail mathematically do not throw: they return NaN or an infinity, following IEEE 754.
double hyp = Math.sqrt(Math.pow(3, 2) + Math.pow(4, 2)); // 5.0 int bigger = Math.max(7, 12); // 12
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
Math has no constructor. The most useful members:
| Member | Description |
|---|---|
static final double PI, E |
The constants pi and e. |
abs(x) |
Absolute value; overloads for int, long, float, double. |
max(a, b), min(a, b) |
Larger or smaller of two values; same four overloads. |
pow(double a, double b) |
a raised to the power b, as a double. |
sqrt(double), cbrt(double) |
Square root and cube root. |
hypot(double x, double y) |
sqrt(x*x + y*y) without intermediate overflow. |
exp(double), log(double), log10(double) |
Exponential, natural logarithm, base-10 logarithm. |
sin, cos, tan, asin, acos, atan, atan2(y, x) |
Trigonometry in radians. |
toRadians(double), toDegrees(double) |
Angle conversion. |
floorDiv, floorMod, ceilDiv, ceilMod |
Integer division that rounds toward negative (or positive) infinity. |
clamp(value, min, max) |
Limits a value to a range (since Java 21). |
signum(double) |
-1.0, 0.0 or 1.0 (or NaN). |
fma(a, b, c) |
a * b + c with a single rounding (since Java 9). |
Throws: most methods never throw. floorDiv and floorMod throw ArithmeticException for a zero divisor, clamp throws IllegalArgumentException when min > max, and the *Exact methods throw on overflow (see Math.addExact()).
Usage notes
- Results are doubles.
Math.pow(2, 10)is1024.0, not1024. Cast with(int)or(long)only when you know the value is exact and in range; for integer powers of small numbers a loop or1 << nis safer. abs(Integer.MIN_VALUE)is negative. There is no positiveintof that size, so the result wraps toInteger.MIN_VALUE. UseMath.absExact()(Java 15) if you want an exception instead.%versusfloorMod. The remainder operator keeps the sign of the dividend, so-7 % 3is-1.Math.floorMod(-7, 3)is2, which is what you want for wrap-around indexes and clock arithmetic.- Special values.
sqrtof a negative number andlog(0)do not throw: you getNaNand-Infinity. Check withDouble.isNaN(), never== Double.NaN, which is always false. minandmaxand NaN. If either argument isNaNthe result isNaN.Math.min(-0.0, 0.0)is-0.0.- Floating-point precision.
Math.sin(Math.PI)is not exactly zero. Compare doubles with a tolerance, and useBigDecimalfor money. - Accuracy and speed. The JVM may use hardware intrinsics, so results can differ in the last bit between platforms. Use
StrictMathwhen you need identical results everywhere. - Not for randomness.
Math.random()exists, butRandomand friends are usually a better choice. - Thread safety. All methods are pure functions and safe to call from any thread.