Skip to content

Commit

Permalink
update readme; impl approx in make_units
Browse files Browse the repository at this point in the history
  • Loading branch information
paholg committed Apr 11, 2017
1 parent 75b7067 commit d7c2d8f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Never again should you need to specify units in a comment!"""
[dependencies]
typenum = "1.6.0"
generic-array = "0.5.1"
clippy = { version = "0.0.118", optional = true }
clippy = { version = "0.0.122", optional = true }
quickcheck = { version = "0.4.1", optional = true }
quickcheck_macros = { version = "0.4.1", optional = true }
approx = { version = "0.1.1", optional = true, features = ["no_std"] }
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,22 @@ line number and hopefully the issue will be apparant from the code alone.

If there are any libraries that work particularly well with dimensioned, such as the vector3d
library used in part 3 of the simulation example, please let me know and they will be listed here.

# Known Bugs

If you are using dimensioned with a version of Rust < 1.17, then type annotations for computed
units may not work.

For example, this will work with any version of Rust that dimensioned supports:

```rust
use dim::si;
let area = 3.0 * si::M * si::M;
```

whereas this will only work with Rust >= 1.17:

```rust
use dim::si;
let area: si::Meter2<f64> = 3.0 * si::M * si::M;
```
10 changes: 3 additions & 7 deletions src/build/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt;
use dim::Dimensioned;
use approx::ApproxEq;

pub trait CmpConsts<B> {
fn test_eq(self, b: B);
Expand All @@ -11,13 +12,8 @@ impl<A, B> CmpConsts<B> for A {
}
}

impl<A, B> CmpConsts<B> for A where A: PartialEq + From<B> + fmt::Debug + Dimensioned<Value=f64> {
impl<A, B> CmpConsts<B> for A where A: From<B> + fmt::Debug + Dimensioned<Value=f64> + ApproxEq<Epsilon=Self> {
fn test_eq(self, b: B) {
let x = self.value_unsafe();
let temp = A::from(b);
let y = temp.value_unsafe();

assert_ulps_eq!(x, y, epsilon = 0.0, max_ulps = 2);
assert_relative_eq!(x, y, epsilon = 0.0);
assert_ulps_eq!(self, b.into(), epsilon = A::new(0.0), max_ulps = 2);
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ macro_rules! tarr {
#[macro_use]
pub extern crate generic_array;

#[cfg(feature = "approx")]
pub extern crate approx;

#[macro_use] mod make_units;
mod fmt;

Expand Down
31 changes: 29 additions & 2 deletions src/make_units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ macro_rules! make_units {
use $crate::dimcore::marker;
use $crate::{Dimensioned, Dimensionless};

/// The $System unit system
/// The struct for this unit system
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash)]
pub struct $System<V, U> {
/// This is the value of whatever type we're giving units. Using it directly bypasses
Expand Down Expand Up @@ -285,7 +285,7 @@ macro_rules! make_units {
// Define consts
macro_rules! define_consts {
($module:ident, $prefixes:ident, $t:ident) => (
/// Constants defined for $System of value type $t
/// Constants defined for this system
pub mod $module {
use super::*;
use $crate::dimcore::marker::PhantomData;
Expand Down Expand Up @@ -434,6 +434,33 @@ macro_rules! make_units {
}

// --------------------------------------------------------------------------------
// ApproxEq
#[cfg(feature = "approx")]
impl<V, U> $crate::approx::ApproxEq for $System<V, U> where V: $crate::approx::ApproxEq {
type Epsilon = $System<V::Epsilon, U>;

fn default_epsilon() -> Self::Epsilon {
$System::new(V::default_epsilon())
}
fn default_max_relative() -> Self::Epsilon {
$System::new(V::default_max_relative())
}
fn default_max_ulps() -> u32 {
V::default_max_ulps()
}
fn relative_eq(&self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool {
self.value_unsafe.relative_eq(&other.value_unsafe, epsilon.value_unsafe, max_relative.value_unsafe)
}
fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
self.value_unsafe.ulps_eq(&other.value_unsafe, epsilon.value_unsafe, max_ulps)
}
fn relative_ne(&self, other: &Self, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool {
self.value_unsafe.relative_ne(&other.value_unsafe, epsilon.value_unsafe, max_relative.value_unsafe)
}
fn ulps_ne(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
self.value_unsafe.ulps_ne(&other.value_unsafe, epsilon.value_unsafe, max_ulps)
}
}
// --------------------------------------------------------------------------------
);
}
Expand Down

0 comments on commit d7c2d8f

Please sign in to comment.