Java Math – ceil() Floor() Methods

โšก Smart Summary

Java Math Class (java.lang.Math) supplies static methods for absolute value, rounding, ceiling, floor, minimum, maximum, exponential, logarithmic, and trigonometric operations. These built-in functions replace manual calculation, enabling accurate scientific computation directly within any Java program.

  • ๐Ÿ”ข Constants: Math.E (2.718281828459045) and Math.PI (3.141592653589793) are exposed as double fields for logarithmic and geometric formulas.
  • ๐Ÿ“ Rounding Family: abs() returns magnitude, round() gives the nearest integer, while ceil() and floor() round up and down to whole numbers.
  • โš–๏ธ Comparison Methods: min() and max() return the smaller or larger of two numeric arguments across int, long, float, and double types.
  • ๐Ÿ“ˆ Advanced Math: exp(), log(), pow(), and sqrt() handle exponential, logarithmic, power, and square-root computations on double values.
  • ๐Ÿ“ Trigonometry: sin(), cos(), tan(), atan2(), toRadians(), and toDegrees() operate in radians for angle-based calculations.

Java Math ceil() and floor() Methods

What is the Java Math Class?

Java has had several advanced usage applications, including working with complex calculations in physics, architecture and structural design, working with maps and corresponding latitudes and longitudes, and more.

All such applications require complex calculations and equations that are tedious to perform manually. Programmatically, such calculations would involve the usage of logarithms, trigonometry, exponential equations, and similar operations.

Java Math Class

Now, you cannot have all the log or trigonometry tables hard-coded somewhere in your application or data. The data would be enormous and complex to maintain.

Java provides a very useful class for this purpose. It is the Math Java class (java.lang.Math). This class provides methods for performing operations like exponential, logarithm, roots, and trigonometric equations too.

The two most fundamental elements in mathematics are “e” (the base of the natural logarithm) and “pi” (the ratio of the circumference of a circle to its diameter). These two constants are often required in the above calculations and operations. Hence, the Math class in Java provides these two constants as double fields:

  • Math.E โ€“ having a value of 2.718281828459045
  • Math.PI โ€“ having a value of 3.141592653589793

Let us have a look at the table below that shows the Basic methods and their descriptions:

MethodDescriptionArguments
absReturns the absolute value of the argumentDouble, float, int, long
roundReturns the closest int or long (as per the argument)double or float
ceilMath ceil function in Java returns the smallest integer that is greater than or equal to the argumentDouble
floorJava floor method returns the largest integer that is less than or equal to the argumentDouble
minReturns the smallest of the two argumentsDouble, float, int, long
maxReturns the largest of the two argumentsDouble, float, int, long

Below is the code implementation of the above methods.

Note: There is no need to explicitly import java.lang.Math, as it is imported implicitly. All its methods are static.

Integer variables:

int i1 = 27;
int i2 = -45;

Double (decimal) variables:

double d1 = 84.6;
double d2 = 0.45;

Java Math abs() method with Example

The Java Math abs() method returns the absolute value of the argument.

public class Guru99 {
 public static void main(String args[]) {
  int i1 = 27;
  int i2 = -45;
  double d1 = 84.6;
  double d2 = 0.45;
  System.out.println("Absolute value of i1: " + Math.abs(i1));
  System.out.println("Absolute value of i2: " + Math.abs(i2));
  System.out.println("Absolute value of d1: " + Math.abs(d1));
  System.out.println("Absolute value of d2: " + Math.abs(d2));
 }
}

Expected Output:

Absolute value of i1: 27
Absolute value of i2: 45
Absolute value of d1: 84.6
Absolute value of d2: 0.45

Java Math.round() method with Example

The Math.round() method in Java returns the closest int or long as per the argument. Below is an example of the Math.round Java method.

public class Guru99 {
 public static void main(String args[]) {
  double d1 = 84.6;
  double d2 = 0.45;
  System.out.println("Round off for d1: " + Math.round(d1));
  System.out.println("Round off for d2: " + Math.round(d2));
 }
}

Expected Output:

Round off for d1: 85
Round off for d2: 0

Java Math.ceil() and Math.floor() method with Example

The Math.ceil() and Math.floor() methods in Java are used to return the smallest and largest integer that are greater than or equal to (and less than or equal to) the argument, respectively. Below is the Math floor and ceiling Java example.

public class Guru99 {
 public static void main(String args[]) {
  double d1 = 84.6;
  double d2 = 0.45;
  System.out.println("Ceiling of '" + d1 + "' = " + Math.ceil(d1));
  System.out.println("Floor of '" + d1 + "' = " + Math.floor(d1));
  System.out.println("Ceiling of '" + d2 + "' = " + Math.ceil(d2));
  System.out.println("Floor of '" + d2 + "' = " + Math.floor(d2));
 }
}

We will get the below output of the Math.ceil in Java example.

Expected Output:

