Skip to content

Commit

Permalink
Abs+Pow
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Dec 12, 2023
1 parent 5b84202 commit aaee0e9
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 39 deletions.
18 changes: 1 addition & 17 deletions src/fraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Rem, Sub, Su

use crate::primes::{FactorsOf, PRIMES};
use crate::tables::{approximate_via_lookup_table, ARCTAN_SUBDIVISIONS, ARCTAN_TABLE};
use crate::traits::Zero;
use crate::traits::{Abs, Zero};
use crate::Angle;

/// Returns a new fraction.
Expand Down Expand Up @@ -495,22 +495,6 @@ where
}
}

pub trait Abs {
fn abs(&self) -> Self;
}

impl Abs for i32 {
fn abs(&self) -> Self {
self.wrapping_abs()
}
}

impl Abs for i16 {
fn abs(&self) -> Self {
self.wrapping_abs()
}
}

impl fmt::Debug for Fraction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Fraction({self})")
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ mod size;
mod tables;
mod traits;
pub use traits::{
FloatConversion, FloatOrInt, FromComponents, IntoComponents, IntoSigned, IntoUnsigned, Lp2D,
PixelScaling, Px2D, Ranged, Roots, Round, ScreenScale, ScreenUnit, UPx2D, Unit, UnscaledUnit,
Zero,
Abs, FloatConversion, FloatOrInt, FromComponents, IntoComponents, IntoSigned, IntoUnsigned,
Lp2D, PixelScaling, Pow, Px2D, Ranged, Roots, Round, ScreenScale, ScreenUnit, UPx2D, Unit,
UnscaledUnit, Zero,
};
/// The measurement units supported by figures.
pub mod units;
Expand Down
4 changes: 2 additions & 2 deletions src/size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ impl<Unit> Size<Unit> {
/// Returns a new size using `dimension` for both width and height.
pub fn squared(dimension: Unit) -> Self
where
Unit: Copy,
Unit: Clone,
{
Self {
width: dimension,
width: dimension.clone(),
height: dimension,
}
}
Expand Down
71 changes: 70 additions & 1 deletion src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign};

use intentional::CastInto;
use intentional::{Cast, CastInto};

use crate::units::{Lp, Px, UPx};
use crate::Fraction;
Expand Down Expand Up @@ -83,6 +83,75 @@ impl_int_zero!(u64);
impl_int_zero!(u128);
impl_int_zero!(usize);

/// A type that can have its absolute difference from zero calculated.
pub trait Abs {
/// Returns the positive difference between this value and 0.
///
/// This function should never panic and always perform a saturating
/// absolute value calculation.
#[must_use]
fn abs(&self) -> Self;
}

macro_rules! impl_int_abs {
($type:ident) => {
impl Abs for $type {
fn abs(&self) -> Self {
self.saturating_abs()
}
}
};
}

impl_int_abs!(i8);
impl_int_abs!(i16);
impl_int_abs!(i32);
impl_int_abs!(i64);
impl_int_abs!(i128);
impl_int_abs!(isize);

impl Abs for f32 {
fn abs(&self) -> Self {
(*self).abs()
}
}

/// Raises a value to an exponent.
pub trait Pow {
/// Returns the saturating result of raising `self` to the `exp` power.
#[must_use]
fn pow(&self, exp: u32) -> Self;
}

macro_rules! impl_int_pow {
($type:ident) => {
impl Pow for $type {
fn pow(&self, exp: u32) -> Self {
self.saturating_pow(exp)
}
}
};
}

impl_int_pow!(i8);
impl_int_pow!(i16);
impl_int_pow!(i32);
impl_int_pow!(i64);
impl_int_pow!(i128);
impl_int_pow!(isize);
impl_int_pow!(u8);
impl_int_pow!(u16);
impl_int_pow!(u32);
impl_int_pow!(u64);
impl_int_pow!(u128);
impl_int_pow!(usize);

impl Pow for f32 {
fn pow(&self, exp: u32) -> Self {
self.powf(exp.cast())
}
}

/// Converts from a 2d vector in tuple form
pub trait FromComponents<Unit>: Sized {
/// Returns a new instance from the 2d vector components provided.
Expand Down
26 changes: 25 additions & 1 deletion src/twod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ macro_rules! impl_2d_math {
use super::$type;
use crate::traits::{
FloatConversion, FromComponents, IntoComponents, IntoSigned, IntoUnsigned, Ranged,
Round, ScreenScale, Zero,
Round, ScreenScale, Zero, Abs, Pow,
};
use crate::units::{Lp, Px, UPx};

Expand All @@ -21,6 +21,30 @@ macro_rules! impl_2d_math {
}
}

impl<Unit> Pow for $type<Unit>
where
Unit: Pow,
{
fn pow(&self, exp: u32) -> Self {
Self {
$x: self.$x.pow(exp),
$y: self.$y.pow(exp),
}
}
}

impl<Unit> Abs for $type<Unit>
where
Unit: Abs,
{
fn abs(&self) -> Self {
Self {
$x: self.$x.abs(),
$y: self.$y.abs(),
}
}
}

impl<Unit> Neg for $type<Unit>
where
Unit: Neg<Output = Unit>,
Expand Down
38 changes: 23 additions & 15 deletions src/units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, S
use intentional::{Cast, CastFrom};

use crate::traits::{
FloatConversion, IntoComponents, IntoSigned, IntoUnsigned, Roots, Round, ScreenScale,
Abs, FloatConversion, IntoComponents, IntoSigned, IntoUnsigned, Pow, Roots, Round, ScreenScale,
UnscaledUnit, Zero,
};
use crate::Fraction;
Expand Down Expand Up @@ -466,11 +466,17 @@ impl Lp {
pub fn inches_f(inches: f32) -> Self {
Self((inches * ARBITRARY_SCALE_F32).cast())
}
}

/// Raises this value to power of `exp`.
#[must_use]
pub fn pow(self, exp: u32) -> Self {
Self(self.0.pow(exp))
impl Pow for Lp {
fn pow(&self, exp: u32) -> Self {
Self(self.0.saturating_pow(exp))
}
}

impl Abs for Lp {
fn abs(&self) -> Self {
Self(self.0.saturating_abs())
}
}

Expand All @@ -496,11 +502,15 @@ impl fmt::Display for Lp {

define_integer_type!(Px, i32, "docs/px.md", 4);

impl Px {
/// Raises this value to power of `exp`.
#[must_use]
pub fn pow(self, exp: u32) -> Self {
Self(self.0.pow(exp) / 4_i32.pow(exp.saturating_sub(1)))
impl Pow for Px {
fn pow(&self, exp: u32) -> Self {
Self(self.0.saturating_pow(exp) / 4_i32.pow(exp.saturating_sub(1)))
}
}

impl Abs for Px {
fn abs(&self) -> Self {
Self(self.0.saturating_abs())
}
}

Expand Down Expand Up @@ -620,11 +630,9 @@ impl PartialOrd<UPx> for Px {

define_integer_type!(UPx, u32, "docs/upx.md", 4);

impl UPx {
/// Raises this value to power of `exp`.
#[must_use]
pub fn pow(self, exp: u32) -> Self {
Self(self.0.pow(exp) / 4_u32.pow(exp.saturating_sub(1)))
impl Pow for UPx {
fn pow(&self, exp: u32) -> Self {
Self(self.0.saturating_pow(exp) / 4_u32.pow(exp.saturating_sub(1)))
}
}

Expand Down

0 comments on commit aaee0e9

Please sign in to comment.