Skip to content

Commit

Permalink
Merge 4050629 into 6191c9d
Browse files Browse the repository at this point in the history
  • Loading branch information
chalharu committed Sep 9, 2019
2 parents 6191c9d + 4050629 commit d7fd24f
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 168 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -8,3 +8,4 @@ Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
.DS_Store
3 changes: 2 additions & 1 deletion Cargo.toml
@@ -1,13 +1,14 @@
[package]
name = "appro-eq"
version = "0.2.2"
version = "0.3.0"
authors = ["Mitsuharu Seki <mitsu1986@gmail.com>"]
repository = "https://github.com/chalharu/rust-appro-eq"
keywords = ["assert", "array"]
license = "MPL-2.0"
readme = "README.md"
description = "Approximately equal traits and assertion"
documentation = "https://docs.rs/appro-eq/"
edition = "2018"

[lib]
name = "appro_eq"
Expand Down
83 changes: 52 additions & 31 deletions src/assert.rs
Expand Up @@ -27,16 +27,23 @@
#[macro_export]
#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.1.0"))]
macro_rules! assert_appro_eq {
($a:expr, $b:expr) => ({
assert!($crate::AbsApproEq::abs_appro_eq(&$a, &$b),
"assertion failed: `(left == right)` (left: `{:?}` , right: `{:?}`)",
$a, $b);
});
($a:expr, $b:expr, $eps:expr) => ({
assert!($crate::AbsApproEqWithTol::abs_appro_eq_with_tol(&$a, &$b, &$eps),
"assertion failed: `(left == right)` (left: `{:?}` , right: `{:?}`, eps: `{:?}`)",
$a, $b, $eps);
})
($a:expr, $b:expr) => {{
assert!(
$crate::AbsApproEq::abs_appro_eq(&$a, &$b),
"assertion failed: `(left == right)` (left: `{:?}` , right: `{:?}`)",
$a,
$b
);
}};
($a:expr, $b:expr, $eps:expr) => {{
assert!(
$crate::AbsApproEqWithTol::abs_appro_eq_with_tol(&$a, &$b, &$eps),
"assertion failed: `(left == right)` (left: `{:?}` , right: `{:?}`, eps: `{:?}`)",
$a,
$b,
$eps
);
}};
}

/// Asserts that the absolute error of the two expressions is small enough.
Expand All @@ -63,16 +70,23 @@ macro_rules! assert_appro_eq {
#[macro_export]
#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.1.0"))]
macro_rules! assert_appro_eq_abs {
($a:expr, $b:expr) => ({
assert!($crate::AbsApproEq::abs_appro_eq(&$a, &$b),
"assertion failed: `(left == right)` (left: `{:?}` , right: `{:?}`)",
$a, $b);
});
($a:expr, $b:expr, $eps:expr) => ({
assert!($crate::AbsApproEqWithTol::abs_appro_eq_with_tol(&$a, &$b, &$eps),
"assertion failed: `(left == right)` (left: `{:?}` , right: `{:?}`, eps: `{:?}`)",
$a, $b, $eps);
})
($a:expr, $b:expr) => {{
assert!(
$crate::AbsApproEq::abs_appro_eq(&$a, &$b),
"assertion failed: `(left == right)` (left: `{:?}` , right: `{:?}`)",
$a,
$b
);
}};
($a:expr, $b:expr, $eps:expr) => {{
assert!(
$crate::AbsApproEqWithTol::abs_appro_eq_with_tol(&$a, &$b, &$eps),
"assertion failed: `(left == right)` (left: `{:?}` , right: `{:?}`, eps: `{:?}`)",
$a,
$b,
$eps
);
}};
}

/// Asserts that the relative error of the two expressions is small enough.
Expand All @@ -99,16 +113,23 @@ macro_rules! assert_appro_eq_abs {
#[macro_export]
#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.1.0"))]
macro_rules! assert_appro_eq_rel {
($a:expr, $b:expr) => ({
assert!($crate::RelApproEq::rel_appro_eq(&$a, &$b),
"assertion failed: `(left == right)` (left: `{:?}` , right: `{:?}`)",
$a, $b);
});
($a:expr, $b:expr, $eps:expr) => ({
assert!($crate::RelApproEqWithTol::rel_appro_eq_with_tol(&$a, &$b, &$eps),
"assertion failed: `(left == right)` (left: `{:?}` , right: `{:?}`, eps: `{:?}`)",
$a, $b, $eps);
})
($a:expr, $b:expr) => {{
assert!(
$crate::RelApproEq::rel_appro_eq(&$a, &$b),
"assertion failed: `(left == right)` (left: `{:?}` , right: `{:?}`)",
$a,
$b
);
}};
($a:expr, $b:expr, $eps:expr) => {{
assert!(
$crate::RelApproEqWithTol::rel_appro_eq_with_tol(&$a, &$b, &$eps),
"assertion failed: `(left == right)` (left: `{:?}` , right: `{:?}`, eps: `{:?}`)",
$a,
$b,
$eps
);
}};
}

/// Asserts that two expressions are approximately equal to each other.
Expand Down Expand Up @@ -199,4 +220,4 @@ macro_rules! debug_assert_appro_eq_abs {
#[cfg_attr(feature = "docs", stable(feature = "default", since = "0.1.0"))]
macro_rules! debug_assert_appro_eq_rel {
($($arg:tt)*) => (if cfg!(debug_assertions) { assert_appro_eq_rel!($($arg)*); })
}
}
15 changes: 8 additions & 7 deletions src/complex_impl.rs
Expand Up @@ -3,11 +3,11 @@
//! version 2.0 (the "License"). You can obtain a copy of the License at
//! http://mozilla.org/MPL/2.0/.

use crate::AbsError;
use crate::ApproEqResult;
use crate::RelError;
use num_complex::Complex;
use num_traits::Float;
use AbsError;
use RelError;
use ApproEqResult;
use std::ops::{Div, Sub};

#[cfg_attr(feature = "docs", stable(feature = "num-complex", since = "0.1.0"))]
Expand All @@ -22,9 +22,9 @@ impl<A, D: Float, B: AbsError<A, D>> AbsError<Complex<A>, D> for Complex<B> {
(&None, _) => true,
(_, &None) => false,
(&Some(ref diff_re), &Some(ref diff_im)) => {
return Ok(Some(
Float::sqrt((*diff_re * *diff_re) + (*diff_im * *diff_im)),
))
return Ok(Some(Float::sqrt(
(*diff_re * *diff_re) + (*diff_im * *diff_im),
)))
}
},
} {
Expand All @@ -37,7 +37,8 @@ impl<A, D: Float, B: AbsError<A, D>> AbsError<Complex<A>, D> for Complex<B> {

#[cfg_attr(feature = "docs", stable(feature = "num-complex", since = "0.1.0"))]
impl<A: Float, D: Float + Div<A, Output = D>, B: Sub<A, Output = D> + Copy> RelError<Complex<A>, D>
for Complex<B> {
for Complex<B>
{
fn rel_error(&self, expected: &Complex<A>) -> ApproEqResult<D> {
Ok(Some(
Complex::new(self.re - expected.re, self.im - expected.im).norm() / expected.norm(),
Expand Down

0 comments on commit d7fd24f

Please sign in to comment.