Skip to content

Commit 96ff7e7

Browse files
f-frmbrubeck
authored andcommitted
Implement Zero and One for NotNaN
1 parent fa32827 commit 96ff7e7

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::fmt;
1515
use std::io;
1616
use std::mem;
1717
use unreachable::unreachable;
18-
use num_traits::{Bounded, Float};
18+
use num_traits::{Bounded, Float, One, Zero};
1919

2020
/// A wrapper around Floats providing an implementation of Ord and Hash.
2121
///
@@ -569,6 +569,16 @@ fn raw_double_bits<F: Float>(f: &F) -> u64 {
569569
(man & MAN_MASK) | ((exp_u64 << 52) & EXP_MASK) | ((sign_u64 << 63) & SIGN_MASK)
570570
}
571571

572+
impl<T: Float + Zero> Zero for NotNaN<T> {
573+
fn zero() -> Self { NotNaN(T::zero()) }
574+
575+
fn is_zero(&self) -> bool { self.0.is_zero() }
576+
}
577+
578+
impl<T: Float + One> One for NotNaN<T> {
579+
fn one() -> Self { NotNaN(T::one()) }
580+
}
581+
572582
impl<T: Float + Bounded> Bounded for NotNaN<T> {
573583
fn min_value() -> Self {
574584
NotNaN(Bounded::min_value())

tests/test.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate ordered_float;
55
extern crate num_traits;
66

77
pub use ordered_float::*;
8-
pub use num_traits::{Bounded, Float};
8+
pub use num_traits::{Bounded, Float, One, Zero};
99
pub use std::cmp::Ordering::*;
1010
pub use std::{f32, f64, panic};
1111

@@ -105,6 +105,15 @@ describe! not_nan32 {
105105
assert!(panic::catch_unwind(|| {let mut tmp = NotNan::from(0.0f32); tmp %= f32::NAN;}).is_err());
106106
}
107107

108+
it "should implement Zero" {
109+
assert_eq!(NotNaN::<f32>::zero(), NotNaN::from(0.0f32));
110+
assert!(NotNaN::<f32>::zero().is_zero());
111+
}
112+
113+
it "should implement One" {
114+
assert_eq!(NotNaN::<f32>::one(), NotNaN::from(1.0f32))
115+
}
116+
108117
it "should implement Bounded" {
109118
assert_eq!(NotNaN::<f32>::min_value(), NotNaN::from(<f32 as Bounded>::min_value()));
110119
assert_eq!(NotNaN::<f32>::max_value(), NotNaN::from(<f32 as Bounded>::max_value()));
@@ -173,6 +182,15 @@ describe! not_nan64 {
173182
assert!(panic::catch_unwind(|| {let mut tmp = NotNan::from(0.0f64); tmp %= f64::NAN;}).is_err());
174183
}
175184

185+
it "should implement Zero" {
186+
assert_eq!(NotNaN::<f64>::zero(), NotNaN::from(0.0f64));
187+
assert!(NotNaN::<f64>::zero().is_zero());
188+
}
189+
190+
it "should implement One" {
191+
assert_eq!(NotNaN::<f64>::one(), NotNaN::from(1.0f64))
192+
}
193+
176194
it "should implement Bounded" {
177195
assert_eq!(NotNaN::<f64>::min_value(), NotNaN::from(<f64 as Bounded>::min_value()));
178196
assert_eq!(NotNaN::<f64>::max_value(), NotNaN::from(<f64 as Bounded>::max_value()));

0 commit comments

Comments
 (0)