Skip to content

[pull] master from TheAlgorithms:master #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ These are for demonstration purposes only.

- [x] [Amicable numbers below N](./src/math/amicable_numbers.rs)
- [x] [Baby-Step Giant-Step Algorithm](./src/math/baby_step_giant_step.rs)
- [x] [Ceil](./src/math/ceil.rs)
- [x] [Chinese Remainder Theorem](./src/math/chinese_remainder_theorem.rs)
- [x] [Extended euclidean algorithm](./src/math/extended_euclidean_algorithm.rs)
- [x] [Fast Inverse Square Root 'Quake' Algorithm](./src/math/square_root.rs)
Expand Down
46 changes: 46 additions & 0 deletions src/math/ceil.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// In mathematics and computer science, the ceiling function maps x to the least integer greater than or equal to x
// Source: https://en.wikipedia.org/wiki/Floor_and_ceiling_functions

pub fn ceil(x: f64) -> f64 {
let x_round = x.round();
if (x_round * 10.0).round() < (x * 10.0).round() {
x_round + 1.0
} else {
x_round
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn positive_decimal() {
let num = 1.10;
assert_eq!(ceil(num), num.ceil());
}

#[test]
fn positive_integer() {
let num = 1.00;
assert_eq!(ceil(num), num.ceil());
}

#[test]
fn negative_decimal() {
let num = -1.10;
assert_eq!(ceil(num), num.ceil());
}

#[test]
fn negative_integer() {
let num = -1.00;
assert_eq!(ceil(num), num.ceil());
}

#[test]
fn zero() {
let num = 0.00;
assert_eq!(ceil(num), num.ceil());
}
}
2 changes: 2 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod amicable_numbers;
mod armstrong_number;
mod baby_step_giant_step;
mod ceil;
mod chinese_remainder_theorem;
mod collatz_sequence;
mod doomsday;
Expand Down Expand Up @@ -37,6 +38,7 @@ mod zellers_congruence_algorithm;
pub use self::amicable_numbers::amicable_pairs_under_n;
pub use self::armstrong_number::is_armstrong_number;
pub use self::baby_step_giant_step::baby_step_giant_step;
pub use self::ceil::ceil;
pub use self::chinese_remainder_theorem::chinese_remainder_theorem;
pub use self::collatz_sequence::sequence;
pub use self::doomsday::get_week_day;
Expand Down