Skip to content

Commit ab9ec6d

Browse files
committed
auto merge of #11402 : bjz/rust/remove-approx, r=alexcrichton
This trait seems to stray too far from the mandate of a standard library as implementations may vary between use cases. Third party libraries should implement their own if they need something like it. This closes #5316. r? @alexcrichton, @pcwalton
2 parents fb44e20 + ceea85a commit ab9ec6d

18 files changed

+164
-299
lines changed

doc/guide-testing.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ value. To run the tests in a crate, it must be compiled with the
4848
the resulting executable will run all the tests in the crate. A test
4949
is considered successful if its function returns; if the task running
5050
the test fails, through a call to `fail!`, a failed `check` or
51-
`assert`, or some other (`assert_eq`, `assert_approx_eq`, ...) means,
52-
then the test fails.
51+
`assert`, or some other (`assert_eq`, ...) means, then the test fails.
5352

5453
When compiling a crate with the '--test' flag '--cfg test' is also
5554
implied, so that tests can be conditionally compiled.

src/etc/vim/syntax/rust.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ syn keyword rustTrait Bool
7171
syn keyword rustTrait ToCStr
7272
syn keyword rustTrait Char
7373
syn keyword rustTrait Clone DeepClone
74-
syn keyword rustTrait Eq ApproxEq Ord TotalEq TotalOrd Ordering Equiv
74+
syn keyword rustTrait Eq Ord TotalEq TotalOrd Ordering Equiv
7575
syn keyword rustEnumVariant Less Equal Greater
7676
syn keyword rustTrait Container Mutable Map MutableMap Set MutableSet
7777
syn keyword rustTrait Default

