Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

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:

MethodDescriptionArguments
absReturns the absolute value of the argumentdouble, float, int, long
roundRounds to the nearest int or longdouble or float
ceilSmallest integer ≥ valuedouble
floorLargest integer ≤ valuedouble
minSmallest of two argumentsdouble, float, int, long
maxLargest of two argumentsdouble, 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:

MethodDescriptionArguments
expe raised to the input valuedouble
logNatural logarithmdouble
powBase raised to exponentdouble, double
sqrtSquare rootdouble
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:

MethodDescriptionArguments
sinSine (input in radians)double
cosCosine (radians)double
tanTangent (radians)double
atan2Arctangent of y/x (returns radians)double, double
toDegreesRadians to degreesdouble
toRadiansDegrees to radiansdouble
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

  1. Encapsulation in Java: A Comprehensive Guide with Practical Example
  2. Master Java String Manipulation: Essential Functions, Methods, and Practical Examples
  3. Understanding Java String.charAt(): Syntax, Return Type, Exceptions, and a Practical Example
  4. Mastering Java’s String.endsWith(): How to Check String Suffixes with Examples
  5. Java HashMap: A Comprehensive Guide
  6. Command‑Line Arguments in Java: A Practical Guide with Code Examples
  7. Java Abstraction: Mastering Abstract Classes, Methods, and Practical Examples
  8. Understanding Java's throws Keyword: Examples & Best Practices
  9. Java Switch‑Case Statement Explained: Syntax, Examples, and Best Practices
  10. Java Math Class: Comprehensive Guide to abs(), round(), ceil(), floor(), min(), and More – With Practical Examples