Skip to content

Commit 6939372

Browse files
f-frmbrubeck
authored andcommitted
Implement Signed for NotNaN
1 parent 298e8da commit 6939372

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/lib.rs

Lines changed: 13 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, FromPrimitive, Num, One, ToPrimitive, Zero};
18+
use num_traits::{Bounded, Float, FromPrimitive, Num, One, Signed, ToPrimitive, Zero};
1919

2020
/// A wrapper around Floats providing an implementation of Ord and Hash.
2121
///
@@ -652,6 +652,18 @@ impl<T: Float + Num> Num for NotNaN<T> {
652652
}
653653
}
654654

655+
impl<T: Float + Signed> Signed for NotNaN<T> {
656+
fn abs(&self) -> Self { NotNaN(self.0.abs()) }
657+
658+
fn abs_sub(&self, other: &Self) -> Self {
659+
NotNaN::new(self.0.abs_sub(other.0)).expect("Subtraction resulted in NaN")
660+
}
661+
662+
fn signum(&self) -> Self { NotNaN(self.0.signum()) }
663+
fn is_positive(&self) -> bool { self.0.is_positive() }
664+
fn is_negative(&self) -> bool { self.0.is_negative() }
665+
}
666+
655667
#[cfg(feature = "serde")]
656668
mod impl_serde {
657669
extern crate serde;

tests/test.rs

Lines changed: 17 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, FromPrimitive, Num, One, ToPrimitive, Zero};
8+
pub use num_traits::{Bounded, Float, FromPrimitive, Num, One, Signed, ToPrimitive, Zero};
99
pub use std::cmp::Ordering::*;
1010
pub use std::{f32, f64, panic};
1111

@@ -160,6 +160,14 @@ describe! not_nan32 {
160160
assert_eq!(NotNaN::<f32>::from_str_radix("42.0", 10).unwrap(), NotNaN::from(42.0f32));
161161
assert!(NotNaN::<f32>::from_str_radix("NaN", 10).is_err());
162162
}
163+
164+
it "should implement Signed" {
165+
assert_eq!(NotNaN::from(42f32).abs(), NotNaN::from(42f32));
166+
assert_eq!(NotNaN::from(-42f32).abs(), NotNaN::from(42f32));
167+
168+
assert_eq!(NotNaN::from(50f32).abs_sub(&NotNaN::from(8f32)), NotNaN::from(42f32));
169+
assert_eq!(NotNaN::from(8f32).abs_sub(&NotNaN::from(50f32)), NotNaN::from(0f32));
170+
}
163171
}
164172

165173
describe! not_nan64 {
@@ -279,6 +287,14 @@ describe! not_nan64 {
279287
assert_eq!(NotNaN::<f64>::from_str_radix("42.0", 10).unwrap(), NotNaN::from(42.0f64));
280288
assert!(NotNaN::<f64>::from_str_radix("NaN", 10).is_err());
281289
}
290+
291+
it "should implement Signed" {
292+
assert_eq!(NotNaN::from(42f64).abs(), NotNaN::from(42f64));
293+
assert_eq!(NotNaN::from(-42f64).abs(), NotNaN::from(42f64));
294+
295+
assert_eq!(NotNaN::from(50f64).abs_sub(&NotNaN::from(8f64)), NotNaN::from(42f64));
296+
assert_eq!(NotNaN::from(8f64).abs_sub(&NotNaN::from(50f64)), NotNaN::from(0f64));
297+
}
282298
}
283299

284300
describe! hashing {

0 commit comments

Comments
 (0)