Skip to content

Commit

Permalink
FixedPointNumber: zero is not positive. (paritytech#6385)
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunxw authored Jun 18, 2020
1 parent d6d688c commit f8afa52
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions primitives/arithmetic/src/fixed_point.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ pub trait FixedPointNumber:
self.into_inner() == Self::Inner::one()
}

/// Checks if the number is positive.
/// Returns `true` if `self` is positive and `false` if the number is zero or negative.
fn is_positive(self) -> bool {
self.into_inner() >= Self::Inner::zero()
self.into_inner() > Self::Inner::zero()
}

/// Checks if the number is negative.
/// Returns `true` if `self` is negative and `false` if the number is zero or positive.
fn is_negative(self) -> bool {
self.into_inner() < Self::Inner::zero()
}
Expand Down Expand Up @@ -1393,6 +1393,23 @@ macro_rules! implement_fixed {
assert_eq!(d.checked_div(&$name::zero()), None);
}

#[test]
fn is_positive_negative_works() {
let one = $name::one();
assert!(one.is_positive());
assert!(!one.is_negative());

let zero = $name::zero();
assert!(!zero.is_positive());
assert!(!zero.is_negative());

if $signed {
let minus_one = $name::saturating_from_integer(-1);
assert!(minus_one.is_negative());
assert!(!minus_one.is_positive());
}
}

#[test]
fn trunc_works() {
let n = $name::saturating_from_rational(5, 2).trunc();
Expand Down

0 comments on commit f8afa52

Please sign in to comment.