File tree Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -53,6 +53,7 @@ These are for demonstration purposes only.
53
53
54
54
- [x] [ Amicable numbers below N] ( ./src/math/amicable_numbers.rs )
55
55
- [x] [ Baby-Step Giant-Step Algorithm] ( ./src/math/baby_step_giant_step.rs )
56
+ - [x] [ Ceil] ( ./src/math/ceil.rs )
56
57
- [x] [ Chinese Remainder Theorem] ( ./src/math/chinese_remainder_theorem.rs )
57
58
- [x] [ Extended euclidean algorithm] ( ./src/math/extended_euclidean_algorithm.rs )
58
59
- [x] [ Fast Inverse Square Root 'Quake' Algorithm] ( ./src/math/square_root.rs )
Original file line number Diff line number Diff line change
1
+ // In mathematics and computer science, the ceiling function maps x to the least integer greater than or equal to x
2
+ // Source: https://en.wikipedia.org/wiki/Floor_and_ceiling_functions
3
+
4
+ pub fn ceil ( x : f64 ) -> f64 {
5
+ let x_round = x. round ( ) ;
6
+ if ( x_round * 10.0 ) . round ( ) < ( x * 10.0 ) . round ( ) {
7
+ x_round + 1.0
8
+ } else {
9
+ x_round
10
+ }
11
+ }
12
+
13
+ #[ cfg( test) ]
14
+ mod tests {
15
+ use super :: * ;
16
+
17
+ #[ test]
18
+ fn positive_decimal ( ) {
19
+ let num = 1.10 ;
20
+ assert_eq ! ( ceil( num) , num. ceil( ) ) ;
21
+ }
22
+
23
+ #[ test]
24
+ fn positive_integer ( ) {
25
+ let num = 1.00 ;
26
+ assert_eq ! ( ceil( num) , num. ceil( ) ) ;
27
+ }
28
+
29
+ #[ test]
30
+ fn negative_decimal ( ) {
31
+ let num = -1.10 ;
32
+ assert_eq ! ( ceil( num) , num. ceil( ) ) ;
33
+ }
34
+
35
+ #[ test]
36
+ fn negative_integer ( ) {
37
+ let num = -1.00 ;
38
+ assert_eq ! ( ceil( num) , num. ceil( ) ) ;
39
+ }
40
+
41
+ #[ test]
42
+ fn zero ( ) {
43
+ let num = 0.00 ;
44
+ assert_eq ! ( ceil( num) , num. ceil( ) ) ;
45
+ }
46
+ }
Original file line number Diff line number Diff line change 1
1
mod amicable_numbers;
2
2
mod armstrong_number;
3
3
mod baby_step_giant_step;
4
+ mod ceil;
4
5
mod chinese_remainder_theorem;
5
6
mod collatz_sequence;
6
7
mod doomsday;
@@ -37,6 +38,7 @@ mod zellers_congruence_algorithm;
37
38
pub use self :: amicable_numbers:: amicable_pairs_under_n;
38
39
pub use self :: armstrong_number:: is_armstrong_number;
39
40
pub use self :: baby_step_giant_step:: baby_step_giant_step;
41
+ pub use self :: ceil:: ceil;
40
42
pub use self :: chinese_remainder_theorem:: chinese_remainder_theorem;
41
43
pub use self :: collatz_sequence:: sequence;
42
44
pub use self :: doomsday:: get_week_day;
You can’t perform that action at this time.
0 commit comments