mirror of
https://github.com/TheAlgorithms/Java.git
synced 2026-03-13 08:40:43 +08:00
* factorial using using iteration
* add ceil() to maths * add combinations() to maths * add floor() to maths
This commit is contained in:
32
Maths/Floor.java
Normal file
32
Maths/Floor.java
Normal file
@@ -0,0 +1,32 @@
|
||||
package Maths;
|
||||
|
||||
public class Floor {
|
||||
public static void main(String[] args) {
|
||||
assert floor(10) == Math.floor(10);
|
||||
assert floor(-10) == Math.floor(-10);
|
||||
assert floor(10.0) == Math.floor(10.0);
|
||||
assert floor(-10.0) == Math.floor(-10.0);
|
||||
assert floor(10.1) == Math.floor(10.1);
|
||||
assert floor(-10.1) == Math.floor(-10.1);
|
||||
assert floor(0) == Math.floor(0);
|
||||
assert floor(-0) == Math.floor(-0);
|
||||
assert floor(0.0) == Math.floor(0.0);
|
||||
assert floor(-0.0) == Math.floor(-0.0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the largest (closest to positive infinity)
|
||||
*
|
||||
* @param number the number
|
||||
* @return the largest (closest to positive infinity) of given {@code number}
|
||||
*/
|
||||
public static double floor(double number) {
|
||||
if (number - (int) number == 0) {
|
||||
return number;
|
||||
} else if (number - (int) number > 0) {
|
||||
return (int) number;
|
||||
} else {
|
||||
return (int) number - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user