forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloor.java
More file actions
33 lines (29 loc) · 936 Bytes
/
Floor.java
File metadata and controls
33 lines (29 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.thealgorithms.maths;
/**
* Utility class to compute the floor of a given number.
*/
public final class Floor {
private Floor() {
}
/**
* Returns the largest double value that is less than or equal to the input.
* Equivalent to mathematical ⌊x⌋ (floor function).
*
* @param number the number to floor
* @return the largest double less than or equal to {@code number}
*/
public static double floor(double number) {
if (Double.isNaN(number) || Double.isInfinite(number) || number == 0.0 || number < Integer.MIN_VALUE || number > Integer.MAX_VALUE) {
return number;
}
if (number > 0.0 && number < 1.0) {
return 0.0;
}
long intPart = (long) number;
if (number < 0 && number != intPart) {
return intPart - 1.0;
} else {
return intPart;
}
}
}