Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add abs_diff function for math package #1334

Merged
merged 4 commits into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/std/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,10 @@ impl Decimal {
Decimal(inner.isqrt().checked_mul(Uint128::from(outer_mul)).unwrap())
})
}

pub const fn abs_diff(self, other: Self) -> Self {
Self(self.0.abs_diff(other.0))
}
}

impl Fraction<Uint128> for Decimal {
Expand Down Expand Up @@ -1664,4 +1668,13 @@ mod tests {
Decimal::percent(8765)
);
}

#[test]
fn decimal_abs_diff_works() {
let a = Decimal::percent(285);
let b = Decimal::percent(200);
let expected = Decimal::percent(85);
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}
}
17 changes: 17 additions & 0 deletions packages/std/src/math/decimal256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,14 @@ impl Decimal256 {
Self(inner.isqrt().checked_mul(outer_mul).unwrap())
})
}

pub fn abs_diff(self, other: Self) -> Self {
if self < other {
other - self
} else {
self - other
}
}
}

impl Fraction<Uint256> for Decimal256 {
Expand Down Expand Up @@ -1783,4 +1791,13 @@ mod tests {
Decimal256::percent(8765)
);
}

#[test]
fn decimal256_abs_diff_works() {
let a = Decimal256::percent(285);
let b = Decimal256::percent(200);
let expected = Decimal256::percent(85);
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}
}
13 changes: 13 additions & 0 deletions packages/std/src/math/uint128.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ impl Uint128 {
pub fn saturating_pow(self, other: u32) -> Self {
Self(self.0.saturating_pow(other))
}

pub const fn abs_diff(self, other: Self) -> Self {
Self(self.0.abs_diff(other.0))
webmaster128 marked this conversation as resolved.
Show resolved Hide resolved
}
}

// `From<u{128,64,32,16,8}>` is implemented manually instead of
Expand Down Expand Up @@ -965,4 +969,13 @@ mod tests {
a %= &b;
assert_eq!(a, Uint128::from(1u32));
}

#[test]
fn uint128_abs_diff_works() {
let a = Uint128::from(42u32);
let b = Uint128::from(5u32);
let expected = Uint128::from(37u32);
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}
}
17 changes: 17 additions & 0 deletions packages/std/src/math/uint256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,14 @@ impl Uint256 {
pub fn saturating_mul(self, other: Self) -> Self {
Self(self.0.saturating_mul(other.0))
}

pub fn abs_diff(self, other: Self) -> Self {
if self < other {
other - self
} else {
self - other
}
}
}

impl From<Uint128> for Uint256 {
Expand Down Expand Up @@ -1516,4 +1524,13 @@ mod tests {
a %= &b;
assert_eq!(a, Uint256::from(1u32));
}

#[test]
fn uint256_abs_diff_works() {
let a = Uint256::from(42u32);
let b = Uint256::from(5u32);
let expected = Uint256::from(37u32);
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}
}
17 changes: 17 additions & 0 deletions packages/std/src/math/uint512.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,14 @@ impl Uint512 {
pub fn saturating_mul(self, other: Self) -> Self {
Self(self.0.saturating_mul(other.0))
}

pub fn abs_diff(self, other: Self) -> Self {
if self < other {
other - self
} else {
self - other
}
}
}

impl From<Uint256> for Uint512 {
Expand Down Expand Up @@ -1151,4 +1159,13 @@ mod tests {
a %= &b;
assert_eq!(a, Uint512::from(1u32));
}

#[test]
fn uint512_abs_diff_works() {
let a = Uint512::from(42u32);
let b = Uint512::from(5u32);
let expected = Uint512::from(37u32);
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}
}
13 changes: 13 additions & 0 deletions packages/std/src/math/uint64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ impl Uint64 {
pub fn saturating_pow(self, other: u32) -> Self {
Self(self.0.saturating_pow(other))
}

pub const fn abs_diff(self, other: Self) -> Self {
Self(self.0.abs_diff(other.0))
webmaster128 marked this conversation as resolved.
Show resolved Hide resolved
}
}

// `From<u{128,64,32,16,8}>` is implemented manually instead of
Expand Down Expand Up @@ -878,4 +882,13 @@ mod tests {
a %= &b;
assert_eq!(a, Uint64::from(1u32));
}

#[test]
fn uint64_abs_diff_works() {
let a = Uint64::from(42u32);
let b = Uint64::from(5u32);
let expected = Uint64::from(37u32);
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}
}