Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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