2222
2323use libc:: c_int;
2424use num:: { Zero , One , strconv} ;
25+ use num:: FPCategory ;
2526use prelude:: * ;
2627
2728pub use f64:: { add, sub, mul, div, rem, lt, le, eq, ne, ge, gt} ;
@@ -782,32 +783,37 @@ impl Primitive for float {
782783
783784impl Float for float {
784785 #[inline(always)]
785- fn NaN() -> float { 0.0 / 0.0 }
786+ fn NaN() -> float { Float::NaN::<f64>() as float }
786787
787788 #[inline(always)]
788- fn infinity() -> float { 1.0 / 0.0 }
789+ fn infinity() -> float { Float::infinity::<f64>() as float }
789790
790791 #[inline(always)]
791- fn neg_infinity() -> float { -1.0 / 0.0 }
792+ fn neg_infinity() -> float { Float::neg_infinity::<f64>() as float }
792793
793794 #[inline(always)]
794- fn neg_zero() -> float { -0.0 }
795+ fn neg_zero() -> float { Float::neg_zero::<f64>() as float }
795796
796797 /// Returns `true` if the number is NaN
797798 #[inline(always)]
798- fn is_NaN(&self) -> bool { *self != *self }
799+ fn is_NaN(&self) -> bool { ( *self as f64).is_NaN() }
799800
800801 /// Returns `true` if the number is infinite
801802 #[inline(always)]
802- fn is_infinite(&self) -> bool {
803- *self == Float::infinity() || *self == Float::neg_infinity()
804- }
803+ fn is_infinite(&self) -> bool { (*self as f64).is_infinite() }
805804
806- /// Returns `true` if the number is not infinite or NaN
805+ /// Returns `true` if the number is neither infinite or NaN
807806 #[inline(always)]
808- fn is_finite(&self) -> bool {
809- !(self.is_NaN() || self.is_infinite())
810- }
807+ fn is_finite(&self) -> bool { (*self as f64).is_finite() }
808+
809+ /// Returns `true` if the number is neither zero, infinite, subnormal or NaN
810+ #[inline(always)]
811+ fn is_normal(&self) -> bool { (*self as f64).is_normal() }
812+
813+ /// Returns the floating point category of the number. If only one property is going to
814+ /// be tested, it is generally faster to use the specific predicate instead.
815+ #[inline(always)]
816+ fn classify(&self) -> FPCategory { (*self as f64).classify() }
811817
812818 #[inline(always)]
813819 fn mantissa_digits() -> uint { Float::mantissa_digits::<f64>() }
@@ -844,9 +850,7 @@ impl Float for float {
844850 /// than if the operations were performed separately
845851 ///
846852 #[inline(always)]
847- fn ln_1p(&self) -> float {
848- (*self as f64).ln_1p() as float
849- }
853+ fn ln_1p(&self) -> float { (*self as f64).ln_1p() as float }
850854
851855 ///
852856 /// Fused multiply-add. Computes `(self * a) + b` with only one rounding error. This
@@ -867,6 +871,7 @@ impl Float for float {
867871
868872#[cfg(test)]
869873mod tests {
874+ use num::*;
870875 use super::*;
871876 use prelude::*;
872877
@@ -1063,6 +1068,30 @@ mod tests {
10631068 assert_eq!(Primitive::bytes::<float>(), sys::size_of::<float>());
10641069 }
10651070
1071+ #[test]
1072+ fn test_is_normal() {
1073+ assert!(!Float::NaN::<float>().is_normal());
1074+ assert!(!Float::infinity::<float>().is_normal());
1075+ assert!(!Float::neg_infinity::<float>().is_normal());
1076+ assert!(!Zero::zero::<float>().is_normal());
1077+ assert!(!Float::neg_zero::<float>().is_normal());
1078+ assert!(1f.is_normal());
1079+ assert!(1e-307f.is_normal());
1080+ assert!(!1e-308f.is_normal());
1081+ }
1082+
1083+ #[test]
1084+ fn test_classify() {
1085+ assert_eq!(Float::NaN::<float>().classify(), FPNaN);
1086+ assert_eq!(Float::infinity::<float>().classify(), FPInfinite);
1087+ assert_eq!(Float::neg_infinity::<float>().classify(), FPInfinite);
1088+ assert_eq!(Zero::zero::<float>().classify(), FPZero);
1089+ assert_eq!(Float::neg_zero::<float>().classify(), FPZero);
1090+ assert_eq!(1f.classify(), FPNormal);
1091+ assert_eq!(1e-307f.classify(), FPNormal);
1092+ assert_eq!(1e-308f.classify(), FPSubnormal);
1093+ }
1094+
10661095 #[test]
10671096 pub fn test_to_str_exact_do_decimal() {
10681097 let s = to_str_exact(5.0, 4u);
0 commit comments