diff options
| author | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-01-08 22:57:31 +1100 |
|---|---|---|
| committer | Brendan Zabarauskas <bjzaba@yahoo.com.au> | 2014-01-09 15:41:46 +1100 |
| commit | ceea85a148ec3426edfc00b8353a19c1d2df5dbf (patch) | |
| tree | 34005cf06fde432a44cff41aca0a6603b12cf3a4 /src/libstd/num | |
| parent | 7613b15fdbbb9bf770a2c731f4135886b0ff3cf0 (diff) | |
| download | rust-ceea85a148ec3426edfc00b8353a19c1d2df5dbf.tar.gz rust-ceea85a148ec3426edfc00b8353a19c1d2df5dbf.zip | |
Remove ApproxEq and assert_approx_eq!
This trait seems to stray too far from the mandate of a standard library as implementations may vary between use cases.
Diffstat (limited to 'src/libstd/num')
| -rw-r--r-- | src/libstd/num/f32.rs | 25 | ||||
| -rw-r--r-- | src/libstd/num/f64.rs | 25 | ||||
| -rw-r--r-- | src/libstd/num/float_macros.rs | 20 | ||||
| -rw-r--r-- | src/libstd/num/mod.rs | 59 |
4 files changed, 23 insertions, 106 deletions
diff --git a/src/libstd/num/f32.rs b/src/libstd/num/f32.rs index 43b0235c5f4..b9814b0f5a1 100644 --- a/src/libstd/num/f32.rs +++ b/src/libstd/num/f32.rs @@ -167,22 +167,6 @@ impl Eq for f32 { } #[cfg(not(test))] -impl ApproxEq<f32> for f32 { - #[inline] - fn approx_epsilon() -> f32 { 1.0e-6 } - - #[inline] - fn approx_eq(&self, other: &f32) -> bool { - self.approx_eq_eps(other, &1.0e-6) - } - - #[inline] - fn approx_eq_eps(&self, other: &f32, approx_epsilon: &f32) -> bool { - (*self - *other).abs() < *approx_epsilon - } -} - -#[cfg(not(test))] impl Ord for f32 { #[inline] fn lt(&self, other: &f32) -> bool { (*self) < (*other) } @@ -1196,15 +1180,6 @@ mod tests { } #[test] - fn test_approx_eq() { - assert!(1.0f32.approx_eq(&1f32)); - assert!(0.9999999f32.approx_eq(&1f32)); - assert!(1.000001f32.approx_eq_eps(&1f32, &1.0e-5)); - assert!(1.0000001f32.approx_eq_eps(&1f32, &1.0e-6)); - assert!(!1.0000001f32.approx_eq_eps(&1f32, &1.0e-7)); - } - - #[test] fn test_primitive() { let none: Option<f32> = None; assert_eq!(Primitive::bits(none), mem::size_of::<f32>() * 8); diff --git a/src/libstd/num/f64.rs b/src/libstd/num/f64.rs index c2d19d41215..86259f5af5d 100644 --- a/src/libstd/num/f64.rs +++ b/src/libstd/num/f64.rs @@ -190,22 +190,6 @@ impl Eq for f64 { } #[cfg(not(test))] -impl ApproxEq<f64> for f64 { - #[inline] - fn approx_epsilon() -> f64 { 1.0e-6 } - - #[inline] - fn approx_eq(&self, other: &f64) -> bool { - self.approx_eq_eps(other, &1.0e-6) - } - - #[inline] - fn approx_eq_eps(&self, other: &f64, approx_epsilon: &f64) -> bool { - (*self - *other).abs() < *approx_epsilon - } -} - -#[cfg(not(test))] impl Ord for f64 { #[inline] fn lt(&self, other: &f64) -> bool { (*self) < (*other) } @@ -1247,15 +1231,6 @@ mod tests { } #[test] - fn test_approx_eq() { - assert!(1.0f64.approx_eq(&1f64)); - assert!(0.9999999f64.approx_eq(&1f64)); - assert!(1.000001f64.approx_eq_eps(&1f64, &1.0e-5)); - assert!(1.0000001f64.approx_eq_eps(&1f64, &1.0e-6)); - assert!(!1.0000001f64.approx_eq_eps(&1f64, &1.0e-7)); - } - - #[test] fn test_primitive() { let none: Option<f64> = None; assert_eq!(Primitive::bits(none), mem::size_of::<f64>() * 8); diff --git a/src/libstd/num/float_macros.rs b/src/libstd/num/float_macros.rs new file mode 100644 index 00000000000..7c93602af11 --- /dev/null +++ b/src/libstd/num/float_macros.rs @@ -0,0 +1,20 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#[macro_escape]; +#[doc(hidden)]; + +macro_rules! assert_approx_eq( + ($a:expr, $b:expr) => ({ + let (a, b) = (&$a, &$b); + assert!((*a - *b).abs() < 1.0e-6, + "{} is not approximately equal to {}", *a, *b); + }) +) diff --git a/src/libstd/num/mod.rs b/src/libstd/num/mod.rs index d66d13657fc..8a72f5cbd77 100644 --- a/src/libstd/num/mod.rs +++ b/src/libstd/num/mod.rs @@ -16,7 +16,7 @@ #[allow(missing_doc)]; use clone::{Clone, DeepClone}; -use cmp::{Eq, ApproxEq, Ord}; +use cmp::{Eq, Ord}; use ops::{Add, Sub, Mul, Div, Rem, Neg}; use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr}; use option::{Option, Some, None}; @@ -138,60 +138,19 @@ pub trait Integer: Num /// A collection of rounding operations. pub trait Round { /// Return the largest integer less than or equal to a number. - /// - /// # Example - /// - /// ```rust - /// assert_approx_eq!(1.3f32.floor(), 1.0); - /// assert_approx_eq!((-1.3f32).floor(), -2.0); - /// ``` fn floor(&self) -> Self; /// Return the smallest integer greater than or equal to a number. - /// - /// # Example - /// - /// ```rust - /// assert_approx_eq!(1.3f32.ceil(), 2.0); - /// assert_approx_eq!((-1.3f32).ceil(), -1.0); - /// ``` fn ceil(&self) -> Self; /// Return the nearest integer to a number. Round half-way cases away from /// `0.0`. - /// - /// # Example - /// - /// ```rust - /// assert_approx_eq!(1.3f32.round(), 1.0); - /// assert_approx_eq!((-1.3f32).round(), -1.0); - /// assert_approx_eq!(1.5f32.round(), 2.0); - /// assert_approx_eq!((-1.5f32).round(), -2.0); - /// ``` fn round(&self) -> Self; /// Return the integer part of a number. - /// - /// # Example - /// - /// ```rust - /// assert_approx_eq!(1.3f32.trunc(), 1.0); - /// assert_approx_eq!((-1.3f32).trunc(), -1.0); - /// assert_approx_eq!(1.5f32.trunc(), 1.0); - /// assert_approx_eq!((-1.5f32).trunc(), -1.0); - /// ``` fn trunc(&self) -> Self; /// Return the fractional part of a number. - /// - /// # Example - /// - /// ```rust - /// assert_approx_eq!(1.3f32.fract(), 0.3); - /// assert_approx_eq!((-1.3f32).fract(), -0.3); - /// assert_approx_eq!(1.5f32.fract(), 0.5); - /// assert_approx_eq!((-1.5f32).fract(), -0.5); - /// ``` fn fract(&self) -> Self; } @@ -262,18 +221,7 @@ pub trait Trigonometric { fn atan(&self) -> Self; /// Computes the four quadrant arctangent of a number, `y`, and another - /// number `x`. Return value is in radians in the range [-pi, pi]; - /// - /// # Example - /// - /// ```rust - /// use std::f32; - /// - /// let y = 3f32.sqrt(); - /// let x = 1f32; - /// assert_approx_eq!(y.atan2(&x), f32::consts::PI / 3f32); - /// assert_approx_eq!((-y).atan2(&(-x)), - 2f32 * f32::consts::PI / 3f32); - /// ``` + /// number `x`. Return value is in radians in the range [-pi, pi]. fn atan2(&self, other: &Self) -> Self; /// Simultaneously computes the sine and cosine of the number, `x`. Returns @@ -505,8 +453,7 @@ pub enum FPCategory { /// Primitive floating point numbers pub trait Float: Real + Signed - + Primitive - + ApproxEq<Self> { + + Primitive { // FIXME (#5527): These should be associated constants fn nan() -> Self; fn infinity() -> Self; |
