Skip to content

Commit

Permalink
Move hypot function from quantity! into system!.
Browse files Browse the repository at this point in the history
Previously the function was implemented in the `quantity!` macro for
specific quantities. The function couldn't be used in a generic context.
  • Loading branch information
iliekturtles committed May 20, 2020
1 parent 5883e7c commit 0980882
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 44 deletions.
32 changes: 0 additions & 32 deletions src/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,38 +385,6 @@ macro_rules! quantity {
Self::new::<N>(self.get::<N>().fract())
}

autoconvert! {
/// Calculates the length of the hypotenuse of a right-angle triangle given the legs.
#[cfg(feature = "std")]
#[inline(always)]
pub fn hypot<Ur>(self, other: $quantity<Ur, V>) -> Self
where
V: $crate::num::Float,
Ur: super::Units<V> + ?Sized,
{
Self {
dimension: $crate::lib::marker::PhantomData,
units: $crate::lib::marker::PhantomData,
value: self.value.hypot(
super::change_base::<Dimension, U, Ur, V>(&other.value)),
}
}}

not_autoconvert! {
/// Calculates the length of the hypotenuse of a right-angle triangle given the legs.
#[cfg(feature = "std")]
#[inline(always)]
pub fn hypot(self, other: Self) -> Self
where
V: $crate::num::Float,
{
Self {
dimension: $crate::lib::marker::PhantomData,
units: $crate::lib::marker::PhantomData,
value: self.value.hypot(other.value),
}
}}

/// Creates a struct that can be used to format a compatible quantity for display.
///
/// # Notes
Expand Down
30 changes: 30 additions & 0 deletions src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,38 @@ macro_rules! system {
units: $crate::lib::marker::PhantomData,
value: self.value.cbrt(),
}
}

autoconvert! {
/// Calculates the length of the hypotenuse of a right-angle triangle given the legs.
#[inline(always)]
pub fn hypot<Ur>(self, other: Quantity<D, Ur, V>) -> Self
where
V: $crate::num::Float,
Ur: Units<V> + ?Sized,
{
Self {
dimension: $crate::lib::marker::PhantomData,
units: $crate::lib::marker::PhantomData,
value: self.value.hypot(
change_base::<D, U, Ur, V>(&other.value)),
}
}}

not_autoconvert! {
/// Calculates the length of the hypotenuse of a right-angle triangle given the legs.
#[inline(always)]
pub fn hypot(self, other: Self) -> Self
where
V: $crate::num::Float,
{
Self {
dimension: $crate::lib::marker::PhantomData,
units: $crate::lib::marker::PhantomData,
value: self.value.hypot(other.value),
}
}}}

/// Computes the absolute value of `self`. Returns `NAN` if the quantity is
/// `NAN`.
#[inline(always)]
Expand Down
9 changes: 0 additions & 9 deletions src/tests/quantities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,5 @@ mod float {

Test::assert_eq(&3.3.fract(), &m1.fract::<kilogram>().get::<kilogram>());
}

#[cfg(feature = "std")]
quickcheck! {
#[allow(trivial_casts)]
fn hypot(l: V, r: V) -> bool {
Test::eq(&l.hypot(r),
&Length::new::<meter>(l).hypot(Length::new::<meter>(r)).get::<meter>())
}
}
}
}
5 changes: 2 additions & 3 deletions src/tests/quantity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,8 +462,7 @@ mod float {
}
}

#[cfg(feature = "std")]
autoconvert! {
#[cfg(all(feature = "std", feature = "autoconvert"))]
quickcheck! {
#[allow(trivial_casts)]
fn hypot_mixed(l: V, r: V) -> bool {
Expand All @@ -474,6 +473,6 @@ mod float {

fk && kf
}
}}
}
}
}
7 changes: 7 additions & 0 deletions src/tests/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ mod float {
Test::eq(&v.cbrt(), &l.value)
}

#[cfg(feature = "std")]
#[allow(trivial_casts)]
fn hypot(l: A<V>, r: A<V>) -> bool {
Test::eq(&Length::new::<meter>(l.hypot(*r)),
&Length::new::<meter>(*l).hypot(Length::new::<meter>(*r)))
}

#[allow(trivial_casts)]
fn is_sign_positive(v: A<V>) -> bool {
v.is_sign_positive() == Length::new::<meter>(*v).is_sign_positive()
Expand Down

0 comments on commit 0980882

Please sign in to comment.