Skip to content

Commit dcdabc8

Browse files
justinlovingermbrubeck
authored andcommitted
Add FloatConst implementations
1 parent e45839a commit dcdabc8

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

src/lib.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use num_traits::float::FloatCore as Float;
2929
#[cfg(feature = "std")]
3030
pub use num_traits::Float;
3131
use num_traits::{
32-
AsPrimitive, Bounded, FromPrimitive, Num, NumCast, One, Signed, ToPrimitive, Zero,
32+
AsPrimitive, Bounded, FloatConst, FromPrimitive, Num, NumCast, One, Signed, ToPrimitive, Zero,
3333
};
3434

3535
// masks for the parts of the IEEE 754 float
@@ -1537,6 +1537,42 @@ impl<T: Float> NumCast for NotNan<T> {
15371537
}
15381538
}
15391539

1540+
macro_rules! impl_float_const_method {
1541+
($wrapper:expr, $method:ident) => {
1542+
#[allow(non_snake_case)]
1543+
fn $method() -> Self {
1544+
$wrapper(T::$method())
1545+
}
1546+
};
1547+
}
1548+
1549+
macro_rules! impl_float_const {
1550+
($type:ident, $wrapper:expr) => {
1551+
impl<T: FloatConst> FloatConst for $type<T> {
1552+
impl_float_const_method!($wrapper, E);
1553+
impl_float_const_method!($wrapper, FRAC_1_PI);
1554+
impl_float_const_method!($wrapper, FRAC_1_SQRT_2);
1555+
impl_float_const_method!($wrapper, FRAC_2_PI);
1556+
impl_float_const_method!($wrapper, FRAC_2_SQRT_PI);
1557+
impl_float_const_method!($wrapper, FRAC_PI_2);
1558+
impl_float_const_method!($wrapper, FRAC_PI_3);
1559+
impl_float_const_method!($wrapper, FRAC_PI_4);
1560+
impl_float_const_method!($wrapper, FRAC_PI_6);
1561+
impl_float_const_method!($wrapper, FRAC_PI_8);
1562+
impl_float_const_method!($wrapper, LN_10);
1563+
impl_float_const_method!($wrapper, LN_2);
1564+
impl_float_const_method!($wrapper, LOG10_E);
1565+
impl_float_const_method!($wrapper, LOG2_E);
1566+
impl_float_const_method!($wrapper, PI);
1567+
impl_float_const_method!($wrapper, SQRT_2);
1568+
}
1569+
};
1570+
}
1571+
1572+
impl_float_const!(OrderedFloat, OrderedFloat);
1573+
// Float constants are not NaN.
1574+
impl_float_const!(NotNan, |x| unsafe { NotNan::new_unchecked(x) });
1575+
15401576
#[cfg(feature = "serde")]
15411577
mod impl_serde {
15421578
extern crate serde;

tests/test.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ extern crate ordered_float;
77
pub use num_traits::float::FloatCore as Float;
88
#[cfg(feature = "std")]
99
pub use num_traits::Float;
10-
pub use num_traits::{Bounded, FromPrimitive, Num, One, Signed, ToPrimitive, Zero};
10+
pub use num_traits::{Bounded, FloatConst, FromPrimitive, Num, One, Signed, ToPrimitive, Zero};
1111
pub use ordered_float::*;
1212

1313
pub use std::cmp::Ordering::*;
@@ -693,6 +693,41 @@ fn from_ref() {
693693
assert_eq!(f, 2.0f64);
694694
}
695695

696+
macro_rules! test_float_const_method {
697+
($type:ident < $inner:ident >, $method:ident) => {
698+
assert_eq!($type::<$inner>::$method().into_inner(), $inner::$method())
699+
};
700+
}
701+
702+
macro_rules! test_float_const_methods {
703+
($type:ident < $inner:ident >) => {
704+
test_float_const_method!($type<$inner>, E);
705+
test_float_const_method!($type<$inner>, FRAC_1_PI);
706+
test_float_const_method!($type<$inner>, FRAC_1_SQRT_2);
707+
test_float_const_method!($type<$inner>, FRAC_2_PI);
708+
test_float_const_method!($type<$inner>, FRAC_2_SQRT_PI);
709+
test_float_const_method!($type<$inner>, FRAC_PI_2);
710+
test_float_const_method!($type<$inner>, FRAC_PI_3);
711+
test_float_const_method!($type<$inner>, FRAC_PI_4);
712+
test_float_const_method!($type<$inner>, FRAC_PI_6);
713+
test_float_const_method!($type<$inner>, FRAC_PI_8);
714+
test_float_const_method!($type<$inner>, LN_10);
715+
test_float_const_method!($type<$inner>, LN_2);
716+
test_float_const_method!($type<$inner>, LOG10_E);
717+
test_float_const_method!($type<$inner>, LOG2_E);
718+
test_float_const_method!($type<$inner>, PI);
719+
test_float_const_method!($type<$inner>, SQRT_2);
720+
};
721+
}
722+
723+
#[test]
724+
fn float_consts_equal_inner() {
725+
test_float_const_methods!(OrderedFloat<f64>);
726+
test_float_const_methods!(OrderedFloat<f32>);
727+
test_float_const_methods!(NotNan<f64>);
728+
test_float_const_methods!(NotNan<f32>);
729+
}
730+
696731
#[cfg(feature = "arbitrary")]
697732
mod arbitrary_test {
698733
use super::{NotNan, OrderedFloat};

0 commit comments

Comments
 (0)