Skip to content

Commit

Permalink
Implement sqrt.
Browse files Browse the repository at this point in the history
Add `Dimension` overloads for `Mul` and `PartialDiv` (requires result
have no remainder) so that the return type of `sqrt` has the appropriate
dimensions. Part of #11.
  • Loading branch information
iliekturtles committed May 28, 2017
1 parent 8d2f8b0 commit 40e58f6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +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] `sqrt` is implemented for `Quantity`.

### Changed
* [#28](https://github.com/iliekturtles/uom/issues/28) `Quantity` fields made public in order to
Expand Down
33 changes: 33 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ macro_rules! system {
/// [base]: http://jcgm.bipm.org/vim/en/1.4.html
pub type One = $quantities<$(replace_ty!($symbol $crate::typenum::Z0)),+>;

// Type aliases for dimensions where all exponents of the factors are the given value.
#[cfg(feature = "std")]
type DP2 = $quantities<$(replace_ty!($symbol $crate::typenum::P2)),+>;

#[allow(non_camel_case_types)]
impl<$($name,)+ $($symbol,)+ V> $crate::stdlib::fmt::Debug
for Quantity<$quantities<$($name),+>, BaseUnits<$($symbol,)+ V>, V>
Expand Down Expand Up @@ -423,6 +427,35 @@ macro_rules! system {
#[doc(hidden)]
macro_rules! impl_units {
($V:ty) => {
impl<D, U> Quantity<D, U, $V>
where D: Dimension,
U: Units<D, $V>,
{
/// Takes the square root of a number. Returns `NaN` if `self` is a negative
/// number.
///
/// ```
#[cfg_attr(feature = "f64", doc = " # use uom::si::f64::*;")]
#[cfg_attr(not(feature = "f64"), doc = " # use uom::si::f32::*;")]
/// # use uom::si::area::square_meter;
/// let l: Length = Area::new::<square_meter>(4.0).sqrt();
/// ```
#[cfg(feature = "std")]
#[inline(always)]
pub fn sqrt(self) ->
Quantity<<D as $crate::typenum::type_operators::PartialDiv<DP2>>::Output, U, $V>
where D: $crate::typenum::type_operators::PartialDiv<DP2>,
U: Units<<D as $crate::typenum::type_operators::PartialDiv<DP2>>::Output, $V>,
<D as $crate::typenum::type_operators::PartialDiv<DP2>>::Output: Dimension,
{
Quantity {
dimension: $crate::stdlib::marker::PhantomData,
units: $crate::stdlib::marker::PhantomData,
value: self.value.sqrt(),
}
}
}

#[allow(non_camel_case_types)]
impl<$($name,)+ $($symbol),+> Units<$quantities<$($name),+>, $V>
for BaseUnits<$($symbol,)+ $V>
Expand Down
24 changes: 22 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,28 @@ mod system_macro {
use super::f::*;
use super::length::{kilometer, meter};
use super::mass::kilogram;
#[cfg(feature = "std")]
use quickcheck::TestResult;
#[allow(unused_imports)]
use typenum::{N1, P1, P2, Z0};

quickcheck! {
#[cfg(feature = "std")]
#[allow(trivial_casts)]
fn sqrt(v: F) -> TestResult {
if v < 0.0 {
return TestResult::discard();
}

let l: Quantity<Q<P1, Z0>, U<F>, F> = Quantity::<Q<P2, Z0>, U<F>, F> {
dimension: ::stdlib::marker::PhantomData,
units: ::stdlib::marker::PhantomData,
value: v,
}.sqrt();

TestResult::from_bool(v.sqrt() == l.value)
}
}

quickcheck! {
#[allow(trivial_casts)]
Expand Down Expand Up @@ -277,8 +299,6 @@ mod system_macro {

#[test]
fn conversion() {
use typenum::{N1, P1, Z0};

type U1 = BaseUnits<meter, kilogram, F>;
type U2 = BaseUnits<kilometer, kilogram, F>;

Expand Down
7 changes: 7 additions & 0 deletions tests/compile-fail/unsatisfied-trait-bounds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extern crate uom;

fn main() {
let l1 = uom::si::f32::Length::new::<uom::si::length::meter>(4.0).sqrt();
//~^ ERROR the trait bound `uom::si::ISQ<
}

0 comments on commit 40e58f6

Please sign in to comment.