From 65aa43f0c8173b15b66c7fd79efb33992dc8b9e5 Mon Sep 17 00:00:00 2001 From: Defective Detective <71999854+SpiderMath@users.noreply.github.com> Date: Wed, 16 Nov 2022 01:12:20 +0530 Subject: [PATCH] feat: Add signum (#419) --- src/math/mod.rs | 2 ++ src/math/signum.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/math/signum.rs diff --git a/src/math/mod.rs b/src/math/mod.rs index ff5aa79d8cc..ad4a19dd96a 100644 --- a/src/math/mod.rs +++ b/src/math/mod.rs @@ -31,6 +31,7 @@ mod prime_numbers; mod quadratic_residue; mod random; mod sieve_of_eratosthenes; +mod signum; mod simpson_integration; mod square_root; mod trial_division; @@ -76,6 +77,7 @@ pub use self::prime_numbers::prime_numbers; pub use self::quadratic_residue::cipolla; pub use self::random::PCG32; pub use self::sieve_of_eratosthenes::sieve_of_eratosthenes; +pub use self::signum::signum; pub use self::simpson_integration::simpson_integration; pub use self::square_root::{fast_inv_sqrt, square_root}; pub use self::trial_division::trial_division; diff --git a/src/math/signum.rs b/src/math/signum.rs new file mode 100644 index 00000000000..83b7e7b5538 --- /dev/null +++ b/src/math/signum.rs @@ -0,0 +1,36 @@ +/// Signum function is a mathematical function that extracts +/// the sign of a real number. It is also known as the sign function, +/// and it is an odd piecewise function. +/// If a number is negative, i.e. it is less than zero, then sgn(x) = -1 +/// If a number is zero, then sgn(0) = 0 +/// If a number is positive, i.e. it is greater than zero, then sgn(x) = 1 + +pub fn signum(number: f64) -> i8 { + if number == 0.0 { + return 0; + } else if number > 0.0 { + return 1; + } + + -1 +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn positive_integer() { + assert_eq!(signum(15.0), 1); + } + + #[test] + fn negative_integer() { + assert_eq!(signum(-30.0), -1); + } + + #[test] + fn zero() { + assert_eq!(signum(0.0), 0); + } +}