Ceiling of '84.6' = 85.0
Floor of '84.6' = 84.0
Ceiling of '0.45' = 1.0
Floor of '0.45' = 0.0

Java Math.min() and Math.max() method with Example

The Java Math.min() method returns the smallest of the two arguments, while Math.max() returns the largest of the two arguments.

public class Guru99 {
 public static void main(String args[]) {
  int i1 = 27;
  int i2 = -45;
  double d1 = 84.6;
  double d2 = 0.45;
  System.out.println("Minimum out of '" + i1 + "' and '" + i2 + "' = " + Math.min(i1, i2));
  System.out.println("Maximum out of '" + i1 + "' and '" + i2 + "' = " + Math.max(i1, i2));
  System.out.println("Minimum out of '" + d1 + "' and '" + d2 + "' = " + Math.min(d1, d2));
  System.out.println("Maximum out of '" + d1 + "' and '" + d2 + "' = " + Math.max(d1, d2));
 }
}

Expected Output:

Minimum out of '27' and '-45' = -45
Maximum out of '27' and '-45' = 27
Minimum out of '84.6' and '0.45' = 0.45
Maximum out of '84.6' and '0.45' = 84.6

Java Math Exponential and Logarithmic Methods

Beyond the basic methods, the Java Math class also provides exponential and logarithmic operations. The table below shows these methods and their descriptions.

MethodDescriptionArguments
expReturns the base of natural log (e) to the power of the argumentDouble
logReturns the natural log of the argumentdouble
powTakes 2 arguments as input and returns the value of the first argument raised to the power of the second argumentDouble
floorJava math floor returns the largest integer that is less than or equal to the argumentDouble
sqrtReturns the square root of the argumentDouble

Below is the code implementation of the above methods (the same variables are used as above).

public class Guru99 {
 public static void main(String args[]) {
  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));
 }
}

Expected Output:

exp(0.45) = 1.568312185490169
log(0.45) = -0.7985076962177716
pow(5, 3) = 125.0
sqrt(16) = 4.0

Java Math Trigonometric Methods

The Java Math class further supports trigonometric operations. The table below shows these methods and their descriptions.

MethodDescriptionArguments
sinReturns the sine of the specified argumentDouble
cosReturns the cosine of the specified argumentdouble
tanReturns the tangent of the specified argumentDouble
atan2Converts rectangular coordinates (x, y) to polar (r, theta) and returns thetaDouble
toDegreesConverts the argument to degreesDouble
sqrtReturns the square root of the argumentDouble
toRadiansConverts the argument to radiansDouble

The default arguments are in radians. Below is the code implementation.

public class Guru99 {
 public static void main(String args[]) {
  double angle_30 = 30.0;
  double radian_30 = Math.toRadians(angle_30);
  System.out.println("sin(30) = " + Math.sin(radian_30));
  System.out.println("cos(30) = " + Math.cos(radian_30));
  System.out.println("tan(30) = " + Math.tan(radian_30));
  System.out.println("Theta = " + Math.atan2(4, 2));
 }
}

Expected Output:

sin(30) = 0.49999999999999994
cos(30) = 0.8660254037844387
tan(30) = 0.5773502691896257
Theta = 1.1071487177940904

Now, with the above methods, you can also design your own scientific calculator in Java.

FAQs

Java Math is a final utility class in java.lang that provides static methods for common numerical operations, including absolute value, rounding, powers, roots, logarithms, and trigonometry, plus the constants Math.PI and Math.E.

No. The java.lang package is imported implicitly, so Math methods are always available. For terser code, add import static java.lang.Math.*; and call sqrt or pow without the Math prefix.

Math.ceil returns the smallest integer greater than or equal to the argument, while Math.floor returns the largest integer less than or equal to the argument. Both return a double, so 4.2 becomes 5.0 or 4.0 respectively.

All trigonometric methods expect radians, not degrees. Convert first with Math.toRadians(30) before calling sin, cos, or tan. Otherwise the method treats 30 as 30 radians and returns a wildly different value.

Java has no dedicated exponent operator. The caret ^ is bitwise XOR, not exponentiation. Use Math.pow(base, exponent) for powers. For integer squares, multiplying x * x is faster than calling Math.pow.

Use Math.addExact whenever an overflow would represent a real bug, such as counters, financial totals, or index arithmetic. It throws ArithmeticException instead of wrapping silently, making incorrect results loud and easy to spot.

Machine learning libraries in Java rely on Math.exp for softmax, Math.log for cross-entropy loss, Math.sqrt for RMS metrics, and Math.tanh for activation functions. Vector math frameworks then batch these calls across arrays for training and inference.

Yes. GitHub Copilot scaffolds Math method calls, radian conversions, and overflow-safe arithmetic from a comment describing the formula. Review each suggestion for numerical edge cases such as division by zero or precision loss before shipping.

Summarize this post with: