Skip to content

Commit

Permalink
Implement floating point sign methods.
Browse files Browse the repository at this point in the history
`abs`, `signum`, `is_sign_positive`, and `is_sign_negative`. Resolves #11.
  • Loading branch information
iliekturtles committed Jun 18, 2017
1 parent 95cbb4b commit cd2ba90
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
54 changes: 50 additions & 4 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ macro_rules! system {
D: Dimension,
U: Units<D, $V>,
{
/// Returns `true` if this value is `NaN` and `false` otherwise.
/// Returns `true` if this value is `NAN` and `false` otherwise.
#[cfg_attr(feature = "clippy", allow(wrong_self_convention))]
#[inline(always)]
pub fn is_nan(self) -> bool {
Expand All @@ -477,7 +477,7 @@ macro_rules! system {
self.value.is_infinite()
}

/// Returns `true` if this number is neither infinite nor `NaN`.
/// Returns `true` if this number is neither infinite nor `NAN`.
#[cfg_attr(feature = "clippy", allow(wrong_self_convention))]
#[inline(always)]
pub fn is_finite(self) -> bool {
Expand All @@ -488,7 +488,7 @@ macro_rules! system {
self.value.is_finite()
}

/// Returns `true` if the number is neither zero, infinite, subnormal, or `NaN`.
/// Returns `true` if the number is neither zero, infinite, subnormal, or `NAN`.
#[cfg_attr(feature = "clippy", allow(wrong_self_convention))]
#[inline(always)]
pub fn is_normal(self) -> bool {
Expand Down Expand Up @@ -543,6 +543,52 @@ macro_rules! system {
}
}

/// Computes the absolute value of `self`. Returns `NAN` if the quantity is
/// `NAN`.
#[cfg(feature = "std")]
#[inline(always)]
pub fn abs(self) -> Self {
Quantity {
dimension: $crate::stdlib::marker::PhantomData,
units: $crate::stdlib::marker::PhantomData,
value: self.value.abs(),
}
}

/// Returns a quantity that represents the sign of `self`.
///
/// * `1.0` of the base unit if the number is positive, `+0.0`, or `INFINITY`.
/// * `-1.0` of the base unit if the number is negative, `-0.0`, or
/// `NEG_INFINITY`.
/// * `NAN` if the number is `NAN`.
#[cfg(feature = "std")]
#[inline(always)]
pub fn signum(self) -> Self {
Quantity {
dimension: $crate::stdlib::marker::PhantomData,
units: $crate::stdlib::marker::PhantomData,
value: self.value.signum(),
}
}

/// Returns `true` if `self`'s sign bit is positive, including `+0.0` and
/// `INFINITY`.
#[cfg(feature = "std")]
#[cfg_attr(feature = "clippy", allow(wrong_self_convention))]
#[inline(always)]
pub fn is_sign_positive(self) -> bool {
self.value.is_sign_positive()
}
/// Returns `true` if `self`'s sign is negative, including `-0.0` and
/// `NEG_INFINITY`.
#[cfg(feature = "std")]
#[cfg_attr(feature = "clippy", allow(wrong_self_convention))]
#[inline(always)]
pub fn is_sign_negative(self) -> bool {
self.value.is_sign_negative()
}

/// Fused multiply-add. Computes `(self * a) + b` with only one rounding error.
/// This produces a more accurate result with better performance than a separate
/// multiplication operation followed by an add.
Expand Down Expand Up @@ -618,7 +664,7 @@ macro_rules! system {
}
}

/// Takes the square root of a number. Returns `NaN` if `self` is a negative
/// Takes the square root of a number. Returns `NAN` if `self` is a negative
/// number.
///
/// ```
Expand Down
32 changes: 32 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,38 @@ macro_rules! test {
}
}

quickcheck! {
#[cfg(feature = "std")]
#[allow(trivial_casts)]
fn abs(v: $V) -> bool {
v.abs() == TLength::new::<meter>(v).abs().get(meter)
}
}

quickcheck! {
#[cfg(feature = "std")]
#[allow(trivial_casts)]
fn signum(v: $V) -> bool {
v.signum() == TLength::new::<meter>(v).signum().get(meter)
}
}

quickcheck! {
#[cfg(feature = "std")]
#[allow(trivial_casts)]
fn is_sign_positive(v: $V) -> bool {
v.is_sign_positive() == TLength::new::<meter>(v).is_sign_positive()
}
}

quickcheck! {
#[cfg(feature = "std")]
#[allow(trivial_casts)]
fn is_sign_negative(v: $V) -> bool {
v.is_sign_negative() == TLength::new::<meter>(v).is_sign_negative()
}
}

quickcheck! {
#[cfg(feature = "std")]
#[allow(trivial_casts)]
Expand Down

0 comments on commit cd2ba90

Please sign in to comment.