Skip to content

Commit ad0b337

Browse files
committed
Add is_zero method to Zero
1 parent ac69ee4 commit ad0b337

File tree

9 files changed

+67
-56
lines changed

9 files changed

+67
-56
lines changed

src/libcore/num/f32.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use from_str;
1414
use libc::c_int;
15-
use num::strconv;
15+
use num::{Zero, One, strconv};
1616
use prelude::*;
1717

1818
pub use cmath::c_float_targ_consts::*;
@@ -154,12 +154,6 @@ pub fn gt(x: f32, y: f32) -> bool { return x > y; }
154154
// FIXME (#1999): replace the predicates below with llvm intrinsics or
155155
// calls to the libmath macros in the rust runtime for performance.
156156

157-
/// Returns true if `x` is a zero number (positive or negative zero)
158-
#[inline(always)]
159-
pub fn is_zero(x: f32) -> bool {
160-
return x == 0.0f32 || x == -0.0f32;
161-
}
162-
163157
/// Returns true if `x`is an infinite number
164158
#[inline(always)]
165159
pub fn is_infinite(x: f32) -> bool {
@@ -245,12 +239,16 @@ impl Ord for f32 {
245239
fn gt(&self, other: &f32) -> bool { (*self) > (*other) }
246240
}
247241

248-
impl num::Zero for f32 {
242+
impl Zero for f32 {
249243
#[inline(always)]
250244
fn zero() -> f32 { 0.0 }
245+
246+
/// Returns true if the number is equal to either `0.0` or `-0.0`
247+
#[inline(always)]
248+
fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 }
251249
}
252250

253-
impl num::One for f32 {
251+
impl One for f32 {
254252
#[inline(always)]
255253
fn one() -> f32 { 1.0 }
256254
}

src/libcore/num/f64.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use from_str;
1414
use libc::c_int;
15-
use num::strconv;
15+
use num::{Zero, One, strconv};
1616
use prelude::*;
1717

1818
pub use cmath::c_double_targ_consts::*;
@@ -174,12 +174,6 @@ pub fn ge(x: f64, y: f64) -> bool { return x >= y; }
174174
#[inline(always)]
175175
pub fn gt(x: f64, y: f64) -> bool { return x > y; }
176176

177-
/// Returns true if `x` is a zero number (positive or negative zero)
178-
#[inline(always)]
179-
pub fn is_zero(x: f64) -> bool {
180-
return x == 0.0f64 || x == -0.0f64;
181-
}
182-
183177
/// Returns true if `x`is an infinite number
184178
#[inline(always)]
185179
pub fn is_infinite(x: f64) -> bool {
@@ -266,12 +260,16 @@ impl Ord for f64 {
266260
fn gt(&self, other: &f64) -> bool { (*self) > (*other) }
267261
}
268262

269-
impl num::Zero for f64 {
263+
impl Zero for f64 {
270264
#[inline(always)]
271265
fn zero() -> f64 { 0.0 }
266+
267+
/// Returns true if the number is equal to either `0.0` or `-0.0`
268+
#[inline(always)]
269+
fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 }
272270
}
273271

274-
impl num::One for f64 {
272+
impl One for f64 {
275273
#[inline(always)]
276274
fn one() -> f64 { 1.0 }
277275
}

src/libcore/num/float.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
use from_str;
2424
use libc::c_int;
25-
use num::strconv;
25+
use num::{Zero, One, strconv};
2626
use prelude::*;
2727

2828
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 {
337337
return total;
338338
}
339339
340-
#[inline(always)]
341-
pub fn is_zero(x: float) -> bool { f64::is_zero(x as f64) }
342340
#[inline(always)]
343341
pub fn is_infinite(x: float) -> bool { f64::is_infinite(x as f64) }
344342
#[inline(always)]
@@ -393,12 +391,16 @@ impl Ord for float {
393391
fn gt(&self, other: &float) -> bool { (*self) > (*other) }
394392
}
395393
396-
impl num::Zero for float {
394+
impl Zero for float {
397395
#[inline(always)]
398396
fn zero() -> float { 0.0 }
397+
398+
/// Returns true if the number is equal to either `0.0` or `-0.0`
399+
#[inline(always)]
400+
fn is_zero(&self) -> bool { *self == 0.0 || *self == -0.0 }
399401
}
400402
401-
impl num::One for float {
403+
impl One for float {
402404
#[inline(always)]
403405
fn one() -> float { 1.0 }
404406
}
@@ -867,11 +869,11 @@ mod tests {
867869
}
868870
// note: -0 == 0, hence these slightly more complex tests
869871
match from_str(~"-0") {
870-
Some(v) if is_zero(v) => assert!(v.is_negative()),
872+
Some(v) if v.is_zero() => assert!(v.is_negative()),
871873
_ => fail!()
872874
}
873875
match from_str(~"0") {
874-
Some(v) if is_zero(v) => assert!(v.is_positive()),
876+
Some(v) if v.is_zero() => assert!(v.is_positive()),
875877
_ => fail!()
876878
}
877879
@@ -914,11 +916,11 @@ mod tests {
914916
}
915917
// note: -0 == 0, hence these slightly more complex tests
916918
match from_str_hex(~"-0") {
917-
Some(v) if is_zero(v) => assert!(v.is_negative()),
919+
Some(v) if v.is_zero() => assert!(v.is_negative()),
918920
_ => fail!()
919921
}
920922
match from_str_hex(~"0") {
921-
Some(v) if is_zero(v) => assert!(v.is_positive()),
923+
Some(v) if v.is_zero() => assert!(v.is_positive()),
922924
_ => fail!()
923925
}
924926
assert_eq!(from_str_hex(~"e"), Some(14.));

src/libcore/num/int-template.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use T = self::inst::T;
1212

1313
use from_str::FromStr;
1414
use num::{ToStrRadix, FromStrRadix};
15-
use num::strconv;
15+
use num::{Zero, One, strconv};
1616
use prelude::*;
1717

1818
pub use cmp::{min, max};
@@ -152,12 +152,15 @@ impl Eq for T {
152152
fn ne(&self, other: &T) -> bool { return (*self) != (*other); }
153153
}
154154
155-
impl num::Zero for T {
155+
impl Zero for T {
156156
#[inline(always)]
157157
fn zero() -> T { 0 }
158+
159+
#[inline(always)]
160+
fn is_zero(&self) -> bool { *self == 0 }
158161
}
159162
160-
impl num::One for T {
163+
impl One for T {
161164
#[inline(always)]
162165
fn one() -> T { 1 }
163166
}

src/libcore/num/num.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ pub trait IntConvertible {
3737
}
3838

3939
pub trait Zero {
40-
// FIXME (#5527): These should be associated constants
41-
fn zero() -> Self;
40+
fn zero() -> Self; // FIXME (#5527): This should be an associated constant
41+
fn is_zero(&self) -> bool;
4242
}
4343

4444
pub trait One {
45-
// FIXME (#5527): These should be associated constants
46-
fn one() -> Self;
45+
fn one() -> Self; // FIXME (#5527): This should be an associated constant
4746
}
4847

4948
pub trait Signed: Num

src/libcore/num/uint-template.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use T_SIGNED = self::inst::T_SIGNED;
1313

1414
use from_str::FromStr;
1515
use num::{ToStrRadix, FromStrRadix};
16-
use num::strconv;
16+
use num::{Zero, One, strconv};
1717
use prelude::*;
1818

1919
pub use cmp::{min, max};
@@ -118,12 +118,15 @@ impl Eq for T {
118118
fn ne(&self, other: &T) -> bool { return (*self) != (*other); }
119119
}
120120
121-
impl num::Zero for T {
121+
impl Zero for T {
122122
#[inline(always)]
123123
fn zero() -> T { 0 }
124+
125+
#[inline(always)]
126+
fn is_zero(&self) -> bool { *self == 0 }
124127
}
125128
126-
impl num::One for T {
129+
impl One for T {
127130
#[inline(always)]
128131
fn one() -> T { 1 }
129132
}

src/libstd/num/bigint.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,12 @@ impl Shr<uint, BigUint> for BigUint {
148148

149149
impl Zero for BigUint {
150150
fn zero() -> BigUint { BigUint::new(~[]) }
151+
152+
fn is_zero(&self) -> bool { self.data.is_empty() }
151153
}
152154

153155
impl One for BigUint {
154-
pub fn one() -> BigUint { BigUint::new(~[1]) }
156+
fn one() -> BigUint { BigUint::new(~[1]) }
155157
}
156158

157159
impl Unsigned for BigUint {}
@@ -310,7 +312,7 @@ impl ToStrRadix for BigUint {
310312
result += [m0.to_uint() as BigDigit];
311313
m = d;
312314
}
313-
if m.is_not_zero() {
315+
if !m.is_zero() {
314316
result += [m.to_uint() as BigDigit];
315317
}
316318
return result;
@@ -470,10 +472,6 @@ pub impl BigUint {
470472
self.div_mod(other)
471473
}
472474

473-
fn is_zero(&self) -> bool { self.data.is_empty() }
474-
475-
fn is_not_zero(&self) -> bool { !self.data.is_empty() }
476-
477475
fn to_uint(&self) -> uint {
478476
match self.data.len() {
479477
0 => 0,
@@ -684,6 +682,8 @@ impl Zero for BigInt {
684682
pub fn zero() -> BigInt {
685683
BigInt::from_biguint(Zero, Zero::zero())
686684
}
685+
686+
fn is_zero(&self) -> bool { self.sign == Zero }
687687
}
688688

689689
impl One for BigInt {
@@ -909,8 +909,6 @@ pub impl BigInt {
909909
910910
fn is_zero(&self) -> bool { self.sign == Zero }
911911
912-
fn is_not_zero(&self) -> bool { self.sign != Zero }
913-
914912
fn to_uint(&self) -> uint {
915913
match self.sign {
916914
Plus => self.data.to_uint(),
@@ -1212,10 +1210,10 @@ mod biguint_tests {
12121210
let b = BigUint::from_slice(bVec);
12131211
let c = BigUint::from_slice(cVec);
12141212

1215-
if a.is_not_zero() {
1213+
if !a.is_zero() {
12161214
assert!(c.quot_rem(&a) == (b, Zero::zero()));
12171215
}
1218-
if b.is_not_zero() {
1216+
if !b.is_zero() {
12191217
assert!(c.quot_rem(&b) == (a, Zero::zero()));
12201218
}
12211219
}
@@ -1227,7 +1225,7 @@ mod biguint_tests {
12271225
let c = BigUint::from_slice(cVec);
12281226
let d = BigUint::from_slice(dVec);
12291227

1230-
if b.is_not_zero() { assert!(a.quot_rem(&b) == (c, d)); }
1228+
if !b.is_zero() { assert!(a.quot_rem(&b) == (c, d)); }
12311229
}
12321230
}
12331231

@@ -1577,7 +1575,7 @@ mod bigint_tests {
15771575
fn test_div_mod() {
15781576
fn check_sub(a: &BigInt, b: &BigInt, ans_d: &BigInt, ans_m: &BigInt) {
15791577
let (d, m) = a.div_mod(b);
1580-
if m.is_not_zero() {
1578+
if !m.is_zero() {
15811579
assert!(m.sign == b.sign);
15821580
}
15831581
assert!(m.abs() <= b.abs());
@@ -1606,8 +1604,8 @@ mod bigint_tests {
16061604
let b = BigInt::from_slice(Plus, bVec);
16071605
let c = BigInt::from_slice(Plus, cVec);
16081606
1609-
if a.is_not_zero() { check(&c, &a, &b, &Zero::zero()); }
1610-
if b.is_not_zero() { check(&c, &b, &a, &Zero::zero()); }
1607+
if !a.is_zero() { check(&c, &a, &b, &Zero::zero()); }
1608+
if !b.is_zero() { check(&c, &b, &a, &Zero::zero()); }
16111609
}
16121610
16131611
for quot_rem_quadruples.each |elm| {
@@ -1617,7 +1615,7 @@ mod bigint_tests {
16171615
let c = BigInt::from_slice(Plus, cVec);
16181616
let d = BigInt::from_slice(Plus, dVec);
16191617
1620-
if b.is_not_zero() {
1618+
if !b.is_zero() {
16211619
check(&a, &b, &c, &d);
16221620
}
16231621
}
@@ -1628,7 +1626,7 @@ mod bigint_tests {
16281626
fn test_quot_rem() {
16291627
fn check_sub(a: &BigInt, b: &BigInt, ans_q: &BigInt, ans_r: &BigInt) {
16301628
let (q, r) = a.quot_rem(b);
1631-
if r.is_not_zero() {
1629+
if !r.is_zero() {
16321630
assert!(r.sign == a.sign);
16331631
}
16341632
assert!(r.abs() <= b.abs());
@@ -1649,8 +1647,8 @@ mod bigint_tests {
16491647
let b = BigInt::from_slice(Plus, bVec);
16501648
let c = BigInt::from_slice(Plus, cVec);
16511649
1652-
if a.is_not_zero() { check(&c, &a, &b, &Zero::zero()); }
1653-
if b.is_not_zero() { check(&c, &b, &a, &Zero::zero()); }
1650+
if !a.is_zero() { check(&c, &a, &b, &Zero::zero()); }
1651+
if !b.is_zero() { check(&c, &b, &a, &Zero::zero()); }
16541652
}
16551653
16561654
for quot_rem_quadruples.each |elm| {
@@ -1660,7 +1658,7 @@ mod bigint_tests {
16601658
let c = BigInt::from_slice(Plus, cVec);
16611659
let d = BigInt::from_slice(Plus, dVec);
16621660
1663-
if b.is_not_zero() {
1661+
if !b.is_zero() {
16641662
check(&a, &b, &c, &d);
16651663
}
16661664
}

src/libstd/num/complex.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ impl<T: Copy + Num> Zero for Cmplx<T> {
125125
fn zero() -> Cmplx<T> {
126126
Cmplx::new(Zero::zero(), Zero::zero())
127127
}
128+
129+
#[inline]
130+
fn is_zero(&self) -> bool {
131+
*self == Zero::zero()
132+
}
128133
}
129134

130135
impl<T: Copy + Num> One for Cmplx<T> {

src/libstd/num/rational.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,11 @@ impl<T: Copy + Num + Ord>
191191
fn zero() -> Ratio<T> {
192192
Ratio::new_raw(Zero::zero(), One::one())
193193
}
194+
195+
#[inline]
196+
fn is_zero(&self) -> bool {
197+
*self == Zero::zero()
198+
}
194199
}
195200
196201
impl<T: Copy + Num + Ord>

0 commit comments

Comments
 (0)