Make Float::classify matching more clear for f64 and f32

This commit is contained in:
Brendan Zabarauskas 2013-05-13 07:57:27 +10:00
parent 36771ef609
commit 4f8084a363
2 changed files with 14 additions and 20 deletions

View file

@ -578,10 +578,7 @@ impl Float for f32 {
/// Returns `true` if the number is neither zero, infinite, subnormal or NaN /// Returns `true` if the number is neither zero, infinite, subnormal or NaN
#[inline(always)] #[inline(always)]
fn is_normal(&self) -> bool { fn is_normal(&self) -> bool {
match self.classify() { self.classify() == FPNormal
FPNormal => true,
_ => false,
}
} }
/// Returns the floating point category of the number. If only one property is going to /// Returns the floating point category of the number. If only one property is going to
@ -591,14 +588,14 @@ impl Float for f32 {
static MAN_MASK: u32 = 0x007fffff; static MAN_MASK: u32 = 0x007fffff;
match ( match (
unsafe { ::cast::transmute::<f32,u32>(*self) } & MAN_MASK,
unsafe { ::cast::transmute::<f32,u32>(*self) } & EXP_MASK, unsafe { ::cast::transmute::<f32,u32>(*self) } & EXP_MASK,
unsafe { ::cast::transmute::<f32,u32>(*self) } & MAN_MASK
) { ) {
(EXP_MASK, 0) => FPInfinite, (0, 0) => FPZero,
(EXP_MASK, _) => FPNaN, (_, 0) => FPSubnormal,
(exp, _) if exp != 0 => FPNormal, (0, EXP_MASK) => FPInfinite,
_ if self.is_zero() => FPZero, (_, EXP_MASK) => FPNaN,
_ => FPSubnormal, _ => FPNormal,
} }
} }

View file

@ -621,10 +621,7 @@ impl Float for f64 {
/// Returns `true` if the number is neither zero, infinite, subnormal or NaN /// Returns `true` if the number is neither zero, infinite, subnormal or NaN
#[inline(always)] #[inline(always)]
fn is_normal(&self) -> bool { fn is_normal(&self) -> bool {
match self.classify() { self.classify() == FPNormal
FPNormal => true,
_ => false,
}
} }
/// Returns the floating point category of the number. If only one property is going to /// Returns the floating point category of the number. If only one property is going to
@ -634,14 +631,14 @@ impl Float for f64 {
static MAN_MASK: u64 = 0x000fffffffffffff; static MAN_MASK: u64 = 0x000fffffffffffff;
match ( match (
unsafe { ::cast::transmute::<f64,u64>(*self) } & MAN_MASK,
unsafe { ::cast::transmute::<f64,u64>(*self) } & EXP_MASK, unsafe { ::cast::transmute::<f64,u64>(*self) } & EXP_MASK,
unsafe { ::cast::transmute::<f64,u64>(*self) } & MAN_MASK
) { ) {
(EXP_MASK, 0) => FPInfinite, (0, 0) => FPZero,
(EXP_MASK, _) => FPNaN, (_, 0) => FPSubnormal,
(exp, _) if exp != 0 => FPNormal, (0, EXP_MASK) => FPInfinite,
_ if self.is_zero() => FPZero, (_, EXP_MASK) => FPNaN,
_ => FPSubnormal, _ => FPNormal,
} }
} }