Skip to content

Commit

Permalink
Merge pull request #41 from droundy/master
Browse files Browse the repository at this point in the history
create Abs trait for absolute value
  • Loading branch information
paholg committed Jun 25, 2018
2 parents 3ef45da + b4c2924 commit 339614f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/make_units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,12 @@ macro_rules! make_units {
fn recip(self) -> Self::Output { $System::new(self.value_unsafe.recip()) }
}

impl<V: $crate::Abs, U> $crate::Abs for $System<V,U>
{
#[inline]
fn abs(self) -> Self { $System::new(self.value_unsafe.abs()) }
}

use $crate::typenum::Pow;
impl<Exp, V, U> Pow<Exp> for $System<V, U>
where V: Pow<Exp>,
Expand Down
38 changes: 38 additions & 0 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,44 @@ pub trait Recip {
impl_unary!(f32, Recip, recip);
impl_unary!(f64, Recip, recip);

/// `Abs` is used for implementing an `abs()` member for types that
/// can have their absolute value taken (which is most of them).
///
/// # Example
/// ```rust
/// extern crate dimensioned as dim;
/// use dim::si;
///
/// fn main() {
/// let t = -2.0 * si::S;
/// let abst = 2.0 * si::S;
///
/// use dim::Abs;
/// assert_eq!(t.abs(), abst);
/// }
/// ```
pub trait Abs {
/// The method for taking the absolute value
fn abs(self) -> Self;
}

macro_rules! impl_abs {
($t:ty) => {
impl Abs for $t {
fn abs(self) -> Self {
self.abs()
}
}
};
}
impl_abs!(f32);
impl_abs!(f64);
impl_abs!(i8);
impl_abs!(i16);
impl_abs!(i32);
impl_abs!(i64);
impl_abs!(isize);

/// `Root` is used for implementing general integer roots for types that aren't necessarily
/// preserved under root.
///
Expand Down

0 comments on commit 339614f

Please sign in to comment.