Skip to content

Commit

Permalink
Implement floating point powi method.
Browse files Browse the repository at this point in the history
Part of #11.
  • Loading branch information
iliekturtles committed Jun 18, 2017
1 parent c40097e commit efdc9de
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
`max` and `min` for `Quantity`.
* [#11](https://github.com/iliekturtles/uom/issues/11) Add floating point `mul_add` method for
`Quantity`.
* [#11](https://github.com/iliekturtles/uom/issues/11) Add floating point `powi` method for
`Quantity`.

## [v0.14.0] — 2017-05-30

Expand Down
25 changes: 25 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,31 @@ macro_rules! system {
}
}

/// Raises a quantity to an integer power.
///
/// ```
#[cfg_attr(feature = "f32", doc = " # use uom::si::f32::*;")]
#[cfg_attr(not(feature = "f32"), doc = " # use uom::si::f64::*;")]
/// # use uom::si::length::meter;
/// let a: Area = Length::new::<meter>(3.0).powi(::uom::typenum::P2::new());
/// ```
#[cfg(feature = "std")]
#[inline(always)]
pub fn powi<E>(self, _e: E) -> Quantity<$crate::typenum::Prod<D, DN<E>>, U, $V>
where
D: $crate::stdlib::ops::Mul<DN<E>>,
U: Units<$crate::typenum::Prod<D, DN<E>>, $V>,
E: $crate::typenum::Integer,
$crate::typenum::Prod<D, DN<E>>: Dimension,
{
Quantity {
dimension: $crate::stdlib::marker::PhantomData,
units: $crate::stdlib::marker::PhantomData,
// TODO #29 value: self.value.powi(e), // if $V becomes V.
value: self.value.powi(E::to_i32()),
}
}

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

quickcheck! {
#[cfg(feature = "std")]
#[allow(trivial_casts)]
fn powi(v: $V) -> bool {
v.powi(3) == TLength::new::<meter>(v).powi(P3::new()).value
}
}

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

0 comments on commit efdc9de

Please sign in to comment.