What is floor function in mathematics?
A floor function which is also known as the greatest integer function in maths takes a real number “x” as an input. It returns the greatest integer that is less than or equal to input number x. It is normally denoted as floor(x) or ⌊x⌋. It is used to convert a real number having fractional part into an integer without the fractional part. To understand it better let's have a quick look at the examples below.
floor(5) = 5
floor (1.3) = 1
floor (7.9) = 7
What is Math floor() method in Java?
Java provides an equivalent of mathematical floor function. Here’s how you can understand it.Math floor() Header
public static double floor(double x)
The Math floor() method in Java takes a double value (double x) as a parameter whose floor has to be determined. It does not require to import any external package.
Return Type math.floor
The method returns a double (double floor) value which is less than or equal to the given parameter.Example
public class Driver1 {
public static void main(String[] args) {
double x = 50; // floor for whole number (Integer value)
double floorValue = Math.floor(x);
System.out.println("floor⌊" + x + "⌋ = " + floorValue);
x = 21.7; // floor for positive decimal
floorValue = Math.floor(x);
System.out.println("floor⌊" + x + "⌋ = " + floorValue);
x = -21.7; // floor for negative decimal
floorValue = Math.floor(x);
System.out.println("floor⌊" + x + "⌋ = " + floorValue);
x = 0; // floor for zero (Integer value)
floorValue = Math.floor(x);
System.out.println("floor⌊" + x + "⌋ = " + floorValue);
// Boundary Cases
x = +3.3/0; // Case I - floor for +Infinity
floorValue = Math.floor(x);
System.out.println("floor⌊" + x + "⌋ = " + floorValue);
x = -3.3/0; // Case II - floor for -infinity
floorValue = Math.floor(x);
System.out.println("floor⌊" + x + "⌋ = " + floorValue);
x = -0.0/0; // Case III - floor for NaN
floorValue = Math.floor(x);
System.out.println("floor⌊" + x + "⌋ = " + floorValue);
}
}
Output
floor⌊50.0⌋ = 50.0
floor⌊21.7⌋ = 21.0
floor⌊-21.7⌋ = -22.0
floor⌊0.0⌋ = 0.0
floor⌊Infinity⌋ = Infinity
floor⌊-Infinity⌋ = -Infinity
floor⌊NaN⌋ = NaN
GO TO FULL VERSION