2014-01-25 15:54:54 +00:00
|
|
|
// Copyright 2014 The CGMath Developers. For a full listing of the authors,
|
2015-03-14 02:49:46 +00:00
|
|
|
// refer to the Cargo.toml file at the top-level directory of this distribution.
|
2014-01-09 00:26:50 +00:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-04-25 03:28:31 +00:00
|
|
|
use rust_num::{Float, NumCast};
|
|
|
|
use rust_num::traits::cast;
|
2014-01-09 00:26:50 +00:00
|
|
|
|
2015-11-03 03:00:39 +00:00
|
|
|
pub trait ApproxEq: Sized {
|
|
|
|
type Epsilon: NumCast + Float;
|
|
|
|
|
|
|
|
fn approx_epsilon() -> Self::Epsilon {
|
2015-04-05 01:19:11 +00:00
|
|
|
cast(1.0e-5f64).unwrap()
|
2014-01-09 00:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn approx_eq(&self, other: &Self) -> bool {
|
2015-11-03 03:00:39 +00:00
|
|
|
self.approx_eq_eps(other, &Self::approx_epsilon())
|
2014-01-09 00:26:50 +00:00
|
|
|
}
|
|
|
|
|
2015-11-03 03:00:39 +00:00
|
|
|
fn approx_eq_eps(&self, other: &Self, epsilon: &Self::Epsilon) -> bool;
|
2014-01-09 00:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-28 01:59:03 +00:00
|
|
|
macro_rules! approx_float(
|
2014-01-09 00:26:50 +00:00
|
|
|
($S:ident) => (
|
2015-11-03 03:00:39 +00:00
|
|
|
impl ApproxEq for $S {
|
|
|
|
type Epsilon = $S;
|
|
|
|
|
2014-01-09 00:26:50 +00:00
|
|
|
#[inline]
|
|
|
|
fn approx_eq_eps(&self, other: &$S, epsilon: &$S) -> bool {
|
2014-11-15 14:59:43 +00:00
|
|
|
(*self - *other).abs() < *epsilon
|
2014-01-09 00:26:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2014-12-19 17:48:59 +00:00
|
|
|
);
|
2014-01-09 00:26:50 +00:00
|
|
|
|
2014-12-19 17:48:59 +00:00
|
|
|
approx_float!(f32);
|
|
|
|
approx_float!(f64);
|
2014-10-23 08:15:47 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! assert_approx_eq_eps(
|
|
|
|
($given: expr, $expected: expr, $eps: expr) => ({
|
|
|
|
let eps = &($eps);
|
|
|
|
let (given_val, expected_val) = (&($given), &($expected));
|
|
|
|
if !given_val.approx_eq_eps(expected_val, eps) {
|
2015-01-09 22:06:45 +00:00
|
|
|
panic!("assertion failed: `left ≈ right` (left: `{:?}`, right: `{:?}`, tolerance: `{:?}`)",
|
2014-10-23 08:15:47 +00:00
|
|
|
*given_val, *expected_val, *eps
|
2014-12-19 17:48:59 +00:00
|
|
|
);
|
2014-10-23 08:15:47 +00:00
|
|
|
}
|
|
|
|
})
|
2014-12-19 17:48:59 +00:00
|
|
|
);
|
2014-10-23 08:15:47 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! assert_approx_eq(
|
|
|
|
($given: expr, $expected: expr) => ({
|
|
|
|
let (given_val, expected_val) = (&($given), &($expected));
|
|
|
|
if !given_val.approx_eq(expected_val) {
|
2015-11-03 03:00:39 +00:00
|
|
|
panic!("assertion failed: `left ≈ right` (left: `{:?}`, right: `{:?}`)",
|
|
|
|
*given_val, *expected_val
|
2014-12-19 17:48:59 +00:00
|
|
|
);
|
2014-10-23 08:15:47 +00:00
|
|
|
}
|
|
|
|
})
|
2014-12-19 17:48:59 +00:00
|
|
|
);
|