use core::mem;
use core::num::FpCategory;
use core::ops::Neg;
use core::f32;
use core::f64;
use {Num, NumCast, ToPrimitive};
pub trait FloatCore: Num + NumCast + Neg<Output = Self> + PartialOrd + Copy {
fn infinity() -> Self;
fn neg_infinity() -> Self;
fn nan() -> Self;
fn neg_zero() -> Self;
fn min_value() -> Self;
fn min_positive_value() -> Self;
fn epsilon() -> Self;
fn max_value() -> Self;
#[inline]
fn is_nan(self) -> bool {
self != self
}
#[inline]
fn is_infinite(self) -> bool {
self == Self::infinity() || self == Self::neg_infinity()
}
#[inline]
fn is_finite(self) -> bool {
!(self.is_nan() || self.is_infinite())
}
#[inline]
fn is_normal(self) -> bool {
self.classify() == FpCategory::Normal
}
fn classify(self) -> FpCategory;
#[inline]
fn floor(self) -> Self {
let f = self.fract();
if f.is_nan() || f.is_zero() {
self
} else if self < Self::zero() {
self - f - Self::one()
} else {
self - f
}
}
#[inline]
fn ceil(self) -> Self {
let f = self.fract();
if f.is_nan() || f.is_zero() {
self
} else if self > Self::zero() {
self - f + Self::one()
} else {
self - f
}
}
#[inline]
fn round(self) -> Self {
let one = Self::one();
let h = Self::from(0.5).expect("Unable to cast from 0.5");
let f = self.fract();
if f.is_nan() || f.is_zero() {
self
} else if self > Self::zero() {
if f < h {
self - f
} else {
self - f + one
}
} else {
if -f < h {
self - f
} else {
self - f - one
}
}
}
#[inline]
fn trunc(self) -> Self {
let f = self.fract();
if f.is_nan() {
self
} else {
self - f
}
}
#[inline]
fn fract(self) -> Self {
if self.is_zero() {
Self::zero()
} else {
self % Self::one()
}
}
#[inline]
fn abs(self) -> Self {
if self.is_sign_positive() {
return self;
}
if self.is_sign_negative() {
return -self;
}
Self::nan()
}
#[inline]
fn signum(self) -> Self {
if self.is_nan() {
Self::nan()
} else if self.is_sign_negative() {
-Self::one()
} else {
Self::one()
}
}
#[inline]
fn is_sign_positive(self) -> bool {
!self.is_sign_negative()
}
#[inline]
fn is_sign_negative(self) -> bool {
let (_, _, sign) = self.integer_decode();
sign < 0
}
#[inline]
fn min(self, other: Self) -> Self {
if self.is_nan() {
return other;
}
if other.is_nan() {
return self;
}
if self < other {
self
} else {
other
}
}
#[inline]
fn max(self, other: Self) -> Self {
if self.is_nan() {
return other;
}
if other.is_nan() {
return self;
}
if self > other {
self
} else {
other
}
}
#[inline]
fn recip(self) -> Self {
Self::one() / self
}
#[inline]
fn powi(mut self, mut exp: i32) -> Self {
if exp < 0 {
exp = exp.wrapping_neg();
self = self.recip();
}
super::pow(self, (exp as u32).to_usize().unwrap())
}
fn to_degrees(self) -> Self;
fn to_radians(self) -> Self;
fn integer_decode(self) -> (u64, i16, i8);
}
impl FloatCore for f32 {
constant! {
infinity() -> f32::INFINITY;
neg_infinity() -> f32::NEG_INFINITY;
nan() -> f32::NAN;
neg_zero() -> -0.0;
min_value() -> f32::MIN;
min_positive_value() -> f32::MIN_POSITIVE;
epsilon() -> f32::EPSILON;
max_value() -> f32::MAX;
}
#[inline]
fn integer_decode(self) -> (u64, i16, i8) {
integer_decode_f32(self)
}
#[inline]
#[cfg(not(feature = "std"))]
fn classify(self) -> FpCategory {
const EXP_MASK: u32 = 0x7f800000;
const MAN_MASK: u32 = 0x007fffff;
let bits: u32 = unsafe { mem::transmute(self) };
match (bits & MAN_MASK, bits & EXP_MASK) {
(0, 0) => FpCategory::Zero,
(_, 0) => FpCategory::Subnormal,
(0, EXP_MASK) => FpCategory::Infinite,
(_, EXP_MASK) => FpCategory::Nan,
_ => FpCategory::Normal,
}
}
#[inline]
#[cfg(not(feature = "std"))]
fn to_degrees(self) -> Self {
const PIS_IN_180: f32 = 57.2957795130823208767981548141051703_f32;
self * PIS_IN_180
}
#[inline]
#[cfg(not(feature = "std"))]
fn to_radians(self) -> Self {
self * (f32::consts::PI / 180.0)
}
#[cfg(feature = "std")]
forward! {
Self::is_nan(self) -> bool;
Self::is_infinite(self) -> bool;
Self::is_finite(self) -> bool;
Self::is_normal(self) -> bool;
Self::classify(self) -> FpCategory;
Self::floor(self) -> Self;
Self::ceil(self) -> Self;
Self::round(self) -> Self;
Self::trunc(self) -> Self;
Self::fract(self) -> Self;
Self::abs(self) -> Self;
Self::signum(self) -> Self;
Self::is_sign_positive(self) -> bool;
Self::is_sign_negative(self) -> bool;
Self::min(self, other: Self) -> Self;
Self::max(self, other: Self) -> Self;
Self::recip(self) -> Self;
Self::powi(self, n: i32) -> Self;
Self::to_degrees(self) -> Self;
Self::to_radians(self) -> Self;
}
}
impl FloatCore for f64 {
constant! {
infinity() -> f64::INFINITY;
neg_infinity() -> f64::NEG_INFINITY;
nan() -> f64::NAN;
neg_zero() -> -0.0;
min_value() -> f64::MIN;
min_positive_value() -> f64::MIN_POSITIVE;
epsilon() -> f64::EPSILON;
max_value() -> f64::MAX;
}
#[inline]
fn integer_decode(self) -> (u64, i16, i8) {
integer_decode_f64(self)
}
#[inline]
#[cfg(not(feature = "std"))]
fn classify(self) -> FpCategory {
const EXP_MASK: u64 = 0x7ff0000000000000;
const MAN_MASK: u64 = 0x000fffffffffffff;
let bits: u64 = unsafe { mem::transmute(self) };
match (bits & MAN_MASK, bits & EXP_MASK) {
(0, 0) => FpCategory::Zero,
(_, 0) => FpCategory::Subnormal,
(0, EXP_MASK) => FpCategory::Infinite,
(_, EXP_MASK) => FpCategory::Nan,
_ => FpCategory::Normal,
}
}
#[inline]
#[cfg(not(feature = "std"))]
fn to_degrees(self) -> Self {
self * (180.0 / f64::consts::PI)
}
#[inline]
#[cfg(not(feature = "std"))]
fn to_radians(self) -> Self {
self * (f64::consts::PI / 180.0)
}
#[cfg(feature = "std")]
forward! {
Self::is_nan(self) -> bool;
Self::is_infinite(self) -> bool;
Self::is_finite(self) -> bool;
Self::is_normal(self) -> bool;
Self::classify(self) -> FpCategory;
Self::floor(self) -> Self;
Self::ceil(self) -> Self;
Self::round(self) -> Self;
Self::trunc(self) -> Self;
Self::fract(self) -> Self;
Self::abs(self) -> Self;
Self::signum(self) -> Self;
Self::is_sign_positive(self) -> bool;
Self::is_sign_negative(self) -> bool;
Self::min(self, other: Self) -> Self;
Self::max(self, other: Self) -> Self;
Self::recip(self) -> Self;
Self::powi(self, n: i32) -> Self;
Self::to_degrees(self) -> Self;
Self::to_radians(self) -> Self;
}
}
#[cfg(feature = "std")]
pub trait Float: Num + Copy + NumCast + PartialOrd + Neg<Output = Self> {
fn nan() -> Self;
fn infinity() -> Self;
fn neg_infinity() -> Self;
fn neg_zero() -> Self;
fn min_value() -> Self;
fn min_positive_value() -> Self;
fn epsilon() -> Self {
Self::from(f32::EPSILON).expect("Unable to cast from f32::EPSILON")
}
fn max_value() -> Self;
fn is_nan(self) -> bool;
fn is_infinite(self) -> bool;
fn is_finite(self) -> bool;
fn is_normal(self) -> bool;
fn classify(self) -> FpCategory;
fn floor(self) -> Self;
fn ceil(self) -> Self;
fn round(self) -> Self;
fn trunc(self) -> Self;
fn fract(self) -> Self;
fn abs(self) -> Self;
fn signum(self) -> Self;
fn is_sign_positive(self) -> bool;
fn is_sign_negative(self) -> bool;
fn mul_add(self, a: Self, b: Self) -> Self;
fn recip(self) -> Self;
fn powi(self, n: i32) -> Self;
fn powf(self, n: Self) -> Self;
fn sqrt(self) -> Self;
fn exp(self) -> Self;
fn exp2(self) -> Self;
fn ln(self) -> Self;
fn log(self, base: Self) -> Self;
fn log2(self) -> Self;
fn log10(self) -> Self;
#[inline]
fn to_degrees(self) -> Self {
let halfpi = Self::zero().acos();
let ninety = Self::from(90u8).unwrap();
self * ninety / halfpi
}
#[inline]
fn to_radians(self) -> Self {
let halfpi = Self::zero().acos();
let ninety = Self::from(90u8).unwrap();
self * halfpi / ninety
}
fn max(self, other: Self) -> Self;
fn min(self, other: Self) -> Self;
fn abs_sub(self, other: Self) -> Self;
fn cbrt(self) -> Self;
fn hypot(self, other: Self) -> Self;
fn sin(self) -> Self;
fn cos(self) -> Self;
fn tan(self) -> Self;
fn asin(self) -> Self;
fn acos(self) -> Self;
fn atan(self) -> Self;
fn atan2(self, other: Self) -> Self;
fn sin_cos(self) -> (Self, Self);
fn exp_m1(self) -> Self;
fn ln_1p(self) -> Self;
fn sinh(self) -> Self;
fn cosh(self) -> Self;
fn tanh(self) -> Self;
fn asinh(self) -> Self;
fn acosh(self) -> Self;
fn atanh(self) -> Self;
fn integer_decode(self) -> (u64, i16, i8);
}
#[cfg(feature = "std")]
macro_rules! float_impl {
($T:ident $decode:ident) => {
impl Float for $T {
constant! {
nan() -> $T::NAN;
infinity() -> $T::INFINITY;
neg_infinity() -> $T::NEG_INFINITY;
neg_zero() -> -0.0;
min_value() -> $T::MIN;
min_positive_value() -> $T::MIN_POSITIVE;
epsilon() -> $T::EPSILON;
max_value() -> $T::MAX;
}
#[inline]
#[allow(deprecated)]
fn abs_sub(self, other: Self) -> Self {
<$T>::abs_sub(self, other)
}
#[inline]
fn integer_decode(self) -> (u64, i16, i8) {
$decode(self)
}
forward! {
Self::is_nan(self) -> bool;
Self::is_infinite(self) -> bool;
Self::is_finite(self) -> bool;
Self::is_normal(self) -> bool;
Self::classify(self) -> FpCategory;
Self::floor(self) -> Self;
Self::ceil(self) -> Self;
Self::round(self) -> Self;
Self::trunc(self) -> Self;
Self::fract(self) -> Self;
Self::abs(self) -> Self;
Self::signum(self) -> Self;
Self::is_sign_positive(self) -> bool;
Self::is_sign_negative(self) -> bool;
Self::mul_add(self, a: Self, b: Self) -> Self;
Self::recip(self) -> Self;
Self::powi(self, n: i32) -> Self;
Self::powf(self, n: Self) -> Self;
Self::sqrt(self) -> Self;
Self::exp(self) -> Self;
Self::exp2(self) -> Self;
Self::ln(self) -> Self;
Self::log(self, base: Self) -> Self;
Self::log2(self) -> Self;
Self::log10(self) -> Self;
Self::to_degrees(self) -> Self;
Self::to_radians(self) -> Self;
Self::max(self, other: Self) -> Self;
Self::min(self, other: Self) -> Self;
Self::cbrt(self) -> Self;
Self::hypot(self, other: Self) -> Self;
Self::sin(self) -> Self;
Self::cos(self) -> Self;
Self::tan(self) -> Self;
Self::asin(self) -> Self;
Self::acos(self) -> Self;
Self::atan(self) -> Self;
Self::atan2(self, other: Self) -> Self;
Self::sin_cos(self) -> (Self, Self);
Self::exp_m1(self) -> Self;
Self::ln_1p(self) -> Self;
Self::sinh(self) -> Self;
Self::cosh(self) -> Self;
Self::tanh(self) -> Self;
Self::asinh(self) -> Self;
Self::acosh(self) -> Self;
Self::atanh(self) -> Self;
}
}
};
}
fn integer_decode_f32(f: f32) -> (u64, i16, i8) {
let bits: u32 = unsafe { mem::transmute(f) };
let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 };
let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;
let mantissa = if exponent == 0 {
(bits & 0x7fffff) << 1
} else {
(bits & 0x7fffff) | 0x800000
};
exponent -= 127 + 23;
(mantissa as u64, exponent, sign)
}
fn integer_decode_f64(f: f64) -> (u64, i16, i8) {
let bits: u64 = unsafe { mem::transmute(f) };
let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 };
let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
let mantissa = if exponent == 0 {
(bits & 0xfffffffffffff) << 1
} else {
(bits & 0xfffffffffffff) | 0x10000000000000
};
exponent -= 1023 + 52;
(mantissa, exponent, sign)
}
#[cfg(feature = "std")]
float_impl!(f32 integer_decode_f32);
#[cfg(feature = "std")]
float_impl!(f64 integer_decode_f64);
macro_rules! float_const_impl {
($(#[$doc:meta] $constant:ident,)+) => (
#[allow(non_snake_case)]
pub trait FloatConst {
$(#[$doc] fn $constant() -> Self;)+
}
float_const_impl! { @float f32, $($constant,)+ }
float_const_impl! { @float f64, $($constant,)+ }
);
(@float $T:ident, $($constant:ident,)+) => (
impl FloatConst for $T {
constant! {
$( $constant() -> $T::consts::$constant; )+
}
}
);
}
float_const_impl! {
#[doc = "Return Euler’s number."]
E,
#[doc = "Return `1.0 / π`."]
FRAC_1_PI,
#[doc = "Return `1.0 / sqrt(2.0)`."]
FRAC_1_SQRT_2,
#[doc = "Return `2.0 / π`."]
FRAC_2_PI,
#[doc = "Return `2.0 / sqrt(π)`."]
FRAC_2_SQRT_PI,
#[doc = "Return `π / 2.0`."]
FRAC_PI_2,
#[doc = "Return `π / 3.0`."]
FRAC_PI_3,
#[doc = "Return `π / 4.0`."]
FRAC_PI_4,
#[doc = "Return `π / 6.0`."]
FRAC_PI_6,
#[doc = "Return `π / 8.0`."]
FRAC_PI_8,
#[doc = "Return `ln(10.0)`."]
LN_10,
#[doc = "Return `ln(2.0)`."]
LN_2,
#[doc = "Return `log10(e)`."]
LOG10_E,
#[doc = "Return `log2(e)`."]
LOG2_E,
#[doc = "Return Archimedes’ constant."]
PI,
#[doc = "Return `sqrt(2.0)`."]
SQRT_2,
}
#[cfg(test)]
mod tests {
use core::f64::consts;
const DEG_RAD_PAIRS: [(f64, f64); 7] = [
(0.0, 0.),
(22.5, consts::FRAC_PI_8),
(30.0, consts::FRAC_PI_6),
(45.0, consts::FRAC_PI_4),
(60.0, consts::FRAC_PI_3),
(90.0, consts::FRAC_PI_2),
(180.0, consts::PI),
];
#[test]
fn convert_deg_rad() {
use float::FloatCore;
for &(deg, rad) in &DEG_RAD_PAIRS {
assert!((FloatCore::to_degrees(rad) - deg).abs() < 1e-6);
assert!((FloatCore::to_radians(deg) - rad).abs() < 1e-6);
let (deg, rad) = (deg as f32, rad as f32);
assert!((FloatCore::to_degrees(rad) - deg).abs() < 1e-5);
assert!((FloatCore::to_radians(deg) - rad).abs() < 1e-5);
}
}
#[cfg(feature = "std")]
#[test]
fn convert_deg_rad_std() {
for &(deg, rad) in &DEG_RAD_PAIRS {
use Float;
assert!((Float::to_degrees(rad) - deg).abs() < 1e-6);
assert!((Float::to_radians(deg) - rad).abs() < 1e-6);
let (deg, rad) = (deg as f32, rad as f32);
assert!((Float::to_degrees(rad) - deg).abs() < 1e-5);
assert!((Float::to_radians(deg) - rad).abs() < 1e-5);
}
}
#[test]
#[cfg(not(feature = "std"))]
fn to_degrees_rounding() {
use float::FloatCore;
assert_eq!(
FloatCore::to_degrees(1_f32),
57.2957795130823208767981548141051703
);
}
}