diff --git a/src/lib.rs b/src/lib.rs index 1f521a5..b34de94 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,7 +29,7 @@ use num_traits::float::FloatCore as Float; #[cfg(feature = "std")] pub use num_traits::Float; use num_traits::{ - AsPrimitive, Bounded, FromPrimitive, Num, NumCast, One, Signed, ToPrimitive, Zero, + AsPrimitive, Bounded, FloatConst, FromPrimitive, Num, NumCast, One, Signed, ToPrimitive, Zero, }; // masks for the parts of the IEEE 754 float @@ -1537,6 +1537,42 @@ impl NumCast for NotNan { } } +macro_rules! impl_float_const_method { + ($wrapper:expr, $method:ident) => { + #[allow(non_snake_case)] + fn $method() -> Self { + $wrapper(T::$method()) + } + }; +} + +macro_rules! impl_float_const { + ($type:ident, $wrapper:expr) => { + impl FloatConst for $type { + impl_float_const_method!($wrapper, E); + impl_float_const_method!($wrapper, FRAC_1_PI); + impl_float_const_method!($wrapper, FRAC_1_SQRT_2); + impl_float_const_method!($wrapper, FRAC_2_PI); + impl_float_const_method!($wrapper, FRAC_2_SQRT_PI); + impl_float_const_method!($wrapper, FRAC_PI_2); + impl_float_const_method!($wrapper, FRAC_PI_3); + impl_float_const_method!($wrapper, FRAC_PI_4); + impl_float_const_method!($wrapper, FRAC_PI_6); + impl_float_const_method!($wrapper, FRAC_PI_8); + impl_float_const_method!($wrapper, LN_10); + impl_float_const_method!($wrapper, LN_2); + impl_float_const_method!($wrapper, LOG10_E); + impl_float_const_method!($wrapper, LOG2_E); + impl_float_const_method!($wrapper, PI); + impl_float_const_method!($wrapper, SQRT_2); + } + }; +} + +impl_float_const!(OrderedFloat, OrderedFloat); +// Float constants are not NaN. +impl_float_const!(NotNan, |x| unsafe { NotNan::new_unchecked(x) }); + #[cfg(feature = "serde")] mod impl_serde { extern crate serde; diff --git a/tests/test.rs b/tests/test.rs index 8e6b835..906141c 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -7,7 +7,7 @@ extern crate ordered_float; pub use num_traits::float::FloatCore as Float; #[cfg(feature = "std")] pub use num_traits::Float; -pub use num_traits::{Bounded, FromPrimitive, Num, One, Signed, ToPrimitive, Zero}; +pub use num_traits::{Bounded, FloatConst, FromPrimitive, Num, One, Signed, ToPrimitive, Zero}; pub use ordered_float::*; pub use std::cmp::Ordering::*; @@ -693,6 +693,41 @@ fn from_ref() { assert_eq!(f, 2.0f64); } +macro_rules! test_float_const_method { + ($type:ident < $inner:ident >, $method:ident) => { + assert_eq!($type::<$inner>::$method().into_inner(), $inner::$method()) + }; +} + +macro_rules! test_float_const_methods { + ($type:ident < $inner:ident >) => { + test_float_const_method!($type<$inner>, E); + test_float_const_method!($type<$inner>, FRAC_1_PI); + test_float_const_method!($type<$inner>, FRAC_1_SQRT_2); + test_float_const_method!($type<$inner>, FRAC_2_PI); + test_float_const_method!($type<$inner>, FRAC_2_SQRT_PI); + test_float_const_method!($type<$inner>, FRAC_PI_2); + test_float_const_method!($type<$inner>, FRAC_PI_3); + test_float_const_method!($type<$inner>, FRAC_PI_4); + test_float_const_method!($type<$inner>, FRAC_PI_6); + test_float_const_method!($type<$inner>, FRAC_PI_8); + test_float_const_method!($type<$inner>, LN_10); + test_float_const_method!($type<$inner>, LN_2); + test_float_const_method!($type<$inner>, LOG10_E); + test_float_const_method!($type<$inner>, LOG2_E); + test_float_const_method!($type<$inner>, PI); + test_float_const_method!($type<$inner>, SQRT_2); + }; +} + +#[test] +fn float_consts_equal_inner() { + test_float_const_methods!(OrderedFloat); + test_float_const_methods!(OrderedFloat); + test_float_const_methods!(NotNan); + test_float_const_methods!(NotNan); +} + #[cfg(feature = "arbitrary")] mod arbitrary_test { use super::{NotNan, OrderedFloat};