Skip to content

Commit

Permalink
Add FloatConst implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
justinlovinger authored and mbrubeck committed Jan 5, 2023
1 parent e45839a commit dcdabc8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
38 changes: 37 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -1537,6 +1537,42 @@ impl<T: Float> NumCast for NotNan<T> {
}
}

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<T: FloatConst> FloatConst for $type<T> {
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;
Expand Down
37 changes: 36 additions & 1 deletion tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -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<f64>);
test_float_const_methods!(OrderedFloat<f32>);
test_float_const_methods!(NotNan<f64>);
test_float_const_methods!(NotNan<f32>);
}

#[cfg(feature = "arbitrary")]
mod arbitrary_test {
use super::{NotNan, OrderedFloat};
Expand Down

0 comments on commit dcdabc8

Please sign in to comment.