Use macros for more float constants

This commit is contained in:
Josh Stone 2018-02-27 16:51:30 -08:00
parent 83d498d0be
commit 6fa29be7c0
2 changed files with 46 additions and 127 deletions

View File

@ -2,9 +2,8 @@ use core::mem;
use core::ops::Neg; use core::ops::Neg;
use core::num::FpCategory; use core::num::FpCategory;
// Used for default implementation of `epsilon` use core::f32;
#[cfg(feature = "std")] use core::f64;
use std::f32;
use {Num, NumCast, ToPrimitive}; use {Num, NumCast, ToPrimitive};
@ -238,44 +237,15 @@ pub trait FloatCore: Num + NumCast + Neg<Output = Self> + PartialOrd + Copy {
} }
impl FloatCore for f32 { impl FloatCore for f32 {
#[inline] constant! {
fn infinity() -> Self { infinity() -> f32::INFINITY;
::core::f32::INFINITY neg_infinity() -> f32::NEG_INFINITY;
} nan() -> f32::NAN;
neg_zero() -> -0.0;
#[inline] min_value() -> f32::MIN;
fn neg_infinity() -> Self { min_positive_value() -> f32::MIN_POSITIVE;
::core::f32::NEG_INFINITY epsilon() -> f32::EPSILON;
} max_value() -> f32::MAX;
#[inline]
fn nan() -> Self {
::core::f32::NAN
}
#[inline]
fn neg_zero() -> Self {
-0.0
}
#[inline]
fn min_value() -> Self {
::core::f32::MIN
}
#[inline]
fn min_positive_value() -> Self {
::core::f32::MIN_POSITIVE
}
#[inline]
fn epsilon() -> Self {
::core::f32::EPSILON
}
#[inline]
fn max_value() -> Self {
::core::f32::MAX
} }
#[inline] #[inline]
@ -302,13 +272,13 @@ impl FloatCore for f32 {
#[inline] #[inline]
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
fn to_degrees(self) -> Self { fn to_degrees(self) -> Self {
self * (180.0 / ::core::f32::consts::PI) self * (180.0 / f32::consts::PI)
} }
#[inline] #[inline]
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
fn to_radians(self) -> Self { fn to_radians(self) -> Self {
self * (::core::f32::consts::PI / 180.0) self * (f32::consts::PI / 180.0)
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
@ -337,44 +307,15 @@ impl FloatCore for f32 {
} }
impl FloatCore for f64 { impl FloatCore for f64 {
#[inline] constant! {
fn infinity() -> Self { infinity() -> f64::INFINITY;
::core::f64::INFINITY neg_infinity() -> f64::NEG_INFINITY;
} nan() -> f64::NAN;
neg_zero() -> -0.0;
#[inline] min_value() -> f64::MIN;
fn neg_infinity() -> Self { min_positive_value() -> f64::MIN_POSITIVE;
::core::f64::NEG_INFINITY epsilon() -> f64::EPSILON;
} max_value() -> f64::MAX;
#[inline]
fn nan() -> Self {
::core::f64::NAN
}
#[inline]
fn neg_zero() -> Self {
-0.0
}
#[inline]
fn min_value() -> Self {
::core::f64::MIN
}
#[inline]
fn min_positive_value() -> Self {
::core::f64::MIN_POSITIVE
}
#[inline]
fn epsilon() -> Self {
::core::f64::EPSILON
}
#[inline]
fn max_value() -> Self {
::core::f64::MAX
} }
#[inline] #[inline]
@ -401,13 +342,13 @@ impl FloatCore for f64 {
#[inline] #[inline]
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
fn to_degrees(self) -> Self { fn to_degrees(self) -> Self {
self * (180.0 / ::core::f64::consts::PI) self * (180.0 / f64::consts::PI)
} }
#[inline] #[inline]
#[cfg(not(feature = "std"))] #[cfg(not(feature = "std"))]
fn to_radians(self) -> Self { fn to_radians(self) -> Self {
self * (::core::f64::consts::PI / 180.0) self * (f64::consts::PI / 180.0)
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
@ -1359,44 +1300,15 @@ pub trait Float
macro_rules! float_impl { macro_rules! float_impl {
($T:ident $decode:ident) => ( ($T:ident $decode:ident) => (
impl Float for $T { impl Float for $T {
#[inline] constant! {
fn nan() -> Self { nan() -> $T::NAN;
::std::$T::NAN infinity() -> $T::INFINITY;
} neg_infinity() -> $T::NEG_INFINITY;
neg_zero() -> -0.0;
#[inline] min_value() -> $T::MIN;
fn infinity() -> Self { min_positive_value() -> $T::MIN_POSITIVE;
::std::$T::INFINITY epsilon() -> $T::EPSILON;
} max_value() -> $T::MAX;
#[inline]
fn neg_infinity() -> Self {
::std::$T::NEG_INFINITY
}
#[inline]
fn neg_zero() -> Self {
-0.0
}
#[inline]
fn min_value() -> Self {
::std::$T::MIN
}
#[inline]
fn min_positive_value() -> Self {
::std::$T::MIN_POSITIVE
}
#[inline]
fn epsilon() -> Self {
::std::$T::EPSILON
}
#[inline]
fn max_value() -> Self {
::std::$T::MAX
} }
#[inline] #[inline]
@ -1515,12 +1427,9 @@ macro_rules! float_const_impl {
); );
(@float $T:ident, $($constant:ident,)+) => ( (@float $T:ident, $($constant:ident,)+) => (
impl FloatConst for $T { impl FloatConst for $T {
$( constant! {
#[inline] $( $constant() -> $T::consts::$constant; )+
fn $constant() -> Self { }
::core::$T::consts::$constant
}
)+
} }
); );
} }

View File

@ -23,3 +23,13 @@ macro_rules! forward {
} }
)*} )*}
} }
macro_rules! constant {
($( $method:ident () -> $ret:expr ; )*)
=> {$(
#[inline]
fn $method() -> Self {
$ret
}
)*};
}