Skip to content

Commit

Permalink
feat: Add signum (TheAlgorithms#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
SpiderMath authored Nov 15, 2022
1 parent 560aa2b commit 65aa43f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions src/math/signum.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 65aa43f

Please sign in to comment.