Skip to content

Commit 219afe7

Browse files
committed
Remove DECIMAL_FRACTIONAL_UINT* from decimals
1 parent 8f82748 commit 219afe7

File tree

2 files changed

+4
-13
lines changed

2 files changed

+4
-13
lines changed

packages/std/src/math/decimal.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@ impl Decimal {
2121
const DECIMAL_FRACTIONAL: Uint128 = Uint128::new(1_000_000_000_000_000_000u128); // 1*10**18
2222
const DECIMAL_FRACTIONAL_SQUARED: Uint128 =
2323
Uint128::new(1_000_000_000_000_000_000_000_000_000_000_000_000u128); // (1*10**18)**2 = 1*10**36
24-
const DECIMAL_FRACTIONAL_UINT256: Uint256 = Uint256::from_be_bytes([
25-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 224, 182, 179,
26-
167, 100, 0, 0,
27-
]); // Python: `[b for b in (1*10**18).to_bytes(32, "big")]`
2824
const DECIMAL_PLACES: usize = 18; // This needs to be an even number.
2925

3026
pub const MAX: Self = Self(Uint128::MAX);
@@ -220,8 +216,8 @@ impl ops::Mul for Decimal {
220216
// (a.numerator() * b.numerator()) / (a.denominator() * b.denominator())
221217
// = (a.numerator() * b.numerator()) / a.denominator() / b.denominator()
222218

223-
let result_as_uint256 =
224-
self.numerator().full_mul(other.numerator()) / Self::DECIMAL_FRACTIONAL_UINT256;
219+
let result_as_uint256 = self.numerator().full_mul(other.numerator())
220+
/ Uint256::from_uint128(Self::DECIMAL_FRACTIONAL); // from_uint128 is a const method and should be "free"
225221
match result_as_uint256.try_into() {
226222
Ok(result) => Self(result),
227223
Err(_) => panic!("attempt to multiply with overflow"),

packages/std/src/math/decimal256.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ impl Decimal256 {
3232
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 192, 151, 206, 123, 201, 7, 21, 179,
3333
75, 159, 16, 0, 0, 0, 0,
3434
]);
35-
const DECIMAL_FRACTIONAL_UINT512: Uint512 = Uint512::from_be_bytes([
36-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37-
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 224, 182,
38-
179, 167, 100, 0, 0,
39-
]); // Python: `[b for b in (1*10**18).to_bytes(32, "big")]`
4035

4136
pub const MAX: Self = Self(Uint256::MAX);
4237

@@ -231,8 +226,8 @@ impl ops::Mul for Decimal256 {
231226
// (a.numerator() * b.numerator()) / (a.denominator() * b.denominator())
232227
// = (a.numerator() * b.numerator()) / a.denominator() / b.denominator()
233228

234-
let result_as_uint512 =
235-
self.numerator().full_mul(other.numerator()) / Self::DECIMAL_FRACTIONAL_UINT512;
229+
let result_as_uint512 = self.numerator().full_mul(other.numerator())
230+
/ Uint512::from_uint256(Self::DECIMAL_FRACTIONAL); // from_uint256 is a const method and should be "free"
236231
match result_as_uint512.try_into() {
237232
Ok(result) => Self(result),
238233
Err(_) => panic!("attempt to multiply with overflow"),

0 commit comments

Comments
 (0)