Skip to content

Commit

Permalink
Implement recip.
Browse files Browse the repository at this point in the history
Part of #11.
  • Loading branch information
iliekturtles committed May 28, 2017
1 parent ee01efe commit e0f355c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* [Breaking] A new feature, `std`, is now available and is enabled by default. `uom` can still be
compiled with `no_std` by using `--no-default-features` when compiling the crate or
`default-features = false` in the `dependencies` section of `Cargo.toml`
* [#11] `cbrt` and `sqrt` are implemented for `Quantity`.
* [#11] `cbrt`, `recip`, and `sqrt` are implemented for `Quantity`.

### Changed
* [#28](https://github.com/iliekturtles/uom/issues/28) `Quantity` fields made public in order to
Expand Down
26 changes: 26 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ macro_rules! system {

// Type aliases for dimensions where all exponents of the factors are the given value.
#[cfg(feature = "std")]
type DN1 = $quantities<$(replace_ty!($symbol $crate::typenum::N1)),+>;
#[cfg(feature = "std")]
type DP2 = $quantities<$(replace_ty!($symbol $crate::typenum::P2)),+>;
#[cfg(feature = "std")]
type DP3 = $quantities<$(replace_ty!($symbol $crate::typenum::P3)),+>;
Expand Down Expand Up @@ -463,6 +465,30 @@ macro_rules! system {
}
}

/// Takes the reciprocal (inverse) of a number, `1/x`.
///
/// ```
#[cfg_attr(feature = "f64", doc = " # use uom::si::f64::*;")]
#[cfg_attr(not(feature = "f64"), doc = " # use uom::si::f32::*;")]
/// # use uom::si::time::second;
/// // TODO #30 implement Frequency.
/// let f/*: Frequency*/ = Time::new::<second>(1.0).recip();
/// ```
#[cfg(feature = "std")]
#[inline(always)]
pub fn recip(self) ->
Quantity<<D as $crate::stdlib::ops::Mul<DN1>>::Output, U, $V>
where D: $crate::stdlib::ops::Mul<DN1>,
U: Units<<D as $crate::stdlib::ops::Mul<DN1>>::Output, $V>,
<D as $crate::stdlib::ops::Mul<DN1>>::Output: Dimension,
{
Quantity {
dimension: $crate::stdlib::marker::PhantomData,
units: $crate::stdlib::marker::PhantomData,
value: self.value.recip(),
}
}

/// Takes the square root of a number. Returns `NaN` if `self` is a negative
/// number.
///
Expand Down
14 changes: 14 additions & 0 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ mod system_macro {
}
}

quickcheck! {
#[cfg(feature = "std")]
#[allow(trivial_casts)]
fn recip(v: F) -> bool {
let a: Quantity<Q<N1, Z0>, U<F>, F> = Quantity::<Q<P1, Z0>, U<F>, F> {
dimension: ::stdlib::marker::PhantomData,
units: ::stdlib::marker::PhantomData,
value: v,
}.recip();

v.recip() == a.value
}
}

quickcheck! {
#[cfg(feature = "std")]
#[allow(trivial_casts)]
Expand Down

0 comments on commit e0f355c

Please sign in to comment.