diff --git a/src/math/abs.rs b/src/math/abs.rs new file mode 100644 index 00000000000..c3d88dbc023 --- /dev/null +++ b/src/math/abs.rs @@ -0,0 +1,30 @@ +/// This function returns the absolute value of a number. +/// The absolute value of a number is the non-negative value of the number, regardless of its sign +/// Wikipedia: https://en.wikipedia.org/wiki/Absolute_value +pub fn abs(num: f64) -> f64 { + if num < 0.0 { + return -num; + } + + num +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn negative_number() { + assert_eq!(420.0, abs(-420.0)); + } + + #[test] + fn zero() { + assert_eq!(0.0, abs(0.0)); + } + + #[test] + fn positive_number() { + assert_eq!(69.69, abs(69.69)); + } +} diff --git a/src/math/mod.rs b/src/math/mod.rs index 59737cb7e37..074415902d7 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -1,3 +1,4 @@ +mod abs; mod aliquot_sum; mod amicable_numbers; mod armstrong_number; @@ -39,6 +40,7 @@ mod square_root; mod trial_division; mod zellers_congruence_algorithm; +pub use self::abs::abs; pub use self::aliquot_sum::aliquot_sum; pub use self::amicable_numbers::amicable_pairs_under_n; pub use self::armstrong_number::is_armstrong_number;