From ad0b337036f2f9076852d5d6701ec302e3cce101 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Thu, 25 Apr 2013 15:30:56 +1000 Subject: Add is_zero method to Zero --- src/libcore/num/f32.rs | 16 +++++++--------- src/libcore/num/f64.rs | 16 +++++++--------- src/libcore/num/float.rs | 20 +++++++++++--------- src/libcore/num/int-template.rs | 9 ++++++--- src/libcore/num/num.rs | 7 +++---- src/libcore/num/uint-template.rs | 9 ++++++--- 6 files changed, 40 insertions(+), 37 deletions(-) (limited to 'src/libcore/num') diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs index 7d5807ba546..c03761c2322 100644 --- a/src/libcore/num/f32.rs +++ b/src/libcore/num/f32.rs @@ -12,7 +12,7 @@ use from_str; use libc::c_int; -use num::strconv; +use num::{Zero, One, strconv}; use prelude::*; pub use cmath::c_float_targ_consts::*; @@ -154,12 +154,6 @@ pub fn gt(x: f32, y: f32) -> bool { return x > y; } // FIXME (#1999): replace the predicates below with llvm intrinsics or // calls to the libmath macros in the rust runtime for performance. -/// Returns true if `x` is a zero number (positive or negative zero) -#[inline(always)] -pub fn is_zero(x: f32) -> bool { - return x == 0.0f32 || x == -0.0f32; -} - /// Returns true if `x`is an infinite number #[inline(always)] pub fn is_infinite(x: f32) -> bool { @@ -245,12 +239,16 @@ impl Ord for f32 { fn gt(&self, other: &f32) -> bool { (*self) > (*other) } } -impl num::Zero for f32 { +impl Zero for f32 { #[inline(always)] fn zero() -> f32 { 0.0 } + + /// Returns true if the number is equal to either `0.0` or `-0.0` + #[inline(always)] + fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 } } -impl num::One for f32 { +impl One for f32 { #[inline(always)] fn one() -> f32 { 1.0 } } diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs index 3b6198bfc47..ca6416be739 100644 --- a/src/libcore/num/f64.rs +++ b/src/libcore/num/f64.rs @@ -12,7 +12,7 @@ use from_str; use libc::c_int; -use num::strconv; +use num::{Zero, One, strconv}; use prelude::*; pub use cmath::c_double_targ_consts::*; @@ -174,12 +174,6 @@ pub fn ge(x: f64, y: f64) -> bool { return x >= y; } #[inline(always)] pub fn gt(x: f64, y: f64) -> bool { return x > y; } -/// Returns true if `x` is a zero number (positive or negative zero) -#[inline(always)] -pub fn is_zero(x: f64) -> bool { - return x == 0.0f64 || x == -0.0f64; -} - /// Returns true if `x`is an infinite number #[inline(always)] pub fn is_infinite(x: f64) -> bool { @@ -266,12 +260,16 @@ impl Ord for f64 { fn gt(&self, other: &f64) -> bool { (*self) > (*other) } } -impl num::Zero for f64 { +impl Zero for f64 { #[inline(always)] fn zero() -> f64 { 0.0 } + + /// Returns true if the number is equal to either `0.0` or `-0.0` + #[inline(always)] + fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 } } -impl num::One for f64 { +impl One for f64 { #[inline(always)] fn one() -> f64 { 1.0 } } diff --git a/src/libcore/num/float.rs b/src/libcore/num/float.rs index 9c0412b422f..248bc2a4d56 100644 --- a/src/libcore/num/float.rs +++ b/src/libcore/num/float.rs @@ -22,7 +22,7 @@ use from_str; use libc::c_int; -use num::strconv; +use num::{Zero, One, strconv}; use prelude::*; pub use f64::{add, sub, mul, quot, rem, lt, le, eq, ne, ge, gt}; @@ -337,8 +337,6 @@ pub fn pow_with_uint(base: uint, pow: uint) -> float { return total; } -#[inline(always)] -pub fn is_zero(x: float) -> bool { f64::is_zero(x as f64) } #[inline(always)] pub fn is_infinite(x: float) -> bool { f64::is_infinite(x as f64) } #[inline(always)] @@ -393,12 +391,16 @@ impl Ord for float { fn gt(&self, other: &float) -> bool { (*self) > (*other) } } -impl num::Zero for float { +impl Zero for float { #[inline(always)] fn zero() -> float { 0.0 } + + /// Returns true if the number is equal to either `0.0` or `-0.0` + #[inline(always)] + fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 } } -impl num::One for float { +impl One for float { #[inline(always)] fn one() -> float { 1.0 } } @@ -867,11 +869,11 @@ mod tests { } // note: -0 == 0, hence these slightly more complex tests match from_str(~"-0") { - Some(v) if is_zero(v) => assert!(v.is_negative()), + Some(v) if v.is_zero() => assert!(v.is_negative()), _ => fail!() } match from_str(~"0") { - Some(v) if is_zero(v) => assert!(v.is_positive()), + Some(v) if v.is_zero() => assert!(v.is_positive()), _ => fail!() } @@ -914,11 +916,11 @@ mod tests { } // note: -0 == 0, hence these slightly more complex tests match from_str_hex(~"-0") { - Some(v) if is_zero(v) => assert!(v.is_negative()), + Some(v) if v.is_zero() => assert!(v.is_negative()), _ => fail!() } match from_str_hex(~"0") { - Some(v) if is_zero(v) => assert!(v.is_positive()), + Some(v) if v.is_zero() => assert!(v.is_positive()), _ => fail!() } assert_eq!(from_str_hex(~"e"), Some(14.)); diff --git a/src/libcore/num/int-template.rs b/src/libcore/num/int-template.rs index f9edf1cefc8..b53bf876f77 100644 --- a/src/libcore/num/int-template.rs +++ b/src/libcore/num/int-template.rs @@ -12,7 +12,7 @@ use T = self::inst::T; use from_str::FromStr; use num::{ToStrRadix, FromStrRadix}; -use num::strconv; +use num::{Zero, One, strconv}; use prelude::*; pub use cmp::{min, max}; @@ -152,12 +152,15 @@ impl Eq for T { fn ne(&self, other: &T) -> bool { return (*self) != (*other); } } -impl num::Zero for T { +impl Zero for T { #[inline(always)] fn zero() -> T { 0 } + + #[inline(always)] + fn is_zero(&self) -> bool { *self == 0 } } -impl num::One for T { +impl One for T { #[inline(always)] fn one() -> T { 1 } } diff --git a/src/libcore/num/num.rs b/src/libcore/num/num.rs index e19afdc69c3..19d5340527d 100644 --- a/src/libcore/num/num.rs +++ b/src/libcore/num/num.rs @@ -37,13 +37,12 @@ pub trait IntConvertible { } pub trait Zero { - // FIXME (#5527): These should be associated constants - fn zero() -> Self; + fn zero() -> Self; // FIXME (#5527): This should be an associated constant + fn is_zero(&self) -> bool; } pub trait One { - // FIXME (#5527): These should be associated constants - fn one() -> Self; + fn one() -> Self; // FIXME (#5527): This should be an associated constant } pub trait Signed: Num diff --git a/src/libcore/num/uint-template.rs b/src/libcore/num/uint-template.rs index 96019ddd564..803d034c919 100644 --- a/src/libcore/num/uint-template.rs +++ b/src/libcore/num/uint-template.rs @@ -13,7 +13,7 @@ use T_SIGNED = self::inst::T_SIGNED; use from_str::FromStr; use num::{ToStrRadix, FromStrRadix}; -use num::strconv; +use num::{Zero, One, strconv}; use prelude::*; pub use cmp::{min, max}; @@ -118,12 +118,15 @@ impl Eq for T { fn ne(&self, other: &T) -> bool { return (*self) != (*other); } } -impl num::Zero for T { +impl Zero for T { #[inline(always)] fn zero() -> T { 0 } + + #[inline(always)] + fn is_zero(&self) -> bool { *self == 0 } } -impl num::One for T { +impl One for T { #[inline(always)] fn one() -> T { 1 } } -- cgit 1.4.1-3-g733a5