Skip to content

Commit

Permalink
Merge pull request #9 from chalharu/release/0.1.2
Browse files Browse the repository at this point in the history
Release 0.1.2
  • Loading branch information
chalharu committed Oct 9, 2017
2 parents 8dcba7d + de545d9 commit 5f9b350
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 6 deletions.
10 changes: 5 additions & 5 deletions .travis.yml
Expand Up @@ -46,17 +46,17 @@ before_script:
fi
script:
- cargo build
- cargo build --features=num-complex
- |
if [ "${TRAVIS_OS_NAME}" = "linux" ]; then
cargo kcov --coveralls
cargo kcov --coveralls --features=num-complex
else
cargo test
cargo test --features=num-complex
fi
- cargo test --release
- cargo test --release --features=num-complex
- |
if [ "$TRAVIS_RUST_VERSION" = "nightly" ]; then
cargo bench --verbose
cargo bench --verbose --features=num-complex
fi
- |
if [ "${TRAVIS_OS_NAME}" = "linux" ]; then
Expand Down
6 changes: 5 additions & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "nearly_eq"
version = "0.1.1"
version = "0.1.2"
authors = ["Mitsuharu Seki <mitsu1986@gmail.com>"]
repository = "https://github.com/chalharu/rust-nearly-eq"
keywords = ["assert"]
Expand All @@ -13,3 +13,7 @@ documentation = "https://docs.rs/nearly_eq/"
name = "nearly_eq"
bench = false
test = true

[dependencies.num-complex]
optional = true
version = ">=0.0.0"
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -5,3 +5,14 @@
[![Coverage Status](https://coveralls.io/repos/github/chalharu/rust-nearly-eq/badge.svg)](https://coveralls.io/github/chalharu/rust-nearly-eq)

rust crate: nearly equal


## If you are using the num-complex crate
To depend on the `nearly_eq` crate with this feature enabled, put the following in
your project's `Cargo.toml` file:

```toml
[dependencies.nearly_eq]
features = ["num-complex"]
version = ... # Whichever version you are using
```
17 changes: 17 additions & 0 deletions src/complex.rs
@@ -0,0 +1,17 @@
//! # Licensing
//! This Source Code is subject to the terms of the Mozilla Public License
//! version 2.0 (the "License"). You can obtain a copy of the License at
//! http://mozilla.org/MPL/2.0/.

use num_complex::Complex;
use NearlyEq;

impl<A, B, C: NearlyEq<A, B>> NearlyEq<Complex<A>, B> for Complex<C> {
fn eps() -> B {
C::eps()
}

fn eq(&self, other: &Complex<A>, eps: &B) -> bool {
self.re.eq(&other.re, eps) && self.im.eq(&other.im, eps)
}
}
6 changes: 6 additions & 0 deletions src/lib.rs
Expand Up @@ -5,9 +5,15 @@
//! version 2.0 (the "License"). You can obtain a copy of the License at
//! http://mozilla.org/MPL/2.0/.

#[cfg(feature = "num-complex")]
extern crate num_complex;

#[macro_use]
mod assert;

#[cfg(feature = "num-complex")]
mod complex;

/// Trait for nearly equality comparisons.
pub trait NearlyEq<Rhs: ?Sized = Self, Diff: ?Sized = Self> {
fn eps() -> Diff;
Expand Down
24 changes: 24 additions & 0 deletions tests/lib.rs
@@ -1,6 +1,12 @@
#[cfg(feature = "num-complex")]
extern crate num_complex;

#[macro_use]
extern crate nearly_eq;

#[cfg(feature = "num-complex")]
use num_complex::Complex;

#[test]
fn it_should_not_panic_if_values_are_nearly_equal() {
assert_nearly_eq!(8f32, 8f32 + 1e-7);
Expand Down Expand Up @@ -88,3 +94,21 @@ fn bad_compare_with_array() {
let right = [1f32, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
assert_nearly_eq!(left, right);
}


#[test]
#[cfg(feature = "num-complex")]
fn compare_with_complex() {
let left = Complex::new(1.0f64, 0.0);
let right = Complex::new(1.0f64, 1e-12);
assert_nearly_eq!(left, right);
}

#[test]
#[should_panic]
#[cfg(feature = "num-complex")]
fn bad_compare_with_complex() {
let left = Complex::new(1.0f64, 0.0);
let right = Complex::new(1.0f64, 1e-8);
assert_nearly_eq!(left, right);
}

0 comments on commit 5f9b350

Please sign in to comment.