src/libextra/num/complex.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ mod test {
268268
#[test]
269269
fn test_arg() {
270270
fn test(c: Complex64, arg: f64) {
271-
assert!(c.arg().approx_eq(&arg))
271+
assert!((c.arg() - arg).abs() < 1.0e-6)
272272
}
273273
test(_1_0i, 0.0);
274274
test(_1_1i, 0.25 * Real::pi());

src/libextra/stats.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,14 @@ mod tests {
439439
use std::io;
440440
use std::str;
441441

442+
macro_rules! assert_approx_eq(
443+
($a:expr, $b:expr) => ({
444+
let (a, b) = (&$a, &$b);
445+
assert!((*a - *b).abs() < 1.0e-6,
446+
"{} is not approximately equal to {}", *a, *b);
447+
})
448+
)
449+
442450
fn check(samples: &[f64], summ: &Summary) {
443451

444452
let summ2 = Summary::new(samples);

src/libstd/cmp.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,6 @@ totaleq_impl!(uint)
7272

7373
totaleq_impl!(char)
7474

75-
/// Trait for testing approximate equality
76-
pub trait ApproxEq<Eps> {
77-
fn approx_epsilon() -> Eps;
78-
fn approx_eq(&self, other: &Self) -> bool;
79-
fn approx_eq_eps(&self, other: &Self, approx_epsilon: &Eps) -> bool;
80-
}
81-
8275
#[deriving(Clone, Eq)]
8376
pub enum Ordering { Less = -1, Equal = 0, Greater = 1 }
8477

src/libstd/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ pub mod prelude;
8585

8686
/* Primitive types */
8787

88+
#[path = "num/float_macros.rs"] mod float_macros;
8889
#[path = "num/int_macros.rs"] mod int_macros;
8990
#[path = "num/uint_macros.rs"] mod uint_macros;
9091

src/libstd/num/f32.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,6 @@ impl Eq for f32 {
166166
fn eq(&self, other: &f32) -> bool { (*self) == (*other) }
167167
}
168168

169-
#[cfg(not(test))]
170-
impl ApproxEq<f32> for f32 {
171-
#[inline]
172-
fn approx_epsilon() -> f32 { 1.0e-6 }
173-
174-
#[inline]
175-
fn approx_eq(&self, other: &f32) -> bool {
176-
self.approx_eq_eps(other, &1.0e-6)
177-
}
178-
179-
#[inline]
180-
fn approx_eq_eps(&self, other: &f32, approx_epsilon: &f32) -> bool {
181-
(*self - *other).abs() < *approx_epsilon
182-
}
183-
}
184-
185169
#[cfg(not(test))]
186170
impl Ord for f32 {
187171
#[inline]
@@ -1195,15 +1179,6 @@ mod tests {
11951179
assert!(!NAN.is_negative());
11961180
}
11971181

1198-
#[test]
1199-
fn test_approx_eq() {
1200-
assert!(1.0f32.approx_eq(&1f32));
1201-
assert!(0.9999999f32.approx_eq(&1f32));
1202-
assert!(1.000001f32.approx_eq_eps(&1f32, &1.0e-5));
1203-
assert!(1.0000001f32.approx_eq_eps(&1f32, &1.0e-6));
1204-
assert!(!1.0000001f32.approx_eq_eps(&1f32, &1.0e-7));
1205-
}
1206-
12071182
#[test]
12081183
fn test_primitive() {
12091184
let none: Option<f32> = None;

src/libstd/num/f64.rs

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,6 @@ impl Eq for f64 {
189189
fn eq(&self, other: &f64) -> bool { (*self) == (*other) }
190190
}
191191

192-
#[cfg(not(test))]
193-
impl ApproxEq<f64> for f64 {
194-
#[inline]
195-
fn approx_epsilon() -> f64 { 1.0e-6 }
196-
197-
#[inline]
198-
fn approx_eq(&self, other: &f64) -> bool {
199-
self.approx_eq_eps(other, &1.0e-6)
200-
}
201-
202-
#[inline]
203-
fn approx_eq_eps(&self, other: &f64, approx_epsilon: &f64) -> bool {
204-
(*self - *other).abs() < *approx_epsilon
205-
}
206-
}
207-
208192
#[cfg(not(test))]
209193
impl Ord for f64 {
210194
#[inline]
@@ -1246,15 +1230,6 @@ mod tests {
12461230
assert!(!NAN.is_negative());
12471231
}
12481232

1249-
#[test]
1250-
fn test_approx_eq() {
1251-
assert!(1.0f64.approx_eq(&1f64));
1252-
assert!(0.9999999f64.approx_eq(&1f64));
1253-
assert!(1.000001f64.approx_eq_eps(&1f64, &1.0e-5));
1254-
assert!(1.0000001f64.approx_eq_eps(&1f64, &1.0e-6));
1255-
assert!(!1.0000001f64.approx_eq_eps(&1f64, &1.0e-7));
1256-
}
1257-
12581233
#[test]
12591234
fn test_primitive() {
12601235
let none: Option<f64> = None;
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -8,7 +8,13 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// error-pattern:left: 1.0000001f64 does not approximately equal right: 1f64 with epsilon: 0.0000001f64
12-
pub fn main() {
13-
assert_approx_eq!(1.0000001f64, 1.0f64, 1.0e-7);
14-
}
11+
#[macro_escape];
12+
#[doc(hidden)];
13+
14+
macro_rules! assert_approx_eq(
15+
($a:expr, $b:expr) => ({
16+
let (a, b) = (&$a, &$b);
17+
assert!((*a - *b).abs() < 1.0e-6,
18+
"{} is not approximately equal to {}", *a, *b);
19+
})
20+
)

src/libstd/num/mod.rs

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#[allow(missing_doc)];
1717

1818
use clone::{Clone, DeepClone};
19-
use cmp::{Eq, ApproxEq, Ord};
19+
use cmp::{Eq, Ord};
2020
use ops::{Add, Sub, Mul, Div, Rem, Neg};
2121
use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
2222
use option::{Option, Some, None};
@@ -138,60 +138,19 @@ pub trait Integer: Num
138138
/// A collection of rounding operations.
139139
pub trait Round {
140140
/// Return the largest integer less than or equal to a number.
141-
///
142-
/// # Example
143-
///
144-
/// ```rust
145-
/// assert_approx_eq!(1.3f32.floor(), 1.0);
146-
/// assert_approx_eq!((-1.3f32).floor(), -2.0);
147-
/// ```
148141
fn floor(&self) -> Self;
149142

150143
/// Return the smallest integer greater than or equal to a number.
151-
///
152-
/// # Example
153-
///
154-
/// ```rust
155-
/// assert_approx_eq!(1.3f32.ceil(), 2.0);
156-
/// assert_approx_eq!((-1.3f32).ceil(), -1.0);
157-
/// ```
158144
fn ceil(&self) -> Self;
159145

160146
/// Return the nearest integer to a number. Round half-way cases away from
161147
/// `0.0`.
162-
///
163-
/// # Example
164-
///
165-
/// ```rust
166-
/// assert_approx_eq!(1.3f32.round(), 1.0);
167-
/// assert_approx_eq!((-1.3f32).round(), -1.0);
168-
/// assert_approx_eq!(1.5f32.round(), 2.0);
169-
/// assert_approx_eq!((-1.5f32).round(), -2.0);
170-
/// ```
171148
fn round(&self) -> Self;
172149

173150
/// Return the integer part of a number.
174-
///
175-
/// # Example
176-
///
177-
/// ```rust
178-
/// assert_approx_eq!(1.3f32.trunc(), 1.0);
179-
/// assert_approx_eq!((-1.3f32).trunc(), -1.0);
180-
/// assert_approx_eq!(1.5f32.trunc(), 1.0);
181-
/// assert_approx_eq!((-1.5f32).trunc(), -1.0);
182-
/// ```
183151
fn trunc(&self) -> Self;
184152

185153
/// Return the fractional part of a number.
186-
///
187-
/// # Example
188-
///
189-
/// ```rust
190-
/// assert_approx_eq!(1.3f32.fract(), 0.3);
191-
/// assert_approx_eq!((-1.3f32).fract(), -0.3);
192-
/// assert_approx_eq!(1.5f32.fract(), 0.5);
193-
/// assert_approx_eq!((-1.5f32).fract(), -0.5);
194-
/// ```
195154
fn fract(&self) -> Self;
196155
}
197156

@@ -262,18 +221,7 @@ pub trait Trigonometric {
262221
fn atan(&self) -> Self;
263222

264223
/// Computes the four quadrant arctangent of a number, `y`, and another
265-
/// number `x`. Return value is in radians in the range [-pi, pi];
266-
///
267-
/// # Example
268-
///
269-
/// ```rust
270-
/// use std::f32;
271-
///
272-
/// let y = 3f32.sqrt();
273-
/// let x = 1f32;
274-
/// assert_approx_eq!(y.atan2(&x), f32::consts::PI / 3f32);
275-
/// assert_approx_eq!((-y).atan2(&(-x)), - 2f32 * f32::consts::PI / 3f32);
276-
/// ```
224+
/// number `x`. Return value is in radians in the range [-pi, pi].
277225
fn atan2(&self, other: &Self) -> Self;
278226

279227
/// Simultaneously computes the sine and cosine of the number, `x`. Returns
@@ -505,8 +453,7 @@ pub enum FPCategory {
505453
/// Primitive floating point numbers
506454
pub trait Float: Real
507455
+ Signed
508-
+ Primitive
509-
+ ApproxEq<Self> {
456+
+ Primitive {
510457
// FIXME (#5527): These should be associated constants
511458
fn nan() -> Self;
512459
fn infinity() -> Self;

src/libstd/prelude.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub use bool::Bool;
5050
pub use c_str::ToCStr;
5151
pub use char::Char;
5252
pub use clone::{Clone, DeepClone};
53-
pub use cmp::{Eq, ApproxEq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater, Equiv};
53+
pub use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering, Less, Equal, Greater, Equiv};
5454
pub use container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
5555
pub use default::Default;
5656
pub use from_str::FromStr;

src/libsyntax/ext/expand.rs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -741,43 +741,6 @@ pub fn std_macros() -> @str {
741741
)
742742
)
743743
744-
macro_rules! assert_approx_eq (
745-
($given:expr , $expected:expr) => (
746-
{
747-
use std::cmp::ApproxEq;
748-
749-
let given_val = $given;
750-
let expected_val = $expected;
751-
// check both directions of equality....
752-
if !(
753-
given_val.approx_eq(&expected_val) &&
754-
expected_val.approx_eq(&given_val)
755-
) {
756-
fail!("left: {:?} does not approximately equal right: {:?}",
757-
given_val, expected_val);
758-
}
759-
}
760-
);
761-
($given:expr , $expected:expr , $epsilon:expr) => (
762-
{
763-
use std::cmp::ApproxEq;
764-
765-
let given_val = $given;
766-
let expected_val = $expected;
767-
let epsilon_val = $epsilon;
768-
// check both directions of equality....
769-
if !(
770-
given_val.approx_eq_eps(&expected_val, &epsilon_val) &&
771-
expected_val.approx_eq_eps(&given_val, &epsilon_val)
772-
) {
773-
fail!("left: {:?} does not approximately equal right: \
774-
{:?} with epsilon: {:?}",
775-
given_val, expected_val, epsilon_val);
776-
}
777-
}
778-
)
779-
)
780-
781744
/// A utility macro for indicating unreachable code. It will fail if
782745
/// executed. This is occasionally useful to put after loops that never
783746
/// terminate normally, but instead directly return from a function.

src/test/run-fail/assert-approx-eq-macro-fail.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/test/run-pass/assert-approx-eq-macro-success.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)