summary refs log tree commit diff
path: root/src/libcore/num
diff options
context:
space:
mode:
authorBrendan Zabarauskas <bjzaba@yahoo.com.au>2013-04-25 15:30:56 +1000
committerBrendan Zabarauskas <bjzaba@yahoo.com.au>2013-04-26 05:55:26 +1000
commitad0b337036f2f9076852d5d6701ec302e3cce101 (patch)
treec775173b7fd613916fd51f4e068aeea4d5864021 /src/libcore/num
parentac69ee418b495a25ff5019d2fd08ff97a4cb34e3 (diff)
downloadrust-ad0b337036f2f9076852d5d6701ec302e3cce101.tar.gz
rust-ad0b337036f2f9076852d5d6701ec302e3cce101.zip
Add is_zero method to Zero
Diffstat (limited to 'src/libcore/num')
-rw-r--r--src/libcore/num/f32.rs16
-rw-r--r--src/libcore/num/f64.rs16
-rw-r--r--src/libcore/num/float.rs20
-rw-r--r--src/libcore/num/int-template.rs9
-rw-r--r--src/libcore/num/num.rs7
-rw-r--r--src/libcore/num/uint-template.rs9
6 files changed, 40 insertions, 37 deletions
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};
@@ -338,8 +338,6 @@ pub fn pow_with_uint(base: uint, pow: uint) -> float {
 }
 
 #[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)]
 pub fn is_finite(x: float) -> bool { f64::is_finite(x as f64) }
@@ -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 }
 }