Java Math Class: Comprehensive Guide to abs(), round(), ceil(), floor(), min(), and More – With Practical Examples
Java’s Math class is essential for developers tackling complex calculations—from physics simulations to geographic computations. This guide covers the core methods—abs(), round(), ceil(), floor(), min(), max()—and demonstrates each with practical code examples.
Key constants available in java.lang.Math:
- Math.E – 2.718281828459045 (base of natural logarithm)
- Math.PI – 3.141592653589793 (ratio of circumference to diameter)
| Method | Description | Arguments |
|---|---|---|
| abs | Returns the absolute value of the argument | double, float, int, long |
| round | Rounds to the nearest int or long | double or float |
| ceil | Smallest integer ≥ value | double |
| floor | Largest integer ≤ value | double |
| min | Smallest of two arguments | double, float, int, long |
| max | Largest of two arguments | double, float, int, long |
Below is a concise code snippet illustrating these methods (no explicit import needed; java.lang.Math is automatically available):
int i1 = 27;
int i2 = -45;
double d1 = 84.6;
double d2 = 0.45;
// abs()
System.out.println("abs(i1) = " + Math.abs(i1));
System.out.println("abs(i2) = " + Math.abs(i2));
System.out.println("abs(d1) = " + Math.abs(d1));
System.out.println("abs(d2) = " + Math.abs(d2));
// round()
System.out.println("round(d1) = " + Math.round(d1));
System.out.println("round(d2) = " + Math.round(d2));
// ceil() & floor()
System.out.println("ceil(d1) = " + Math.ceil(d1));
System.out.println("floor(d1) = " + Math.floor(d1));
System.out.println("ceil(d2) = " + Math.ceil(d2));
System.out.println("floor(d2) = " + Math.floor(d2));
// min() & max()
System.out.println("min(i1, i2) = " + Math.min(i1, i2));
System.out.println("max(i1, i2) = " + Math.max(i1, i2));
System.out.println("min(d1, d2) = " + Math.min(d1, d2));
System.out.println("max(d1, d2) = " + Math.max(d1, d2));
Example output:
abs(i1) = 27 abs(i2) = 45 abs(d1) = 84.6 abs(d2) = 0.45 round(d1) = 85 round(d2) = 0 ceil(d1) = 85.0 floor(d1) = 84.0 ceil(d2) = 1.0 floor(d2) = 0.0 min(i1, i2) = -45 max(i1, i2) = 27 min(d1, d2) = 0.45 max(d1, d2) = 84.6
Beyond basic arithmetic, java.lang.Math offers powerful scientific functions:
| Method | Description | Arguments |
|---|---|---|
| exp | e raised to the input value | double |
| log | Natural logarithm | double |
| pow | Base raised to exponent | double, double |
| sqrt | Square root | double |
double d1 = 84.6;
double d2 = 0.45;
System.out.println("exp(d2) = " + Math.exp(d2));
System.out.println("log(d2) = " + Math.log(d2));
System.out.println("pow(5, 3) = " + Math.pow(5.0, 3.0));
System.out.println("sqrt(16) = " + Math.sqrt(16));
Output:
exp(d2) = 1.568312185490169 log(d2) = -0.7985076962177716 pow(5, 3) = 125.0 sqrt(16) = 4.0
And the trigonometric toolbox:
| Method | Description | Arguments |
|---|---|---|
| sin | Sine (input in radians) | double |
| cos | Cosine (radians) | double |
| tan | Tangent (radians) | double |
| atan2 | Arctangent of y/x (returns radians) | double, double |
| toDegrees | Radians to degrees | double |
| toRadians | Degrees to radians | double |
double angleDeg = 30.0;
double angleRad = Math.toRadians(angleDeg);
System.out.println("sin(30°) = " + Math.sin(angleRad));
System.out.println("cos(30°) = " + Math.cos(angleRad));
System.out.println("tan(30°) = " + Math.tan(angleRad));
System.out.println("atan2(4, 2) = " + Math.atan2(4, 2));
Output:
sin(30°) = 0.49999999999999994 cos(30°) = 0.8660254037844387 tan(30°) = 0.5773502691896257 atan2(4, 2) = 1.1071487177940904
With these building blocks, you can create robust scientific calculators, perform engineering calculations, or process geographic coordinates with confidence.
Java
- Encapsulation in Java: A Comprehensive Guide with Practical Example
- Master Java String Manipulation: Essential Functions, Methods, and Practical Examples
- Understanding Java String.charAt(): Syntax, Return Type, Exceptions, and a Practical Example
- Mastering Java’s String.endsWith(): How to Check String Suffixes with Examples
- Java HashMap: A Comprehensive Guide
- Command‑Line Arguments in Java: A Practical Guide with Code Examples
- Java Abstraction: Mastering Abstract Classes, Methods, and Practical Examples
- Understanding Java's throws Keyword: Examples & Best Practices
- Java Switch‑Case Statement Explained: Syntax, Examples, and Best Practices
- Java Math Class: Comprehensive Guide to abs(), round(), ceil(), floor(), min(), and More – With Practical Examples