Math
suggest changeThe java.lang.Math class contains static methods and constants for performing basic numeric operations, including trigonometry, exponentiation, logarithms, roots, and overflow-safe arithmetic. All methods are available automatically without importing.
Constants
Math defines two fundamental mathematical constants as double-precision values:
System.out.println("PI: " + Math.PI); // 3.141592653589793
System.out.println("E: " + Math.E); // 2.718281828459045
Basic Arithmetic and Rounding
// Absolute value, minimum, and maximum
int absVal = Math.abs(-42); // 42
int maxVal = Math.max(10, 20); // 20
int minVal = Math.min(10, 20); // 10
// Rounding functions
double floorVal = Math.floor(5.8); // 5.0 (largest integer <= x)
double ceilVal = Math.ceil(5.2); // 6.0 (smallest integer >= x)
long rounded = Math.round(5.5); // 6 (standard half-up rounding)
double rintVal = Math.rint(5.5); // 6.0 (rounds to nearest even integer)
Powers, Roots, and Logarithms
// Exponentiation and roots
double power = Math.pow(2.0, 8.0); // 256.0
double sqrt = Math.sqrt(49.0); // 7.0
double cbrt = Math.cbrt(27.0); // 3.0
// Euclidean distance: sqrt(x^2 + y^2) without intermediate overflow
double hypot = Math.hypot(3.0, 4.0); // 5.0
// Logarithms
double naturalLog = Math.log(Math.E); // 1.0 (base e)
double log10Val = Math.log10(100.0); // 2.0 (base 10)
Trigonometry
Trigonometric functions accept angles measured in radians. Use Math.toRadians() and Math.toDegrees() to convert:
double degrees = 45.0;
double radians = Math.toRadians(degrees);
double sinVal = Math.sin(radians);
double cosVal = Math.cos(radians);
double tanVal = Math.tan(radians);
// Computes arc tangent of y / x considering signs of both arguments
double angle = Math.toDegrees(Math.atan2(1.0, 1.0)); // 45.0 degrees
Exact Arithmetic (Preventing Silent Overflow)
Standard primitive integer operations wrap around silently when they exceed maximum values. Java provides *Exact methods that explicitly throw an ArithmeticException upon overflow:
int max = Integer.MAX_VALUE; // 2147483647
// Standard addition wraps to negative without warning:
int wrapped = max + 1; // -2147483648
try {
// Throws ArithmeticException: integer overflow
int safe = Math.addExact(max, 1);
} catch (ArithmeticException e) {
System.err.println("Overflow detected: " + e.getMessage());
}
// Available exact operations:
// Math.subtractExact(a, b)
// Math.multiplyExact(a, b)
// Math.incrementExact(a)
// Math.decrementExact(a)
// Math.negateExact(a)
// Math.toIntExact(longValue) // Throws if long does not fit in int
Floor Division and Modulo
The standard % operator in Java is a remainder, not a true mathematical modulo, and returns negative values for negative dividends. Use Math.floorDiv and Math.floorMod:
// Standard remainder
int r1 = -4 % 3; // -1
// Floor modulo (matches mathematical modular arithmetic)
int r2 = Math.floorMod(-4, 3); // 2
// Floor division
int q1 = -4 / 3; // -1
int q2 = Math.floorDiv(-4, 3); // -2
Clamping Values (Java 21+)
Java 21 introduced Math.clamp() to restrict a number within a specified minimum and maximum boundary:
int clamped = Math.clamp(120, 0, 100); // 100
int valid = Math.clamp(45, 0, 100); // 45
Math vs. StrictMath
For almost all applications, Math is preferred because it can utilize hardware-specific CPU instructions for higher performance. StrictMath guarantees identical bit-for-bit floating-point results across all platforms by adhering strictly to the fdlibm floating-point library algorithms.