Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to
- cosmwasm-std: Add `Uint{64,128,256,512}::abs_diff` and
`Decimal{,256}::abs_diff` ([#1334]).
- cosmwasm-std: Implement `From<Decimal> for Decimal256`.
- cosmwasm-std: Implement `Rem`/`RemAssign` for `Decimal`/`Decimal256`.

[#1334]: https://github.com/CosmWasm/cosmwasm/pull/1334

Expand Down
64 changes: 63 additions & 1 deletion packages/std/src/math/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use schemars::JsonSchema;
use serde::{de, ser, Deserialize, Deserializer, Serialize};
use std::cmp::Ordering;
use std::fmt::{self, Write};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign};
use std::str::FromStr;
use thiserror::Error;

Expand Down Expand Up @@ -481,6 +481,26 @@ impl DivAssign<Uint128> for Decimal {
}
}

impl Rem for Decimal {
type Output = Self;

/// # Panics
///
/// This operation will panic if `rhs` is zero
#[inline]
fn rem(self, rhs: Self) -> Self {
Self(self.0.rem(rhs.0))
}
}
forward_ref_binop!(impl Rem, rem for Decimal, Decimal);

impl RemAssign<Decimal> for Decimal {
fn rem_assign(&mut self, rhs: Decimal) {
*self = *self % rhs;
}
}
forward_ref_op_assign!(impl RemAssign, rem_assign for Decimal, Decimal);

impl<A> std::iter::Sum<A> for Decimal
where
Self: Add<A, Output = Self>,
Expand Down Expand Up @@ -1679,4 +1699,46 @@ mod tests {
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}

#[test]
#[allow(clippy::op_ref)]
fn decimal_rem_works() {
// 4.02 % 1.11 = 0.69
assert_eq!(
Decimal::percent(402) % Decimal::percent(111),
Decimal::percent(69)
);

// 15.25 % 4 = 3.25
assert_eq!(
Decimal::percent(1525) % Decimal::percent(400),
Decimal::percent(325)
);

let a = Decimal::percent(318);
let b = Decimal::percent(317);
let expected = Decimal::percent(1);
assert_eq!(a % b, expected);
assert_eq!(a % &b, expected);
assert_eq!(&a % b, expected);
assert_eq!(&a % &b, expected);
}

#[test]
fn decimal_rem_assign_works() {
let mut a = Decimal::percent(17673);
a %= Decimal::percent(2362);
assert_eq!(a, Decimal::percent(1139)); // 176.73 % 23.62 = 11.39

let mut a = Decimal::percent(4262);
let b = Decimal::percent(1270);
a %= &b;
assert_eq!(a, Decimal::percent(452)); // 42.62 % 12.7 = 4.52
}

#[test]
#[should_panic(expected = "divisor of zero")]
fn decimal_rem_panics_for_zero() {
let _ = Decimal::percent(777) % Decimal::zero();
}
}
64 changes: 63 additions & 1 deletion packages/std/src/math/decimal256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use schemars::JsonSchema;
use serde::{de, ser, Deserialize, Deserializer, Serialize};
use std::cmp::Ordering;
use std::fmt::{self, Write};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign};
use std::str::FromStr;
use thiserror::Error;

Expand Down Expand Up @@ -505,6 +505,26 @@ impl DivAssign<Uint256> for Decimal256 {
}
}

impl Rem for Decimal256 {
type Output = Self;

/// # Panics
///
/// This operation will panic if `rhs` is zero
#[inline]
fn rem(self, rhs: Self) -> Self {
Self(self.0.rem(rhs.0))
}
}
forward_ref_binop!(impl Rem, rem for Decimal256, Decimal256);

impl RemAssign<Decimal256> for Decimal256 {
fn rem_assign(&mut self, rhs: Decimal256) {
*self = *self % rhs;
}
}
forward_ref_op_assign!(impl RemAssign, rem_assign for Decimal256, Decimal256);

impl<A> std::iter::Sum<A> for Decimal256
where
Self: Add<A, Output = Self>,
Expand Down Expand Up @@ -1825,4 +1845,46 @@ mod tests {
assert_eq!(a.abs_diff(b), expected);
assert_eq!(b.abs_diff(a), expected);
}

#[test]
#[allow(clippy::op_ref)]
fn decimal256_rem_works() {
// 4.02 % 1.11 = 0.69
assert_eq!(
Decimal256::percent(402) % Decimal256::percent(111),
Decimal256::percent(69)
);

// 15.25 % 4 = 3.25
assert_eq!(
Decimal256::percent(1525) % Decimal256::percent(400),
Decimal256::percent(325)
);

let a = Decimal256::percent(318);
let b = Decimal256::percent(317);
let expected = Decimal256::percent(1);
assert_eq!(a % b, expected);
assert_eq!(a % &b, expected);
assert_eq!(&a % b, expected);
assert_eq!(&a % &b, expected);
}

#[test]
fn decimal_rem_assign_works() {
let mut a = Decimal256::percent(17673);
a %= Decimal256::percent(2362);
assert_eq!(a, Decimal256::percent(1139)); // 176.73 % 23.62 = 11.39

let mut a = Decimal256::percent(4262);
let b = Decimal256::percent(1270);
a %= &b;
assert_eq!(a, Decimal256::percent(452)); // 42.62 % 12.7 = 4.52
}

#[test]
#[should_panic(expected = "division by zero")]
fn decimal256_rem_panics_for_zero() {
let _ = Decimal256::percent(777) % Decimal256::zero();
}
}
35 changes: 2 additions & 33 deletions packages/std/src/math/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,37 +54,6 @@ mod tests {
impl AllImpl<'_> for Uint128 {}
impl AllImpl<'_> for Uint256 {}
impl AllImpl<'_> for Uint512 {}

// TODO: When all implementations are done, extra trait can be removed and
// unified with AllImpl
trait AllImplDecimal<'a>:
Add
+ Add<&'a Self>
+ AddAssign
+ AddAssign<&'a Self>
+ Sub
+ Sub<&'a Self>
+ SubAssign
+ SubAssign<&'a Self>
+ Mul
+ Mul<&'a Self>
+ MulAssign
+ MulAssign<&'a Self>
+ Div
+ Div<&'a Self>
+ DivAssign
+ DivAssign<&'a Self>
// + Rem
// + Rem<&'a Self>
// + RemAssign
// + RemAssign<&'a Self>
+ Sized
+ Copy
where
Self: 'a,
{
}

impl AllImplDecimal<'_> for Decimal {}
impl AllImplDecimal<'_> for Decimal256 {}
impl AllImpl<'_> for Decimal {}
impl AllImpl<'_> for Decimal256 {}
}