cosmwasm_std/math/
decimal256.rs

1use alloc::string::ToString;
2use core::cmp::Ordering;
3use core::fmt::{self, Write};
4use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Rem, RemAssign, Sub, SubAssign};
5use core::str::FromStr;
6use serde::{de, ser, Deserialize, Deserializer, Serialize};
7
8use crate::errors::{
9    CheckedFromRatioError, CheckedMultiplyRatioError, DivideByZeroError, OverflowError,
10    OverflowOperation, RoundUpOverflowError, StdError,
11};
12use crate::forward_ref::{forward_ref_binop, forward_ref_op_assign};
13use crate::{
14    Decimal, SignedDecimal, SignedDecimal256, Uint512, __internal::forward_ref_partial_eq,
15};
16
17use super::Fraction;
18use super::Isqrt;
19use super::Uint256;
20
21/// A fixed-point decimal value with 18 fractional digits, i.e. Decimal256(1_000_000_000_000_000_000) == 1.0
22///
23/// The greatest possible value that can be represented is
24/// 115792089237316195423570985008687907853269984665640564039457.584007913129639935
25/// (which is (2^256 - 1) / 10^18)
26#[derive(Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, schemars::JsonSchema)]
27pub struct Decimal256(#[schemars(with = "String")] Uint256);
28
29forward_ref_partial_eq!(Decimal256, Decimal256);
30
31#[derive(Debug, PartialEq, Eq, thiserror::Error)]
32#[error("Decimal256 range exceeded")]
33pub struct Decimal256RangeExceeded;
34
35impl Decimal256 {
36    const DECIMAL_FRACTIONAL: Uint256 = // 1*10**18
37        Uint256::new(1_000_000_000_000_000_000);
38    const DECIMAL_FRACTIONAL_SQUARED: Uint256 = // 1*10**36
39        Uint256::new(1_000_000_000_000_000_000_000_000_000_000_000_000);
40
41    /// The number of decimal places. Since decimal types are fixed-point rather than
42    /// floating-point, this is a constant.
43    pub const DECIMAL_PLACES: u32 = 18;
44    /// The largest value that can be represented by this decimal type.
45    pub const MAX: Self = Self(Uint256::MAX);
46    /// The smallest value that can be represented by this decimal type.
47    pub const MIN: Self = Self(Uint256::MIN);
48
49    /// Creates a Decimal256 from Uint256
50    /// This is equivalent to `Decimal256::from_atomics(value, 18)` but usable in a const context.
51    ///
52    /// ## Examples
53    ///
54    /// ```
55    /// # use cosmwasm_std::{Uint256, Decimal256};
56    /// let atoms = Uint256::new(141_183_460_469_231_731_687_303_715_884_105_727_125);
57    /// let value = Decimal256::new(atoms);
58    /// assert_eq!(value.to_string(), "141183460469231731687.303715884105727125");
59    /// ```
60    #[inline]
61    #[must_use]
62    pub const fn new(value: Uint256) -> Self {
63        Self(value)
64    }
65
66    /// Creates a Decimal256 from u128
67    /// This is equivalent to `Decimal256::from_atomics(value, 18)` but usable in a const context.
68    #[deprecated(
69        since = "3.0.0",
70        note = "Use Decimal256::new(Uint256::new(value)) instead"
71    )]
72    pub const fn raw(value: u128) -> Self {
73        Self(Uint256::new(value))
74    }
75
76    /// Create a 1.0 Decimal256
77    #[inline]
78    pub const fn one() -> Self {
79        Self(Self::DECIMAL_FRACTIONAL)
80    }
81
82    /// Create a 0.0 Decimal256
83    #[inline]
84    pub const fn zero() -> Self {
85        Self(Uint256::zero())
86    }
87
88    /// Convert x% into Decimal256
89    ///
90    /// ## Examples
91    ///
92    /// ```
93    /// # use std::str::FromStr;
94    /// # use cosmwasm_std::Decimal256;
95    /// const HALF: Decimal256 = Decimal256::percent(50);
96    ///
97    /// assert_eq!(HALF, Decimal256::from_str("0.5").unwrap());
98    /// ```
99    pub const fn percent(x: u64) -> Self {
100        // multiplication does not overflow since `u64::MAX` * 10**16 is well in u128 range
101        let atomics = (x as u128) * 10_000_000_000_000_000;
102        Self(Uint256::new(atomics))
103    }
104
105    /// Convert permille (x/1000) into Decimal256
106    ///
107    /// ## Examples
108    ///
109    /// ```
110    /// # use std::str::FromStr;
111    /// # use cosmwasm_std::Decimal256;
112    /// const HALF: Decimal256 = Decimal256::permille(500);
113    ///
114    /// assert_eq!(HALF, Decimal256::from_str("0.5").unwrap());
115    /// ```
116    pub const fn permille(x: u64) -> Self {
117        // multiplication does not overflow since `u64::MAX` * 10**15 is well in u128 range
118        let atomics = (x as u128) * 1_000_000_000_000_000;
119        Self(Uint256::new(atomics))
120    }
121
122    /// Convert basis points (x/10000) into Decimal256
123    ///
124    /// ## Examples
125    ///
126    /// ```
127    /// # use std::str::FromStr;
128    /// # use cosmwasm_std::Decimal256;
129    /// const TWO_BPS: Decimal256 = Decimal256::bps(2);
130    /// const HALF: Decimal256 = Decimal256::bps(5000);
131    ///
132    /// assert_eq!(TWO_BPS, Decimal256::from_str("0.0002").unwrap());
133    /// assert_eq!(HALF, Decimal256::from_str("0.5").unwrap());
134    /// ```
135    pub const fn bps(x: u64) -> Self {
136        // multiplication does not overflow since `u64::MAX` * 10**14 is well in u128 range
137        let atomics = (x as u128) * 100_000_000_000_000;
138        Self(Uint256::new(atomics))
139    }
140
141    /// Creates a decimal from a number of atomic units and the number
142    /// of decimal places. The inputs will be converted internally to form
143    /// a decimal with 18 decimal places. So the input 123 and 2 will create
144    /// the decimal 1.23.
145    ///
146    /// Using 18 decimal places is slightly more efficient than other values
147    /// as no internal conversion is necessary.
148    ///
149    /// ## Examples
150    ///
151    /// ```
152    /// # use cosmwasm_std::{Decimal256, Uint256};
153    /// let a = Decimal256::from_atomics(1234u64, 3).unwrap();
154    /// assert_eq!(a.to_string(), "1.234");
155    ///
156    /// let a = Decimal256::from_atomics(1234u128, 0).unwrap();
157    /// assert_eq!(a.to_string(), "1234");
158    ///
159    /// let a = Decimal256::from_atomics(1u64, 18).unwrap();
160    /// assert_eq!(a.to_string(), "0.000000000000000001");
161    ///
162    /// let a = Decimal256::from_atomics(Uint256::MAX, 18).unwrap();
163    /// assert_eq!(a, Decimal256::MAX);
164    /// ```
165    pub fn from_atomics(
166        atomics: impl Into<Uint256>,
167        decimal_places: u32,
168    ) -> Result<Self, Decimal256RangeExceeded> {
169        let atomics = atomics.into();
170        const TEN: Uint256 = Uint256::from_be_bytes([
171            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,
172            0, 0, 10,
173        ]);
174        Ok(match decimal_places.cmp(&Self::DECIMAL_PLACES) {
175            Ordering::Less => {
176                let digits = (Self::DECIMAL_PLACES) - decimal_places; // No overflow because decimal_places < DECIMAL_PLACES
177                let factor = TEN.checked_pow(digits).unwrap(); // Safe because digits <= 17
178                Self(
179                    atomics
180                        .checked_mul(factor)
181                        .map_err(|_| Decimal256RangeExceeded)?,
182                )
183            }
184            Ordering::Equal => Self(atomics),
185            Ordering::Greater => {
186                let digits = decimal_places - (Self::DECIMAL_PLACES); // No overflow because decimal_places > DECIMAL_PLACES
187                if let Ok(factor) = TEN.checked_pow(digits) {
188                    Self(atomics.checked_div(factor).unwrap()) // Safe because factor cannot be zero
189                } else {
190                    // In this case `factor` exceeds the Uint256 range.
191                    // Any Uint256 `x` divided by `factor` with `factor > Uint256::MAX` is 0.
192                    // Try e.g. Python3: `(2**256-1) // 2**256`
193                    Self(Uint256::zero())
194                }
195            }
196        })
197    }
198
199    /// Returns the ratio (numerator / denominator) as a Decimal256
200    pub fn from_ratio(numerator: impl Into<Uint256>, denominator: impl Into<Uint256>) -> Self {
201        match Decimal256::checked_from_ratio(numerator, denominator) {
202            Ok(value) => value,
203            Err(CheckedFromRatioError::DivideByZero) => {
204                panic!("Denominator must not be zero")
205            }
206            Err(CheckedFromRatioError::Overflow) => panic!("Multiplication overflow"),
207        }
208    }
209
210    /// Returns the ratio (numerator / denominator) as a Decimal256
211    pub fn checked_from_ratio(
212        numerator: impl Into<Uint256>,
213        denominator: impl Into<Uint256>,
214    ) -> Result<Self, CheckedFromRatioError> {
215        let numerator: Uint256 = numerator.into();
216        let denominator: Uint256 = denominator.into();
217        match numerator.checked_multiply_ratio(Self::DECIMAL_FRACTIONAL, denominator) {
218            Ok(ratio) => {
219                // numerator * DECIMAL_FRACTIONAL / denominator
220                Ok(Self(ratio))
221            }
222            Err(CheckedMultiplyRatioError::Overflow) => Err(CheckedFromRatioError::Overflow),
223            Err(CheckedMultiplyRatioError::DivideByZero) => {
224                Err(CheckedFromRatioError::DivideByZero)
225            }
226        }
227    }
228
229    #[must_use]
230    pub const fn is_zero(&self) -> bool {
231        self.0.is_zero()
232    }
233
234    /// A decimal is an integer of atomic units plus a number that specifies the
235    /// position of the decimal dot. So any decimal can be expressed as two numbers.
236    ///
237    /// ## Examples
238    ///
239    /// ```
240    /// # use cosmwasm_std::{Decimal256, Uint256};
241    /// # use core::str::FromStr;
242    /// // Value with whole and fractional part
243    /// let a = Decimal256::from_str("1.234").unwrap();
244    /// assert_eq!(a.decimal_places(), 18);
245    /// assert_eq!(a.atomics(), Uint256::from(1234000000000000000u128));
246    ///
247    /// // Smallest possible value
248    /// let b = Decimal256::from_str("0.000000000000000001").unwrap();
249    /// assert_eq!(b.decimal_places(), 18);
250    /// assert_eq!(b.atomics(), Uint256::from(1u128));
251    /// ```
252    #[must_use]
253    #[inline]
254    pub const fn atomics(&self) -> Uint256 {
255        self.0
256    }
257
258    /// The number of decimal places. This is a constant value for now
259    /// but this could potentially change as the type evolves.
260    ///
261    /// See also [`Decimal256::atomics()`].
262    #[must_use]
263    #[inline]
264    pub const fn decimal_places(&self) -> u32 {
265        Self::DECIMAL_PLACES
266    }
267
268    /// Rounds value down after decimal places.
269    #[must_use = "this returns the result of the operation, without modifying the original"]
270    pub fn floor(&self) -> Self {
271        Self((self.0 / Self::DECIMAL_FRACTIONAL) * Self::DECIMAL_FRACTIONAL)
272    }
273
274    /// Rounds value up after decimal places. Panics on overflow.
275    #[must_use = "this returns the result of the operation, without modifying the original"]
276    pub fn ceil(&self) -> Self {
277        match self.checked_ceil() {
278            Ok(value) => value,
279            Err(_) => panic!("attempt to ceil with overflow"),
280        }
281    }
282
283    /// Rounds value up after decimal places. Returns OverflowError on overflow.
284    pub fn checked_ceil(&self) -> Result<Self, RoundUpOverflowError> {
285        let floor = self.floor();
286        if floor == self {
287            Ok(floor)
288        } else {
289            floor
290                .checked_add(Decimal256::one())
291                .map_err(|_| RoundUpOverflowError)
292        }
293    }
294
295    pub fn checked_add(self, other: Self) -> Result<Self, OverflowError> {
296        self.0
297            .checked_add(other.0)
298            .map(Self)
299            .map_err(|_| OverflowError::new(OverflowOperation::Add))
300    }
301
302    pub fn checked_sub(self, other: Self) -> Result<Self, OverflowError> {
303        self.0
304            .checked_sub(other.0)
305            .map(Self)
306            .map_err(|_| OverflowError::new(OverflowOperation::Sub))
307    }
308
309    /// Multiplies one `Decimal256` by another, returning an `OverflowError` if an overflow occurred.
310    pub fn checked_mul(self, other: Self) -> Result<Self, OverflowError> {
311        let result_as_uint512 = self.numerator().full_mul(other.numerator())
312            / Uint512::from_uint256(Self::DECIMAL_FRACTIONAL); // from_uint128 is a const method and should be "free"
313        result_as_uint512
314            .try_into()
315            .map(Self)
316            .map_err(|_| OverflowError::new(OverflowOperation::Mul))
317    }
318
319    /// Raises a value to the power of `exp`, panics if an overflow occurred.
320    #[must_use = "this returns the result of the operation, without modifying the original"]
321    pub fn pow(self, exp: u32) -> Self {
322        match self.checked_pow(exp) {
323            Ok(value) => value,
324            Err(_) => panic!("Multiplication overflow"),
325        }
326    }
327
328    /// Raises a value to the power of `exp`, returning an `OverflowError` if an overflow occurred.
329    pub fn checked_pow(self, exp: u32) -> Result<Self, OverflowError> {
330        // This uses the exponentiation by squaring algorithm:
331        // https://en.wikipedia.org/wiki/Exponentiation_by_squaring#Basic_method
332
333        fn inner(mut x: Decimal256, mut n: u32) -> Result<Decimal256, OverflowError> {
334            if n == 0 {
335                return Ok(Decimal256::one());
336            }
337
338            let mut y = Decimal256::one();
339
340            while n > 1 {
341                if n % 2 == 0 {
342                    x = x.checked_mul(x)?;
343                    n /= 2;
344                } else {
345                    y = x.checked_mul(y)?;
346                    x = x.checked_mul(x)?;
347                    n = (n - 1) / 2;
348                }
349            }
350
351            Ok(x * y)
352        }
353
354        inner(self, exp).map_err(|_| OverflowError::new(OverflowOperation::Pow))
355    }
356
357    pub fn checked_div(self, other: Self) -> Result<Self, CheckedFromRatioError> {
358        Decimal256::checked_from_ratio(self.numerator(), other.numerator())
359    }
360
361    pub fn checked_rem(self, other: Self) -> Result<Self, DivideByZeroError> {
362        self.0
363            .checked_rem(other.0)
364            .map(Self)
365            .map_err(|_| DivideByZeroError)
366    }
367
368    /// Returns the approximate square root as a Decimal256.
369    ///
370    /// This should not overflow or panic.
371    #[must_use = "this returns the result of the operation, without modifying the original"]
372    pub fn sqrt(&self) -> Self {
373        // Algorithm described in https://hackmd.io/@webmaster128/SJThlukj_
374        // We start with the highest precision possible and lower it until
375        // there's no overflow.
376        //
377        // TODO: This could be made more efficient once log10 is in:
378        // https://github.com/rust-lang/rust/issues/70887
379        // The max precision is something like `18 - log10(self.0) / 2`.
380        (0..=Self::DECIMAL_PLACES / 2)
381            .rev()
382            .find_map(|i| self.sqrt_with_precision(i))
383            // The last step (i = 0) is guaranteed to succeed because `isqrt(Uint256::MAX) * 10^9` does not overflow
384            .unwrap()
385    }
386
387    /// Lower precision means more aggressive rounding, but less risk of overflow.
388    /// Precision *must* be a number between 0 and 9 (inclusive).
389    ///
390    /// Returns `None` if the internal multiplication overflows.
391    #[must_use = "this returns the result of the operation, without modifying the original"]
392    fn sqrt_with_precision(&self, precision: u32) -> Option<Self> {
393        let inner_mul = Uint256::from(100u128).pow(precision);
394        self.0.checked_mul(inner_mul).ok().map(|inner| {
395            let outer_mul = Uint256::from(10u128).pow(Self::DECIMAL_PLACES / 2 - precision);
396            Self(inner.isqrt().checked_mul(outer_mul).unwrap())
397        })
398    }
399
400    #[must_use = "this returns the result of the operation, without modifying the original"]
401    pub fn abs_diff(self, other: Self) -> Self {
402        if self < other {
403            other - self
404        } else {
405            self - other
406        }
407    }
408
409    #[must_use = "this returns the result of the operation, without modifying the original"]
410    pub fn saturating_add(self, other: Self) -> Self {
411        match self.checked_add(other) {
412            Ok(value) => value,
413            Err(_) => Self::MAX,
414        }
415    }
416
417    #[must_use = "this returns the result of the operation, without modifying the original"]
418    pub fn saturating_sub(self, other: Self) -> Self {
419        match self.checked_sub(other) {
420            Ok(value) => value,
421            Err(_) => Self::zero(),
422        }
423    }
424
425    #[must_use = "this returns the result of the operation, without modifying the original"]
426    pub fn saturating_mul(self, other: Self) -> Self {
427        match self.checked_mul(other) {
428            Ok(value) => value,
429            Err(_) => Self::MAX,
430        }
431    }
432
433    #[must_use = "this returns the result of the operation, without modifying the original"]
434    pub fn saturating_pow(self, exp: u32) -> Self {
435        match self.checked_pow(exp) {
436            Ok(value) => value,
437            Err(_) => Self::MAX,
438        }
439    }
440
441    /// Converts this decimal to an unsigned integer by truncating
442    /// the fractional part, e.g. 22.5 becomes 22.
443    ///
444    /// ## Examples
445    ///
446    /// ```
447    /// use core::str::FromStr;
448    /// use cosmwasm_std::{Decimal256, Uint256};
449    ///
450    /// let d = Decimal256::from_str("12.345").unwrap();
451    /// assert_eq!(d.to_uint_floor(), Uint256::from(12u64));
452    ///
453    /// let d = Decimal256::from_str("12.999").unwrap();
454    /// assert_eq!(d.to_uint_floor(), Uint256::from(12u64));
455    ///
456    /// let d = Decimal256::from_str("75.0").unwrap();
457    /// assert_eq!(d.to_uint_floor(), Uint256::from(75u64));
458    /// ```
459    #[must_use = "this returns the result of the operation, without modifying the original"]
460    pub fn to_uint_floor(self) -> Uint256 {
461        self.0 / Self::DECIMAL_FRACTIONAL
462    }
463
464    /// Converts this decimal to an unsigned integer by rounting up
465    /// to the next integer, e.g. 22.3 becomes 23.
466    ///
467    /// ## Examples
468    ///
469    /// ```
470    /// use core::str::FromStr;
471    /// use cosmwasm_std::{Decimal256, Uint256};
472    ///
473    /// let d = Decimal256::from_str("12.345").unwrap();
474    /// assert_eq!(d.to_uint_ceil(), Uint256::from(13u64));
475    ///
476    /// let d = Decimal256::from_str("12.999").unwrap();
477    /// assert_eq!(d.to_uint_ceil(), Uint256::from(13u64));
478    ///
479    /// let d = Decimal256::from_str("75.0").unwrap();
480    /// assert_eq!(d.to_uint_ceil(), Uint256::from(75u64));
481    /// ```
482    #[must_use = "this returns the result of the operation, without modifying the original"]
483    pub fn to_uint_ceil(self) -> Uint256 {
484        // Using `q = 1 + ((x - 1) / y); // if x != 0` with unsigned integers x, y, q
485        // from https://stackoverflow.com/a/2745086/2013738. We know `x + y` CAN overflow.
486        let x = self.0;
487        let y = Self::DECIMAL_FRACTIONAL;
488        if x.is_zero() {
489            Uint256::zero()
490        } else {
491            Uint256::one() + ((x - Uint256::one()) / y)
492        }
493    }
494}
495
496impl Fraction<Uint256> for Decimal256 {
497    #[inline]
498    fn numerator(&self) -> Uint256 {
499        self.0
500    }
501
502    #[inline]
503    fn denominator(&self) -> Uint256 {
504        Self::DECIMAL_FRACTIONAL
505    }
506
507    /// Returns the multiplicative inverse `1/d` for decimal `d`.
508    ///
509    /// If `d` is zero, none is returned.
510    fn inv(&self) -> Option<Self> {
511        if self.is_zero() {
512            None
513        } else {
514            // Let self be p/q with p = self.0 and q = DECIMAL_FRACTIONAL.
515            // Now we calculate the inverse a/b = q/p such that b = DECIMAL_FRACTIONAL. Then
516            // `a = DECIMAL_FRACTIONAL*DECIMAL_FRACTIONAL / self.0`.
517            Some(Self(Self::DECIMAL_FRACTIONAL_SQUARED / self.0))
518        }
519    }
520}
521
522impl From<Decimal> for Decimal256 {
523    fn from(input: Decimal) -> Self {
524        // Unwrap is safe because Decimal256 and Decimal have the same decimal places.
525        // Every Decimal value can be stored in Decimal256.
526        Decimal256::from_atomics(input.atomics(), input.decimal_places()).unwrap()
527    }
528}
529
530impl TryFrom<SignedDecimal> for Decimal256 {
531    type Error = Decimal256RangeExceeded;
532
533    fn try_from(value: SignedDecimal) -> Result<Self, Self::Error> {
534        value
535            .atomics()
536            .try_into()
537            .map(Decimal256)
538            .map_err(|_| Decimal256RangeExceeded)
539    }
540}
541
542impl TryFrom<SignedDecimal256> for Decimal256 {
543    type Error = Decimal256RangeExceeded;
544
545    fn try_from(value: SignedDecimal256) -> Result<Self, Self::Error> {
546        value
547            .atomics()
548            .try_into()
549            .map(Decimal256)
550            .map_err(|_| Decimal256RangeExceeded)
551    }
552}
553
554impl FromStr for Decimal256 {
555    type Err = StdError;
556
557    /// Converts the decimal string to a Decimal256
558    /// Possible inputs: "1.23", "1", "000012", "1.123000000"
559    /// Disallowed: "", ".23"
560    ///
561    /// This never performs any kind of rounding.
562    /// More than DECIMAL_PLACES fractional digits, even zeros, result in an error.
563    fn from_str(input: &str) -> Result<Self, Self::Err> {
564        let mut parts_iter = input.split('.');
565
566        let whole_part = parts_iter.next().unwrap(); // split always returns at least one element
567        let whole = whole_part
568            .parse::<Uint256>()
569            .map_err(|_| StdError::generic_err("Error parsing whole"))?;
570        let mut atomics = whole
571            .checked_mul(Self::DECIMAL_FRACTIONAL)
572            .map_err(|_| StdError::generic_err("Value too big"))?;
573
574        if let Some(fractional_part) = parts_iter.next() {
575            let fractional = fractional_part
576                .parse::<Uint256>()
577                .map_err(|_| StdError::generic_err("Error parsing fractional"))?;
578            let exp = (Self::DECIMAL_PLACES.checked_sub(fractional_part.len() as u32)).ok_or_else(
579                || {
580                    StdError::generic_err(format!(
581                        "Cannot parse more than {} fractional digits",
582                        Self::DECIMAL_PLACES
583                    ))
584                },
585            )?;
586            debug_assert!(exp <= Self::DECIMAL_PLACES);
587            let fractional_factor = Uint256::from(10u128).pow(exp);
588            atomics = atomics
589                .checked_add(
590                    // The inner multiplication can't overflow because
591                    // fractional < 10^DECIMAL_PLACES && fractional_factor <= 10^DECIMAL_PLACES
592                    fractional.checked_mul(fractional_factor).unwrap(),
593                )
594                .map_err(|_| StdError::generic_err("Value too big"))?;
595        }
596
597        if parts_iter.next().is_some() {
598            return Err(StdError::generic_err("Unexpected number of dots"));
599        }
600
601        Ok(Self(atomics))
602    }
603}
604
605impl fmt::Display for Decimal256 {
606    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
607        let whole = (self.0) / Self::DECIMAL_FRACTIONAL;
608        let fractional = (self.0).checked_rem(Self::DECIMAL_FRACTIONAL).unwrap();
609
610        if fractional.is_zero() {
611            write!(f, "{whole}")
612        } else {
613            let fractional_string = format!(
614                "{:0>padding$}",
615                fractional,
616                padding = Self::DECIMAL_PLACES as usize
617            );
618            f.write_str(&whole.to_string())?;
619            f.write_char('.')?;
620            f.write_str(fractional_string.trim_end_matches('0'))?;
621            Ok(())
622        }
623    }
624}
625
626impl fmt::Debug for Decimal256 {
627    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
628        write!(f, "Decimal256({self})")
629    }
630}
631
632impl Add for Decimal256 {
633    type Output = Self;
634
635    fn add(self, other: Self) -> Self {
636        Self(self.0 + other.0)
637    }
638}
639forward_ref_binop!(impl Add, add for Decimal256, Decimal256);
640
641impl AddAssign for Decimal256 {
642    fn add_assign(&mut self, rhs: Decimal256) {
643        *self = *self + rhs;
644    }
645}
646forward_ref_op_assign!(impl AddAssign, add_assign for Decimal256, Decimal256);
647
648impl Sub for Decimal256 {
649    type Output = Self;
650
651    fn sub(self, other: Self) -> Self {
652        Self(self.0 - other.0)
653    }
654}
655forward_ref_binop!(impl Sub, sub for Decimal256, Decimal256);
656
657impl SubAssign for Decimal256 {
658    fn sub_assign(&mut self, rhs: Decimal256) {
659        *self = *self - rhs;
660    }
661}
662forward_ref_op_assign!(impl SubAssign, sub_assign for Decimal256, Decimal256);
663
664impl Mul for Decimal256 {
665    type Output = Self;
666
667    #[allow(clippy::suspicious_arithmetic_impl)]
668    fn mul(self, other: Self) -> Self {
669        // Decimals are fractions. We can multiply two decimals a and b
670        // via
671        //       (a.numerator() * b.numerator()) / (a.denominator() * b.denominator())
672        //     = (a.numerator() * b.numerator()) / a.denominator() / b.denominator()
673
674        let result_as_uint512 = self.numerator().full_mul(other.numerator())
675            / Uint512::from_uint256(Self::DECIMAL_FRACTIONAL); // from_uint256 is a const method and should be "free"
676        match result_as_uint512.try_into() {
677            Ok(result) => Self(result),
678            Err(_) => panic!("attempt to multiply with overflow"),
679        }
680    }
681}
682forward_ref_binop!(impl Mul, mul for Decimal256, Decimal256);
683
684impl MulAssign for Decimal256 {
685    fn mul_assign(&mut self, rhs: Self) {
686        *self = *self * rhs;
687    }
688}
689forward_ref_op_assign!(impl MulAssign, mul_assign for Decimal256, Decimal256);
690
691impl Div for Decimal256 {
692    type Output = Self;
693
694    fn div(self, other: Self) -> Self {
695        match Decimal256::checked_from_ratio(self.numerator(), other.numerator()) {
696            Ok(ratio) => ratio,
697            Err(CheckedFromRatioError::DivideByZero) => {
698                panic!("Division failed - denominator must not be zero")
699            }
700            Err(CheckedFromRatioError::Overflow) => {
701                panic!("Division failed - multiplication overflow")
702            }
703        }
704    }
705}
706forward_ref_binop!(impl Div, div for Decimal256, Decimal256);
707
708impl DivAssign for Decimal256 {
709    fn div_assign(&mut self, rhs: Decimal256) {
710        *self = *self / rhs;
711    }
712}
713forward_ref_op_assign!(impl DivAssign, div_assign for Decimal256, Decimal256);
714
715impl Div<Uint256> for Decimal256 {
716    type Output = Self;
717
718    fn div(self, rhs: Uint256) -> Self::Output {
719        Self(self.0 / rhs)
720    }
721}
722
723impl DivAssign<Uint256> for Decimal256 {
724    fn div_assign(&mut self, rhs: Uint256) {
725        self.0 /= rhs;
726    }
727}
728
729impl Rem for Decimal256 {
730    type Output = Self;
731
732    /// # Panics
733    ///
734    /// This operation will panic if `rhs` is zero
735    #[inline]
736    fn rem(self, rhs: Self) -> Self {
737        Self(self.0.rem(rhs.0))
738    }
739}
740forward_ref_binop!(impl Rem, rem for Decimal256, Decimal256);
741
742impl RemAssign<Decimal256> for Decimal256 {
743    fn rem_assign(&mut self, rhs: Decimal256) {
744        *self = *self % rhs;
745    }
746}
747forward_ref_op_assign!(impl RemAssign, rem_assign for Decimal256, Decimal256);
748
749impl<A> core::iter::Sum<A> for Decimal256
750where
751    Self: Add<A, Output = Self>,
752{
753    fn sum<I: Iterator<Item = A>>(iter: I) -> Self {
754        iter.fold(Self::zero(), Add::add)
755    }
756}
757
758/// Serializes as a decimal string
759impl Serialize for Decimal256 {
760    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
761    where
762        S: ser::Serializer,
763    {
764        serializer.serialize_str(&self.to_string())
765    }
766}
767
768/// Deserializes as a base64 string
769impl<'de> Deserialize<'de> for Decimal256 {
770    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
771    where
772        D: Deserializer<'de>,
773    {
774        deserializer.deserialize_str(Decimal256Visitor)
775    }
776}
777
778struct Decimal256Visitor;
779
780impl de::Visitor<'_> for Decimal256Visitor {
781    type Value = Decimal256;
782
783    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
784        formatter.write_str("string-encoded decimal")
785    }
786
787    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
788    where
789        E: de::Error,
790    {
791        match Self::Value::from_str(v) {
792            Ok(d) => Ok(d),
793            Err(e) => Err(E::custom(format_args!("Error parsing decimal '{v}': {e}"))),
794        }
795    }
796}
797
798#[cfg(test)]
799mod tests {
800    use super::*;
801    use crate::errors::StdError;
802
803    use alloc::vec::Vec;
804
805    fn dec(input: &str) -> Decimal256 {
806        Decimal256::from_str(input).unwrap()
807    }
808
809    #[test]
810    fn decimal256_new() {
811        let expected = Uint256::from(300u128);
812        assert_eq!(Decimal256::new(expected).0, expected);
813    }
814
815    #[test]
816    #[allow(deprecated)]
817    fn decimal256_raw() {
818        let value = 300u128;
819        let expected = Uint256::from(value);
820        assert_eq!(Decimal256::raw(value).0, expected);
821    }
822
823    #[test]
824    fn decimal256_one() {
825        let value = Decimal256::one();
826        assert_eq!(value.0, Decimal256::DECIMAL_FRACTIONAL);
827    }
828
829    #[test]
830    fn decimal256_zero() {
831        let value = Decimal256::zero();
832        assert!(value.0.is_zero());
833    }
834
835    #[test]
836    fn decimal256_percent() {
837        let value = Decimal256::percent(50);
838        assert_eq!(value.0, Decimal256::DECIMAL_FRACTIONAL / Uint256::from(2u8));
839    }
840
841    #[test]
842    fn decimal256_permille() {
843        let value = Decimal256::permille(125);
844        assert_eq!(value.0, Decimal256::DECIMAL_FRACTIONAL / Uint256::from(8u8));
845    }
846
847    #[test]
848    fn decimal256_bps() {
849        let value = Decimal256::bps(125);
850        assert_eq!(
851            value.0,
852            Decimal256::DECIMAL_FRACTIONAL / Uint256::from(80u8)
853        );
854    }
855
856    #[test]
857    fn decimal256_from_atomics_works() {
858        let one = Decimal256::one();
859        let two = one + one;
860
861        assert_eq!(Decimal256::from_atomics(1u128, 0).unwrap(), one);
862        assert_eq!(Decimal256::from_atomics(10u128, 1).unwrap(), one);
863        assert_eq!(Decimal256::from_atomics(100u128, 2).unwrap(), one);
864        assert_eq!(Decimal256::from_atomics(1000u128, 3).unwrap(), one);
865        assert_eq!(
866            Decimal256::from_atomics(1000000000000000000u128, 18).unwrap(),
867            one
868        );
869        assert_eq!(
870            Decimal256::from_atomics(10000000000000000000u128, 19).unwrap(),
871            one
872        );
873        assert_eq!(
874            Decimal256::from_atomics(100000000000000000000u128, 20).unwrap(),
875            one
876        );
877
878        assert_eq!(Decimal256::from_atomics(2u128, 0).unwrap(), two);
879        assert_eq!(Decimal256::from_atomics(20u128, 1).unwrap(), two);
880        assert_eq!(Decimal256::from_atomics(200u128, 2).unwrap(), two);
881        assert_eq!(Decimal256::from_atomics(2000u128, 3).unwrap(), two);
882        assert_eq!(
883            Decimal256::from_atomics(2000000000000000000u128, 18).unwrap(),
884            two
885        );
886        assert_eq!(
887            Decimal256::from_atomics(20000000000000000000u128, 19).unwrap(),
888            two
889        );
890        assert_eq!(
891            Decimal256::from_atomics(200000000000000000000u128, 20).unwrap(),
892            two
893        );
894
895        // Cuts decimal digits (20 provided but only 18 can be stored)
896        assert_eq!(
897            Decimal256::from_atomics(4321u128, 20).unwrap(),
898            Decimal256::from_str("0.000000000000000043").unwrap()
899        );
900        assert_eq!(
901            Decimal256::from_atomics(6789u128, 20).unwrap(),
902            Decimal256::from_str("0.000000000000000067").unwrap()
903        );
904        assert_eq!(
905            Decimal256::from_atomics(u128::MAX, 38).unwrap(),
906            Decimal256::from_str("3.402823669209384634").unwrap()
907        );
908        assert_eq!(
909            Decimal256::from_atomics(u128::MAX, 39).unwrap(),
910            Decimal256::from_str("0.340282366920938463").unwrap()
911        );
912        assert_eq!(
913            Decimal256::from_atomics(u128::MAX, 45).unwrap(),
914            Decimal256::from_str("0.000000340282366920").unwrap()
915        );
916        assert_eq!(
917            Decimal256::from_atomics(u128::MAX, 51).unwrap(),
918            Decimal256::from_str("0.000000000000340282").unwrap()
919        );
920        assert_eq!(
921            Decimal256::from_atomics(u128::MAX, 56).unwrap(),
922            Decimal256::from_str("0.000000000000000003").unwrap()
923        );
924        assert_eq!(
925            Decimal256::from_atomics(u128::MAX, 57).unwrap(),
926            Decimal256::from_str("0.000000000000000000").unwrap()
927        );
928        assert_eq!(
929            Decimal256::from_atomics(u128::MAX, u32::MAX).unwrap(),
930            Decimal256::from_str("0.000000000000000000").unwrap()
931        );
932
933        // Can be used with max value
934        let max = Decimal256::MAX;
935        assert_eq!(
936            Decimal256::from_atomics(max.atomics(), max.decimal_places()).unwrap(),
937            max
938        );
939
940        // Overflow is only possible with digits < 18
941        let result = Decimal256::from_atomics(Uint256::MAX, 17);
942        assert_eq!(result.unwrap_err(), Decimal256RangeExceeded);
943    }
944
945    #[test]
946    fn decimal256_from_ratio_works() {
947        // 1.0
948        assert_eq!(Decimal256::from_ratio(1u128, 1u128), Decimal256::one());
949        assert_eq!(Decimal256::from_ratio(53u128, 53u128), Decimal256::one());
950        assert_eq!(Decimal256::from_ratio(125u128, 125u128), Decimal256::one());
951
952        // 1.5
953        assert_eq!(
954            Decimal256::from_ratio(3u128, 2u128),
955            Decimal256::percent(150)
956        );
957        assert_eq!(
958            Decimal256::from_ratio(150u128, 100u128),
959            Decimal256::percent(150)
960        );
961        assert_eq!(
962            Decimal256::from_ratio(333u128, 222u128),
963            Decimal256::percent(150)
964        );
965
966        // 0.125
967        assert_eq!(
968            Decimal256::from_ratio(1u64, 8u64),
969            Decimal256::permille(125)
970        );
971        assert_eq!(
972            Decimal256::from_ratio(125u64, 1000u64),
973            Decimal256::permille(125)
974        );
975
976        // 1/3 (result floored)
977        assert_eq!(
978            Decimal256::from_ratio(1u64, 3u64),
979            Decimal256(Uint256::from_str("333333333333333333").unwrap())
980        );
981
982        // 2/3 (result floored)
983        assert_eq!(
984            Decimal256::from_ratio(2u64, 3u64),
985            Decimal256(Uint256::from_str("666666666666666666").unwrap())
986        );
987
988        // large inputs
989        assert_eq!(Decimal256::from_ratio(0u128, u128::MAX), Decimal256::zero());
990        assert_eq!(
991            Decimal256::from_ratio(u128::MAX, u128::MAX),
992            Decimal256::one()
993        );
994        // 340282366920938463463 is the largest integer <= Decimal256::MAX
995        assert_eq!(
996            Decimal256::from_ratio(340282366920938463463u128, 1u128),
997            Decimal256::from_str("340282366920938463463").unwrap()
998        );
999    }
1000
1001    #[test]
1002    #[should_panic(expected = "Denominator must not be zero")]
1003    fn decimal256_from_ratio_panics_for_zero_denominator() {
1004        Decimal256::from_ratio(1u128, 0u128);
1005    }
1006
1007    #[test]
1008    #[should_panic(expected = "Multiplication overflow")]
1009    fn decimal256_from_ratio_panics_for_mul_overflow() {
1010        Decimal256::from_ratio(Uint256::MAX, 1u128);
1011    }
1012
1013    #[test]
1014    fn decimal256_checked_from_ratio_does_not_panic() {
1015        assert_eq!(
1016            Decimal256::checked_from_ratio(1u128, 0u128),
1017            Err(CheckedFromRatioError::DivideByZero)
1018        );
1019
1020        assert_eq!(
1021            Decimal256::checked_from_ratio(Uint256::MAX, 1u128),
1022            Err(CheckedFromRatioError::Overflow)
1023        );
1024    }
1025
1026    #[test]
1027    fn decimal256_implements_fraction() {
1028        let fraction = Decimal256::from_str("1234.567").unwrap();
1029        assert_eq!(
1030            fraction.numerator(),
1031            Uint256::from_str("1234567000000000000000").unwrap()
1032        );
1033        assert_eq!(
1034            fraction.denominator(),
1035            Uint256::from_str("1000000000000000000").unwrap()
1036        );
1037    }
1038
1039    #[test]
1040    fn decimal256_implements_from_decimal() {
1041        let a = Decimal::from_str("123.456").unwrap();
1042        let b = Decimal256::from(a);
1043        assert_eq!(b.to_string(), "123.456");
1044
1045        let a = Decimal::from_str("0").unwrap();
1046        let b = Decimal256::from(a);
1047        assert_eq!(b.to_string(), "0");
1048
1049        let a = Decimal::MAX;
1050        let b = Decimal256::from(a);
1051        assert_eq!(b.to_string(), "340282366920938463463.374607431768211455");
1052    }
1053
1054    #[test]
1055    fn decimal256_from_str_works() {
1056        // Integers
1057        assert_eq!(Decimal256::from_str("0").unwrap(), Decimal256::percent(0));
1058        assert_eq!(Decimal256::from_str("1").unwrap(), Decimal256::percent(100));
1059        assert_eq!(Decimal256::from_str("5").unwrap(), Decimal256::percent(500));
1060        assert_eq!(
1061            Decimal256::from_str("42").unwrap(),
1062            Decimal256::percent(4200)
1063        );
1064        assert_eq!(Decimal256::from_str("000").unwrap(), Decimal256::percent(0));
1065        assert_eq!(
1066            Decimal256::from_str("001").unwrap(),
1067            Decimal256::percent(100)
1068        );
1069        assert_eq!(
1070            Decimal256::from_str("005").unwrap(),
1071            Decimal256::percent(500)
1072        );
1073        assert_eq!(
1074            Decimal256::from_str("0042").unwrap(),
1075            Decimal256::percent(4200)
1076        );
1077
1078        // Decimals
1079        assert_eq!(
1080            Decimal256::from_str("1.0").unwrap(),
1081            Decimal256::percent(100)
1082        );
1083        assert_eq!(
1084            Decimal256::from_str("1.5").unwrap(),
1085            Decimal256::percent(150)
1086        );
1087        assert_eq!(
1088            Decimal256::from_str("0.5").unwrap(),
1089            Decimal256::percent(50)
1090        );
1091        assert_eq!(
1092            Decimal256::from_str("0.123").unwrap(),
1093            Decimal256::permille(123)
1094        );
1095
1096        assert_eq!(
1097            Decimal256::from_str("40.00").unwrap(),
1098            Decimal256::percent(4000)
1099        );
1100        assert_eq!(
1101            Decimal256::from_str("04.00").unwrap(),
1102            Decimal256::percent(400)
1103        );
1104        assert_eq!(
1105            Decimal256::from_str("00.40").unwrap(),
1106            Decimal256::percent(40)
1107        );
1108        assert_eq!(
1109            Decimal256::from_str("00.04").unwrap(),
1110            Decimal256::percent(4)
1111        );
1112
1113        // Can handle 18 fractional digits
1114        assert_eq!(
1115            Decimal256::from_str("7.123456789012345678").unwrap(),
1116            Decimal256(Uint256::from(7123456789012345678u128))
1117        );
1118        assert_eq!(
1119            Decimal256::from_str("7.999999999999999999").unwrap(),
1120            Decimal256(Uint256::from(7999999999999999999u128))
1121        );
1122
1123        // Works for documented max value
1124        assert_eq!(
1125            Decimal256::from_str(
1126                "115792089237316195423570985008687907853269984665640564039457.584007913129639935"
1127            )
1128            .unwrap(),
1129            Decimal256::MAX
1130        );
1131    }
1132
1133    #[test]
1134    fn decimal256_from_str_errors_for_broken_whole_part() {
1135        match Decimal256::from_str("").unwrap_err() {
1136            StdError::GenericErr { msg, .. } => assert_eq!(msg, "Error parsing whole"),
1137            e => panic!("Unexpected error: {e:?}"),
1138        }
1139
1140        match Decimal256::from_str(" ").unwrap_err() {
1141            StdError::GenericErr { msg, .. } => assert_eq!(msg, "Error parsing whole"),
1142            e => panic!("Unexpected error: {e:?}"),
1143        }
1144
1145        match Decimal256::from_str("-1").unwrap_err() {
1146            StdError::GenericErr { msg, .. } => assert_eq!(msg, "Error parsing whole"),
1147            e => panic!("Unexpected error: {e:?}"),
1148        }
1149    }
1150
1151    #[test]
1152    fn decimal256_from_str_errors_for_broken_fractional_part() {
1153        match Decimal256::from_str("1.").unwrap_err() {
1154            StdError::GenericErr { msg, .. } => assert_eq!(msg, "Error parsing fractional"),
1155            e => panic!("Unexpected error: {e:?}"),
1156        }
1157
1158        match Decimal256::from_str("1. ").unwrap_err() {
1159            StdError::GenericErr { msg, .. } => assert_eq!(msg, "Error parsing fractional"),
1160            e => panic!("Unexpected error: {e:?}"),
1161        }
1162
1163        match Decimal256::from_str("1.e").unwrap_err() {
1164            StdError::GenericErr { msg, .. } => assert_eq!(msg, "Error parsing fractional"),
1165            e => panic!("Unexpected error: {e:?}"),
1166        }
1167
1168        match Decimal256::from_str("1.2e3").unwrap_err() {
1169            StdError::GenericErr { msg, .. } => assert_eq!(msg, "Error parsing fractional"),
1170            e => panic!("Unexpected error: {e:?}"),
1171        }
1172    }
1173
1174    #[test]
1175    fn decimal256_from_str_errors_for_more_than_36_fractional_digits() {
1176        match Decimal256::from_str("7.1234567890123456789").unwrap_err() {
1177            StdError::GenericErr { msg, .. } => {
1178                assert_eq!(msg, "Cannot parse more than 18 fractional digits")
1179            }
1180            e => panic!("Unexpected error: {e:?}"),
1181        }
1182
1183        // No special rules for trailing zeros. This could be changed but adds gas cost for the happy path.
1184        match Decimal256::from_str("7.1230000000000000000").unwrap_err() {
1185            StdError::GenericErr { msg, .. } => {
1186                assert_eq!(msg, "Cannot parse more than 18 fractional digits")
1187            }
1188            e => panic!("Unexpected error: {e:?}"),
1189        }
1190    }
1191
1192    #[test]
1193    fn decimal256_from_str_errors_for_invalid_number_of_dots() {
1194        match Decimal256::from_str("1.2.3").unwrap_err() {
1195            StdError::GenericErr { msg, .. } => assert_eq!(msg, "Unexpected number of dots"),
1196            e => panic!("Unexpected error: {e:?}"),
1197        }
1198
1199        match Decimal256::from_str("1.2.3.4").unwrap_err() {
1200            StdError::GenericErr { msg, .. } => assert_eq!(msg, "Unexpected number of dots"),
1201            e => panic!("Unexpected error: {e:?}"),
1202        }
1203    }
1204
1205    #[test]
1206    fn decimal256_from_str_errors_for_more_than_max_value() {
1207        // Integer
1208        match Decimal256::from_str("115792089237316195423570985008687907853269984665640564039458")
1209            .unwrap_err()
1210        {
1211            StdError::GenericErr { msg, .. } => assert_eq!(msg, "Value too big"),
1212            e => panic!("Unexpected error: {e:?}"),
1213        }
1214
1215        // Decimal
1216        match Decimal256::from_str("115792089237316195423570985008687907853269984665640564039458.0")
1217            .unwrap_err()
1218        {
1219            StdError::GenericErr { msg, .. } => assert_eq!(msg, "Value too big"),
1220            e => panic!("Unexpected error: {e:?}"),
1221        }
1222        match Decimal256::from_str(
1223            "115792089237316195423570985008687907853269984665640564039457.584007913129639936",
1224        )
1225        .unwrap_err()
1226        {
1227            StdError::GenericErr { msg, .. } => assert_eq!(msg, "Value too big"),
1228            e => panic!("Unexpected error: {e:?}"),
1229        }
1230    }
1231
1232    #[test]
1233    fn decimal256_atomics_works() {
1234        let zero = Decimal256::zero();
1235        let one = Decimal256::one();
1236        let half = Decimal256::percent(50);
1237        let two = Decimal256::percent(200);
1238        let max = Decimal256::MAX;
1239
1240        assert_eq!(zero.atomics(), Uint256::from(0u128));
1241        assert_eq!(one.atomics(), Uint256::from(1000000000000000000u128));
1242        assert_eq!(half.atomics(), Uint256::from(500000000000000000u128));
1243        assert_eq!(two.atomics(), Uint256::from(2000000000000000000u128));
1244        assert_eq!(max.atomics(), Uint256::MAX);
1245    }
1246
1247    #[test]
1248    fn decimal256_decimal_places_works() {
1249        let zero = Decimal256::zero();
1250        let one = Decimal256::one();
1251        let half = Decimal256::percent(50);
1252        let two = Decimal256::percent(200);
1253        let max = Decimal256::MAX;
1254
1255        assert_eq!(zero.decimal_places(), 18);
1256        assert_eq!(one.decimal_places(), 18);
1257        assert_eq!(half.decimal_places(), 18);
1258        assert_eq!(two.decimal_places(), 18);
1259        assert_eq!(max.decimal_places(), 18);
1260    }
1261
1262    #[test]
1263    fn decimal256_is_zero_works() {
1264        assert!(Decimal256::zero().is_zero());
1265        assert!(Decimal256::percent(0).is_zero());
1266        assert!(Decimal256::permille(0).is_zero());
1267
1268        assert!(!Decimal256::one().is_zero());
1269        assert!(!Decimal256::percent(123).is_zero());
1270        assert!(!Decimal256::permille(1234).is_zero());
1271    }
1272
1273    #[test]
1274    fn decimal256_inv_works() {
1275        // d = 0
1276        assert_eq!(Decimal256::zero().inv(), None);
1277
1278        // d == 1
1279        assert_eq!(Decimal256::one().inv(), Some(Decimal256::one()));
1280
1281        // d > 1 exact
1282        assert_eq!(
1283            Decimal256::from_str("2").unwrap().inv(),
1284            Some(Decimal256::from_str("0.5").unwrap())
1285        );
1286        assert_eq!(
1287            Decimal256::from_str("20").unwrap().inv(),
1288            Some(Decimal256::from_str("0.05").unwrap())
1289        );
1290        assert_eq!(
1291            Decimal256::from_str("200").unwrap().inv(),
1292            Some(Decimal256::from_str("0.005").unwrap())
1293        );
1294        assert_eq!(
1295            Decimal256::from_str("2000").unwrap().inv(),
1296            Some(Decimal256::from_str("0.0005").unwrap())
1297        );
1298
1299        // d > 1 rounded
1300        assert_eq!(
1301            Decimal256::from_str("3").unwrap().inv(),
1302            Some(Decimal256::from_str("0.333333333333333333").unwrap())
1303        );
1304        assert_eq!(
1305            Decimal256::from_str("6").unwrap().inv(),
1306            Some(Decimal256::from_str("0.166666666666666666").unwrap())
1307        );
1308
1309        // d < 1 exact
1310        assert_eq!(
1311            Decimal256::from_str("0.5").unwrap().inv(),
1312            Some(Decimal256::from_str("2").unwrap())
1313        );
1314        assert_eq!(
1315            Decimal256::from_str("0.05").unwrap().inv(),
1316            Some(Decimal256::from_str("20").unwrap())
1317        );
1318        assert_eq!(
1319            Decimal256::from_str("0.005").unwrap().inv(),
1320            Some(Decimal256::from_str("200").unwrap())
1321        );
1322        assert_eq!(
1323            Decimal256::from_str("0.0005").unwrap().inv(),
1324            Some(Decimal256::from_str("2000").unwrap())
1325        );
1326    }
1327
1328    #[test]
1329    #[allow(clippy::op_ref)]
1330    fn decimal256_add_works() {
1331        let value = Decimal256::one() + Decimal256::percent(50); // 1.5
1332        assert_eq!(
1333            value.0,
1334            Decimal256::DECIMAL_FRACTIONAL * Uint256::from(3u8) / Uint256::from(2u8)
1335        );
1336
1337        assert_eq!(
1338            Decimal256::percent(5) + Decimal256::percent(4),
1339            Decimal256::percent(9)
1340        );
1341        assert_eq!(
1342            Decimal256::percent(5) + Decimal256::zero(),
1343            Decimal256::percent(5)
1344        );
1345        assert_eq!(Decimal256::zero() + Decimal256::zero(), Decimal256::zero());
1346
1347        // works for refs
1348        let a = Decimal256::percent(15);
1349        let b = Decimal256::percent(25);
1350        let expected = Decimal256::percent(40);
1351        assert_eq!(a + b, expected);
1352        assert_eq!(&a + b, expected);
1353        assert_eq!(a + &b, expected);
1354        assert_eq!(&a + &b, expected);
1355    }
1356
1357    #[test]
1358    #[should_panic(expected = "attempt to add with overflow")]
1359    fn decimal256_add_overflow_panics() {
1360        let _value = Decimal256::MAX + Decimal256::percent(50);
1361    }
1362
1363    #[test]
1364    fn decimal256_add_assign_works() {
1365        let mut a = Decimal256::percent(30);
1366        a += Decimal256::percent(20);
1367        assert_eq!(a, Decimal256::percent(50));
1368
1369        // works for refs
1370        let mut a = Decimal256::percent(15);
1371        let b = Decimal256::percent(3);
1372        let expected = Decimal256::percent(18);
1373        a += &b;
1374        assert_eq!(a, expected);
1375    }
1376
1377    #[test]
1378    #[allow(clippy::op_ref)]
1379    fn decimal256_sub_works() {
1380        let value = Decimal256::one() - Decimal256::percent(50); // 0.5
1381        assert_eq!(value.0, Decimal256::DECIMAL_FRACTIONAL / Uint256::from(2u8));
1382
1383        assert_eq!(
1384            Decimal256::percent(9) - Decimal256::percent(4),
1385            Decimal256::percent(5)
1386        );
1387        assert_eq!(
1388            Decimal256::percent(16) - Decimal256::zero(),
1389            Decimal256::percent(16)
1390        );
1391        assert_eq!(
1392            Decimal256::percent(16) - Decimal256::percent(16),
1393            Decimal256::zero()
1394        );
1395        assert_eq!(Decimal256::zero() - Decimal256::zero(), Decimal256::zero());
1396
1397        // works for refs
1398        let a = Decimal256::percent(13);
1399        let b = Decimal256::percent(6);
1400        let expected = Decimal256::percent(7);
1401        assert_eq!(a - b, expected);
1402        assert_eq!(&a - b, expected);
1403        assert_eq!(a - &b, expected);
1404        assert_eq!(&a - &b, expected);
1405    }
1406
1407    #[test]
1408    #[should_panic(expected = "attempt to subtract with overflow")]
1409    fn decimal256_sub_overflow_panics() {
1410        let _value = Decimal256::zero() - Decimal256::percent(50);
1411    }
1412
1413    #[test]
1414    fn decimal256_sub_assign_works() {
1415        let mut a = Decimal256::percent(20);
1416        a -= Decimal256::percent(2);
1417        assert_eq!(a, Decimal256::percent(18));
1418
1419        // works for refs
1420        let mut a = Decimal256::percent(33);
1421        let b = Decimal256::percent(13);
1422        let expected = Decimal256::percent(20);
1423        a -= &b;
1424        assert_eq!(a, expected);
1425    }
1426
1427    #[test]
1428    #[allow(clippy::op_ref)]
1429    fn decimal256_implements_mul() {
1430        let one = Decimal256::one();
1431        let two = one + one;
1432        let half = Decimal256::percent(50);
1433
1434        // 1*x and x*1
1435        assert_eq!(one * Decimal256::percent(0), Decimal256::percent(0));
1436        assert_eq!(one * Decimal256::percent(1), Decimal256::percent(1));
1437        assert_eq!(one * Decimal256::percent(10), Decimal256::percent(10));
1438        assert_eq!(one * Decimal256::percent(100), Decimal256::percent(100));
1439        assert_eq!(one * Decimal256::percent(1000), Decimal256::percent(1000));
1440        assert_eq!(one * Decimal256::MAX, Decimal256::MAX);
1441        assert_eq!(Decimal256::percent(0) * one, Decimal256::percent(0));
1442        assert_eq!(Decimal256::percent(1) * one, Decimal256::percent(1));
1443        assert_eq!(Decimal256::percent(10) * one, Decimal256::percent(10));
1444        assert_eq!(Decimal256::percent(100) * one, Decimal256::percent(100));
1445        assert_eq!(Decimal256::percent(1000) * one, Decimal256::percent(1000));
1446        assert_eq!(Decimal256::MAX * one, Decimal256::MAX);
1447
1448        // double
1449        assert_eq!(two * Decimal256::percent(0), Decimal256::percent(0));
1450        assert_eq!(two * Decimal256::percent(1), Decimal256::percent(2));
1451        assert_eq!(two * Decimal256::percent(10), Decimal256::percent(20));
1452        assert_eq!(two * Decimal256::percent(100), Decimal256::percent(200));
1453        assert_eq!(two * Decimal256::percent(1000), Decimal256::percent(2000));
1454        assert_eq!(Decimal256::percent(0) * two, Decimal256::percent(0));
1455        assert_eq!(Decimal256::percent(1) * two, Decimal256::percent(2));
1456        assert_eq!(Decimal256::percent(10) * two, Decimal256::percent(20));
1457        assert_eq!(Decimal256::percent(100) * two, Decimal256::percent(200));
1458        assert_eq!(Decimal256::percent(1000) * two, Decimal256::percent(2000));
1459
1460        // half
1461        assert_eq!(half * Decimal256::percent(0), Decimal256::percent(0));
1462        assert_eq!(half * Decimal256::percent(1), Decimal256::permille(5));
1463        assert_eq!(half * Decimal256::percent(10), Decimal256::percent(5));
1464        assert_eq!(half * Decimal256::percent(100), Decimal256::percent(50));
1465        assert_eq!(half * Decimal256::percent(1000), Decimal256::percent(500));
1466        assert_eq!(Decimal256::percent(0) * half, Decimal256::percent(0));
1467        assert_eq!(Decimal256::percent(1) * half, Decimal256::permille(5));
1468        assert_eq!(Decimal256::percent(10) * half, Decimal256::percent(5));
1469        assert_eq!(Decimal256::percent(100) * half, Decimal256::percent(50));
1470        assert_eq!(Decimal256::percent(1000) * half, Decimal256::percent(500));
1471
1472        // Move left
1473        let a = dec("123.127726548762582");
1474        assert_eq!(a * dec("1"), dec("123.127726548762582"));
1475        assert_eq!(a * dec("10"), dec("1231.27726548762582"));
1476        assert_eq!(a * dec("100"), dec("12312.7726548762582"));
1477        assert_eq!(a * dec("1000"), dec("123127.726548762582"));
1478        assert_eq!(a * dec("1000000"), dec("123127726.548762582"));
1479        assert_eq!(a * dec("1000000000"), dec("123127726548.762582"));
1480        assert_eq!(a * dec("1000000000000"), dec("123127726548762.582"));
1481        assert_eq!(a * dec("1000000000000000"), dec("123127726548762582"));
1482        assert_eq!(a * dec("1000000000000000000"), dec("123127726548762582000"));
1483        assert_eq!(dec("1") * a, dec("123.127726548762582"));
1484        assert_eq!(dec("10") * a, dec("1231.27726548762582"));
1485        assert_eq!(dec("100") * a, dec("12312.7726548762582"));
1486        assert_eq!(dec("1000") * a, dec("123127.726548762582"));
1487        assert_eq!(dec("1000000") * a, dec("123127726.548762582"));
1488        assert_eq!(dec("1000000000") * a, dec("123127726548.762582"));
1489        assert_eq!(dec("1000000000000") * a, dec("123127726548762.582"));
1490        assert_eq!(dec("1000000000000000") * a, dec("123127726548762582"));
1491        assert_eq!(dec("1000000000000000000") * a, dec("123127726548762582000"));
1492
1493        // Move right
1494        let max = Decimal256::MAX;
1495        assert_eq!(
1496            max * dec("1.0"),
1497            dec("115792089237316195423570985008687907853269984665640564039457.584007913129639935")
1498        );
1499        assert_eq!(
1500            max * dec("0.1"),
1501            dec("11579208923731619542357098500868790785326998466564056403945.758400791312963993")
1502        );
1503        assert_eq!(
1504            max * dec("0.01"),
1505            dec("1157920892373161954235709850086879078532699846656405640394.575840079131296399")
1506        );
1507        assert_eq!(
1508            max * dec("0.001"),
1509            dec("115792089237316195423570985008687907853269984665640564039.457584007913129639")
1510        );
1511        assert_eq!(
1512            max * dec("0.000001"),
1513            dec("115792089237316195423570985008687907853269984665640564.039457584007913129")
1514        );
1515        assert_eq!(
1516            max * dec("0.000000001"),
1517            dec("115792089237316195423570985008687907853269984665640.564039457584007913")
1518        );
1519        assert_eq!(
1520            max * dec("0.000000000001"),
1521            dec("115792089237316195423570985008687907853269984665.640564039457584007")
1522        );
1523        assert_eq!(
1524            max * dec("0.000000000000001"),
1525            dec("115792089237316195423570985008687907853269984.665640564039457584")
1526        );
1527        assert_eq!(
1528            max * dec("0.000000000000000001"),
1529            dec("115792089237316195423570985008687907853269.984665640564039457")
1530        );
1531
1532        // works for refs
1533        let a = Decimal256::percent(20);
1534        let b = Decimal256::percent(30);
1535        let expected = Decimal256::percent(6);
1536        assert_eq!(a * b, expected);
1537        assert_eq!(&a * b, expected);
1538        assert_eq!(a * &b, expected);
1539        assert_eq!(&a * &b, expected);
1540    }
1541
1542    #[test]
1543    fn decimal256_mul_assign_works() {
1544        let mut a = Decimal256::percent(15);
1545        a *= Decimal256::percent(60);
1546        assert_eq!(a, Decimal256::percent(9));
1547
1548        // works for refs
1549        let mut a = Decimal256::percent(50);
1550        let b = Decimal256::percent(20);
1551        a *= &b;
1552        assert_eq!(a, Decimal256::percent(10));
1553    }
1554
1555    #[test]
1556    #[should_panic(expected = "attempt to multiply with overflow")]
1557    fn decimal256_mul_overflow_panics() {
1558        let _value = Decimal256::MAX * Decimal256::percent(101);
1559    }
1560
1561    #[test]
1562    fn decimal256_checked_mul() {
1563        let test_data = [
1564            (Decimal256::zero(), Decimal256::zero()),
1565            (Decimal256::zero(), Decimal256::one()),
1566            (Decimal256::one(), Decimal256::zero()),
1567            (Decimal256::percent(10), Decimal256::zero()),
1568            (Decimal256::percent(10), Decimal256::percent(5)),
1569            (Decimal256::MAX, Decimal256::one()),
1570            (
1571                Decimal256::MAX / Uint256::from_uint128(2u128.into()),
1572                Decimal256::percent(200),
1573            ),
1574            (Decimal256::permille(6), Decimal256::permille(13)),
1575        ];
1576
1577        // The regular core::ops::Mul is our source of truth for these tests.
1578        for (x, y) in test_data.into_iter() {
1579            assert_eq!(x * y, x.checked_mul(y).unwrap());
1580        }
1581    }
1582
1583    #[test]
1584    fn decimal256_checked_mul_overflow() {
1585        assert_eq!(
1586            Decimal256::MAX.checked_mul(Decimal256::percent(200)),
1587            Err(OverflowError::new(OverflowOperation::Mul))
1588        );
1589    }
1590
1591    #[test]
1592    #[allow(clippy::op_ref)]
1593    fn decimal256_implements_div() {
1594        let one = Decimal256::one();
1595        let two = one + one;
1596        let half = Decimal256::percent(50);
1597
1598        // 1/x and x/1
1599        assert_eq!(one / Decimal256::percent(1), Decimal256::percent(10_000));
1600        assert_eq!(one / Decimal256::percent(10), Decimal256::percent(1_000));
1601        assert_eq!(one / Decimal256::percent(100), Decimal256::percent(100));
1602        assert_eq!(one / Decimal256::percent(1000), Decimal256::percent(10));
1603        assert_eq!(Decimal256::percent(0) / one, Decimal256::percent(0));
1604        assert_eq!(Decimal256::percent(1) / one, Decimal256::percent(1));
1605        assert_eq!(Decimal256::percent(10) / one, Decimal256::percent(10));
1606        assert_eq!(Decimal256::percent(100) / one, Decimal256::percent(100));
1607        assert_eq!(Decimal256::percent(1000) / one, Decimal256::percent(1000));
1608
1609        // double
1610        assert_eq!(two / Decimal256::percent(1), Decimal256::percent(20_000));
1611        assert_eq!(two / Decimal256::percent(10), Decimal256::percent(2_000));
1612        assert_eq!(two / Decimal256::percent(100), Decimal256::percent(200));
1613        assert_eq!(two / Decimal256::percent(1000), Decimal256::percent(20));
1614        assert_eq!(Decimal256::percent(0) / two, Decimal256::percent(0));
1615        assert_eq!(Decimal256::percent(1) / two, dec("0.005"));
1616        assert_eq!(Decimal256::percent(10) / two, Decimal256::percent(5));
1617        assert_eq!(Decimal256::percent(100) / two, Decimal256::percent(50));
1618        assert_eq!(Decimal256::percent(1000) / two, Decimal256::percent(500));
1619
1620        // half
1621        assert_eq!(half / Decimal256::percent(1), Decimal256::percent(5_000));
1622        assert_eq!(half / Decimal256::percent(10), Decimal256::percent(500));
1623        assert_eq!(half / Decimal256::percent(100), Decimal256::percent(50));
1624        assert_eq!(half / Decimal256::percent(1000), Decimal256::percent(5));
1625        assert_eq!(Decimal256::percent(0) / half, Decimal256::percent(0));
1626        assert_eq!(Decimal256::percent(1) / half, Decimal256::percent(2));
1627        assert_eq!(Decimal256::percent(10) / half, Decimal256::percent(20));
1628        assert_eq!(Decimal256::percent(100) / half, Decimal256::percent(200));
1629        assert_eq!(Decimal256::percent(1000) / half, Decimal256::percent(2000));
1630
1631        // Move right
1632        let a = dec("123127726548762582");
1633        assert_eq!(a / dec("1"), dec("123127726548762582"));
1634        assert_eq!(a / dec("10"), dec("12312772654876258.2"));
1635        assert_eq!(a / dec("100"), dec("1231277265487625.82"));
1636        assert_eq!(a / dec("1000"), dec("123127726548762.582"));
1637        assert_eq!(a / dec("1000000"), dec("123127726548.762582"));
1638        assert_eq!(a / dec("1000000000"), dec("123127726.548762582"));
1639        assert_eq!(a / dec("1000000000000"), dec("123127.726548762582"));
1640        assert_eq!(a / dec("1000000000000000"), dec("123.127726548762582"));
1641        assert_eq!(a / dec("1000000000000000000"), dec("0.123127726548762582"));
1642        assert_eq!(dec("1") / a, dec("0.000000000000000008"));
1643        assert_eq!(dec("10") / a, dec("0.000000000000000081"));
1644        assert_eq!(dec("100") / a, dec("0.000000000000000812"));
1645        assert_eq!(dec("1000") / a, dec("0.000000000000008121"));
1646        assert_eq!(dec("1000000") / a, dec("0.000000000008121647"));
1647        assert_eq!(dec("1000000000") / a, dec("0.000000008121647560"));
1648        assert_eq!(dec("1000000000000") / a, dec("0.000008121647560868"));
1649        assert_eq!(dec("1000000000000000") / a, dec("0.008121647560868164"));
1650        assert_eq!(dec("1000000000000000000") / a, dec("8.121647560868164773"));
1651
1652        // Move left
1653        let a = dec("0.123127726548762582");
1654        assert_eq!(a / dec("1.0"), dec("0.123127726548762582"));
1655        assert_eq!(a / dec("0.1"), dec("1.23127726548762582"));
1656        assert_eq!(a / dec("0.01"), dec("12.3127726548762582"));
1657        assert_eq!(a / dec("0.001"), dec("123.127726548762582"));
1658        assert_eq!(a / dec("0.000001"), dec("123127.726548762582"));
1659        assert_eq!(a / dec("0.000000001"), dec("123127726.548762582"));
1660        assert_eq!(a / dec("0.000000000001"), dec("123127726548.762582"));
1661        assert_eq!(a / dec("0.000000000000001"), dec("123127726548762.582"));
1662        assert_eq!(a / dec("0.000000000000000001"), dec("123127726548762582"));
1663
1664        assert_eq!(
1665            Decimal256::percent(15) / Decimal256::percent(60),
1666            Decimal256::percent(25)
1667        );
1668
1669        // works for refs
1670        let a = Decimal256::percent(100);
1671        let b = Decimal256::percent(20);
1672        let expected = Decimal256::percent(500);
1673        assert_eq!(a / b, expected);
1674        assert_eq!(&a / b, expected);
1675        assert_eq!(a / &b, expected);
1676        assert_eq!(&a / &b, expected);
1677    }
1678
1679    #[test]
1680    fn decimal256_div_assign_works() {
1681        let mut a = Decimal256::percent(15);
1682        a /= Decimal256::percent(20);
1683        assert_eq!(a, Decimal256::percent(75));
1684
1685        // works for refs
1686        let mut a = Decimal256::percent(50);
1687        let b = Decimal256::percent(20);
1688        a /= &b;
1689        assert_eq!(a, Decimal256::percent(250));
1690    }
1691
1692    #[test]
1693    #[should_panic(expected = "Division failed - multiplication overflow")]
1694    fn decimal256_div_overflow_panics() {
1695        let _value = Decimal256::MAX / Decimal256::percent(10);
1696    }
1697
1698    #[test]
1699    #[should_panic(expected = "Division failed - denominator must not be zero")]
1700    fn decimal256_div_by_zero_panics() {
1701        let _value = Decimal256::one() / Decimal256::zero();
1702    }
1703
1704    #[test]
1705    fn decimal256_uint128_division() {
1706        // a/b
1707        let left = Decimal256::percent(150); // 1.5
1708        let right = Uint256::from(3u128);
1709        assert_eq!(left / right, Decimal256::percent(50));
1710
1711        // 0/a
1712        let left = Decimal256::zero();
1713        let right = Uint256::from(300u128);
1714        assert_eq!(left / right, Decimal256::zero());
1715    }
1716
1717    #[test]
1718    #[should_panic(expected = "attempt to divide by zero")]
1719    fn decimal256_uint128_divide_by_zero() {
1720        let left = Decimal256::percent(150); // 1.5
1721        let right = Uint256::from(0u128);
1722        let _result = left / right;
1723    }
1724
1725    #[test]
1726    fn decimal256_uint128_div_assign() {
1727        // a/b
1728        let mut dec = Decimal256::percent(150); // 1.5
1729        dec /= Uint256::from(3u128);
1730        assert_eq!(dec, Decimal256::percent(50));
1731
1732        // 0/a
1733        let mut dec = Decimal256::zero();
1734        dec /= Uint256::from(300u128);
1735        assert_eq!(dec, Decimal256::zero());
1736    }
1737
1738    #[test]
1739    #[should_panic(expected = "attempt to divide by zero")]
1740    fn decimal256_uint128_div_assign_by_zero() {
1741        // a/0
1742        let mut dec = Decimal256::percent(50);
1743        dec /= Uint256::from(0u128);
1744    }
1745
1746    #[test]
1747    fn decimal256_uint128_sqrt() {
1748        assert_eq!(Decimal256::percent(900).sqrt(), Decimal256::percent(300));
1749
1750        assert!(Decimal256::percent(316) < Decimal256::percent(1000).sqrt());
1751        assert!(Decimal256::percent(1000).sqrt() < Decimal256::percent(317));
1752    }
1753
1754    /// sqrt(2) is an irrational number, i.e. all 36 decimal places should be used.
1755    #[test]
1756    fn decimal256_uint128_sqrt_is_precise() {
1757        assert_eq!(
1758            Decimal256::from_str("2").unwrap().sqrt(),
1759            Decimal256::from_str("1.414213562373095048").unwrap() // https://www.wolframalpha.com/input/?i=sqrt%282%29
1760        );
1761    }
1762
1763    #[test]
1764    fn decimal256_uint128_sqrt_does_not_overflow() {
1765        assert_eq!(
1766            Decimal256::from_str("40000000000000000000000000000000000000000000000000000000000")
1767                .unwrap()
1768                .sqrt(),
1769            Decimal256::from_str("200000000000000000000000000000").unwrap()
1770        );
1771    }
1772
1773    #[test]
1774    fn decimal256_uint128_sqrt_intermediate_precision_used() {
1775        assert_eq!(
1776            Decimal256::from_str("40000000000000000000000000000000000000000000000001")
1777                .unwrap()
1778                .sqrt(),
1779            // The last few digits (39110) are truncated below due to the algorithm
1780            // we use. Larger numbers will cause less precision.
1781            // https://www.wolframalpha.com/input/?i=sqrt%2840000000000000000000000000000000000000000000000001%29
1782            Decimal256::from_str("6324555320336758663997787.088865437067400000").unwrap()
1783        );
1784    }
1785
1786    #[test]
1787    fn decimal256_checked_pow() {
1788        for exp in 0..10 {
1789            assert_eq!(
1790                Decimal256::one().checked_pow(exp).unwrap(),
1791                Decimal256::one()
1792            );
1793        }
1794
1795        // This case is mathematically undefined but we ensure consistency with Rust standard types
1796        // https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=20df6716048e77087acd40194b233494
1797        assert_eq!(
1798            Decimal256::zero().checked_pow(0).unwrap(),
1799            Decimal256::one()
1800        );
1801
1802        for exp in 1..10 {
1803            assert_eq!(
1804                Decimal256::zero().checked_pow(exp).unwrap(),
1805                Decimal256::zero()
1806            );
1807        }
1808
1809        for num in &[
1810            Decimal256::percent(50),
1811            Decimal256::percent(99),
1812            Decimal256::percent(200),
1813        ] {
1814            assert_eq!(num.checked_pow(0).unwrap(), Decimal256::one())
1815        }
1816
1817        assert_eq!(
1818            Decimal256::percent(20).checked_pow(2).unwrap(),
1819            Decimal256::percent(4)
1820        );
1821
1822        assert_eq!(
1823            Decimal256::percent(20).checked_pow(3).unwrap(),
1824            Decimal256::permille(8)
1825        );
1826
1827        assert_eq!(
1828            Decimal256::percent(200).checked_pow(4).unwrap(),
1829            Decimal256::percent(1600)
1830        );
1831
1832        assert_eq!(
1833            Decimal256::percent(200).checked_pow(4).unwrap(),
1834            Decimal256::percent(1600)
1835        );
1836
1837        assert_eq!(
1838            Decimal256::percent(700).checked_pow(5).unwrap(),
1839            Decimal256::percent(1680700)
1840        );
1841
1842        assert_eq!(
1843            Decimal256::percent(700).checked_pow(8).unwrap(),
1844            Decimal256::percent(576480100)
1845        );
1846
1847        assert_eq!(
1848            Decimal256::percent(700).checked_pow(10).unwrap(),
1849            Decimal256::percent(28247524900)
1850        );
1851
1852        assert_eq!(
1853            Decimal256::percent(120).checked_pow(123).unwrap(),
1854            Decimal256(5486473221892422150877397607u128.into())
1855        );
1856
1857        assert_eq!(
1858            Decimal256::percent(10).checked_pow(2).unwrap(),
1859            Decimal256(10000000000000000u128.into())
1860        );
1861
1862        assert_eq!(
1863            Decimal256::percent(10).checked_pow(18).unwrap(),
1864            Decimal256(1u128.into())
1865        );
1866    }
1867
1868    #[test]
1869    fn decimal256_checked_pow_overflow() {
1870        assert_eq!(
1871            Decimal256::MAX.checked_pow(2),
1872            Err(OverflowError::new(OverflowOperation::Pow))
1873        );
1874    }
1875
1876    #[test]
1877    fn decimal256_to_string() {
1878        // Integers
1879        assert_eq!(Decimal256::zero().to_string(), "0");
1880        assert_eq!(Decimal256::one().to_string(), "1");
1881        assert_eq!(Decimal256::percent(500).to_string(), "5");
1882
1883        // Decimals
1884        assert_eq!(Decimal256::percent(125).to_string(), "1.25");
1885        assert_eq!(Decimal256::percent(42638).to_string(), "426.38");
1886        assert_eq!(Decimal256::percent(3).to_string(), "0.03");
1887        assert_eq!(Decimal256::permille(987).to_string(), "0.987");
1888
1889        assert_eq!(
1890            Decimal256(Uint256::from(1u128)).to_string(),
1891            "0.000000000000000001"
1892        );
1893        assert_eq!(
1894            Decimal256(Uint256::from(10u128)).to_string(),
1895            "0.00000000000000001"
1896        );
1897        assert_eq!(
1898            Decimal256(Uint256::from(100u128)).to_string(),
1899            "0.0000000000000001"
1900        );
1901        assert_eq!(
1902            Decimal256(Uint256::from(1000u128)).to_string(),
1903            "0.000000000000001"
1904        );
1905        assert_eq!(
1906            Decimal256(Uint256::from(10000u128)).to_string(),
1907            "0.00000000000001"
1908        );
1909        assert_eq!(
1910            Decimal256(Uint256::from(100000u128)).to_string(),
1911            "0.0000000000001"
1912        );
1913        assert_eq!(
1914            Decimal256(Uint256::from(1000000u128)).to_string(),
1915            "0.000000000001"
1916        );
1917        assert_eq!(
1918            Decimal256(Uint256::from(10000000u128)).to_string(),
1919            "0.00000000001"
1920        );
1921        assert_eq!(
1922            Decimal256(Uint256::from(100000000u128)).to_string(),
1923            "0.0000000001"
1924        );
1925        assert_eq!(
1926            Decimal256(Uint256::from(1000000000u128)).to_string(),
1927            "0.000000001"
1928        );
1929        assert_eq!(
1930            Decimal256(Uint256::from(10000000000u128)).to_string(),
1931            "0.00000001"
1932        );
1933        assert_eq!(
1934            Decimal256(Uint256::from(100000000000u128)).to_string(),
1935            "0.0000001"
1936        );
1937        assert_eq!(
1938            Decimal256(Uint256::from(10000000000000u128)).to_string(),
1939            "0.00001"
1940        );
1941        assert_eq!(
1942            Decimal256(Uint256::from(100000000000000u128)).to_string(),
1943            "0.0001"
1944        );
1945        assert_eq!(
1946            Decimal256(Uint256::from(1000000000000000u128)).to_string(),
1947            "0.001"
1948        );
1949        assert_eq!(
1950            Decimal256(Uint256::from(10000000000000000u128)).to_string(),
1951            "0.01"
1952        );
1953        assert_eq!(
1954            Decimal256(Uint256::from(100000000000000000u128)).to_string(),
1955            "0.1"
1956        );
1957    }
1958
1959    #[test]
1960    fn decimal256_iter_sum() {
1961        let items = vec![
1962            Decimal256::zero(),
1963            Decimal256::from_str("2").unwrap(),
1964            Decimal256::from_str("2").unwrap(),
1965        ];
1966        assert_eq!(
1967            items.iter().sum::<Decimal256>(),
1968            Decimal256::from_str("4").unwrap()
1969        );
1970        assert_eq!(
1971            items.into_iter().sum::<Decimal256>(),
1972            Decimal256::from_str("4").unwrap()
1973        );
1974
1975        let empty: Vec<Decimal256> = vec![];
1976        assert_eq!(Decimal256::zero(), empty.iter().sum::<Decimal256>());
1977    }
1978
1979    #[test]
1980    fn decimal256_serialize() {
1981        assert_eq!(serde_json::to_vec(&Decimal256::zero()).unwrap(), br#""0""#);
1982        assert_eq!(serde_json::to_vec(&Decimal256::one()).unwrap(), br#""1""#);
1983        assert_eq!(
1984            serde_json::to_vec(&Decimal256::percent(8)).unwrap(),
1985            br#""0.08""#
1986        );
1987        assert_eq!(
1988            serde_json::to_vec(&Decimal256::percent(87)).unwrap(),
1989            br#""0.87""#
1990        );
1991        assert_eq!(
1992            serde_json::to_vec(&Decimal256::percent(876)).unwrap(),
1993            br#""8.76""#
1994        );
1995        assert_eq!(
1996            serde_json::to_vec(&Decimal256::percent(8765)).unwrap(),
1997            br#""87.65""#
1998        );
1999    }
2000
2001    #[test]
2002    fn decimal256_deserialize() {
2003        assert_eq!(
2004            serde_json::from_slice::<Decimal256>(br#""0""#).unwrap(),
2005            Decimal256::zero()
2006        );
2007        assert_eq!(
2008            serde_json::from_slice::<Decimal256>(br#""1""#).unwrap(),
2009            Decimal256::one()
2010        );
2011        assert_eq!(
2012            serde_json::from_slice::<Decimal256>(br#""000""#).unwrap(),
2013            Decimal256::zero()
2014        );
2015        assert_eq!(
2016            serde_json::from_slice::<Decimal256>(br#""001""#).unwrap(),
2017            Decimal256::one()
2018        );
2019
2020        assert_eq!(
2021            serde_json::from_slice::<Decimal256>(br#""0.08""#).unwrap(),
2022            Decimal256::percent(8)
2023        );
2024        assert_eq!(
2025            serde_json::from_slice::<Decimal256>(br#""0.87""#).unwrap(),
2026            Decimal256::percent(87)
2027        );
2028        assert_eq!(
2029            serde_json::from_slice::<Decimal256>(br#""8.76""#).unwrap(),
2030            Decimal256::percent(876)
2031        );
2032        assert_eq!(
2033            serde_json::from_slice::<Decimal256>(br#""87.65""#).unwrap(),
2034            Decimal256::percent(8765)
2035        );
2036    }
2037
2038    #[test]
2039    fn decimal256_abs_diff_works() {
2040        let a = Decimal256::percent(285);
2041        let b = Decimal256::percent(200);
2042        let expected = Decimal256::percent(85);
2043        assert_eq!(a.abs_diff(b), expected);
2044        assert_eq!(b.abs_diff(a), expected);
2045    }
2046
2047    #[test]
2048    #[allow(clippy::op_ref)]
2049    fn decimal256_rem_works() {
2050        // 4.02 % 1.11 = 0.69
2051        assert_eq!(
2052            Decimal256::percent(402) % Decimal256::percent(111),
2053            Decimal256::percent(69)
2054        );
2055
2056        // 15.25 % 4 = 3.25
2057        assert_eq!(
2058            Decimal256::percent(1525) % Decimal256::percent(400),
2059            Decimal256::percent(325)
2060        );
2061
2062        let a = Decimal256::percent(318);
2063        let b = Decimal256::percent(317);
2064        let expected = Decimal256::percent(1);
2065        assert_eq!(a % b, expected);
2066        assert_eq!(a % &b, expected);
2067        assert_eq!(&a % b, expected);
2068        assert_eq!(&a % &b, expected);
2069    }
2070
2071    #[test]
2072    fn decimal_rem_assign_works() {
2073        let mut a = Decimal256::percent(17673);
2074        a %= Decimal256::percent(2362);
2075        assert_eq!(a, Decimal256::percent(1139)); // 176.73 % 23.62 = 11.39
2076
2077        let mut a = Decimal256::percent(4262);
2078        let b = Decimal256::percent(1270);
2079        a %= &b;
2080        assert_eq!(a, Decimal256::percent(452)); // 42.62 % 12.7 = 4.52
2081    }
2082
2083    #[test]
2084    #[should_panic(expected = "divisor of zero")]
2085    fn decimal256_rem_panics_for_zero() {
2086        let _ = Decimal256::percent(777) % Decimal256::zero();
2087    }
2088
2089    #[test]
2090    fn decimal256_checked_methods() {
2091        // checked add
2092        assert_eq!(
2093            Decimal256::percent(402)
2094                .checked_add(Decimal256::percent(111))
2095                .unwrap(),
2096            Decimal256::percent(513)
2097        );
2098        assert!(matches!(
2099            Decimal256::MAX.checked_add(Decimal256::percent(1)),
2100            Err(OverflowError { .. })
2101        ));
2102
2103        // checked sub
2104        assert_eq!(
2105            Decimal256::percent(1111)
2106                .checked_sub(Decimal256::percent(111))
2107                .unwrap(),
2108            Decimal256::percent(1000)
2109        );
2110        assert!(matches!(
2111            Decimal256::zero().checked_sub(Decimal256::percent(1)),
2112            Err(OverflowError { .. })
2113        ));
2114
2115        // checked div
2116        assert_eq!(
2117            Decimal256::percent(30)
2118                .checked_div(Decimal256::percent(200))
2119                .unwrap(),
2120            Decimal256::percent(15)
2121        );
2122        assert_eq!(
2123            Decimal256::percent(88)
2124                .checked_div(Decimal256::percent(20))
2125                .unwrap(),
2126            Decimal256::percent(440)
2127        );
2128        assert!(matches!(
2129            Decimal256::MAX.checked_div(Decimal256::zero()),
2130            Err(CheckedFromRatioError::DivideByZero { .. })
2131        ));
2132        assert!(matches!(
2133            Decimal256::MAX.checked_div(Decimal256::percent(1)),
2134            Err(CheckedFromRatioError::Overflow { .. })
2135        ));
2136
2137        // checked rem
2138        assert_eq!(
2139            Decimal256::percent(402)
2140                .checked_rem(Decimal256::percent(111))
2141                .unwrap(),
2142            Decimal256::percent(69)
2143        );
2144        assert_eq!(
2145            Decimal256::percent(1525)
2146                .checked_rem(Decimal256::percent(400))
2147                .unwrap(),
2148            Decimal256::percent(325)
2149        );
2150        assert!(matches!(
2151            Decimal256::MAX.checked_rem(Decimal256::zero()),
2152            Err(DivideByZeroError { .. })
2153        ));
2154    }
2155
2156    #[test]
2157    fn decimal256_pow_works() {
2158        assert_eq!(Decimal256::percent(200).pow(2), Decimal256::percent(400));
2159        assert_eq!(
2160            Decimal256::percent(200).pow(10),
2161            Decimal256::percent(102400)
2162        );
2163    }
2164
2165    #[test]
2166    #[should_panic]
2167    fn decimal256_pow_overflow_panics() {
2168        _ = Decimal256::MAX.pow(2u32);
2169    }
2170
2171    #[test]
2172    fn decimal256_saturating_works() {
2173        assert_eq!(
2174            Decimal256::percent(200).saturating_add(Decimal256::percent(200)),
2175            Decimal256::percent(400)
2176        );
2177        assert_eq!(
2178            Decimal256::MAX.saturating_add(Decimal256::percent(200)),
2179            Decimal256::MAX
2180        );
2181        assert_eq!(
2182            Decimal256::percent(200).saturating_sub(Decimal256::percent(100)),
2183            Decimal256::percent(100)
2184        );
2185        assert_eq!(
2186            Decimal256::zero().saturating_sub(Decimal256::percent(200)),
2187            Decimal256::zero()
2188        );
2189        assert_eq!(
2190            Decimal256::percent(200).saturating_mul(Decimal256::percent(50)),
2191            Decimal256::percent(100)
2192        );
2193        assert_eq!(
2194            Decimal256::MAX.saturating_mul(Decimal256::percent(200)),
2195            Decimal256::MAX
2196        );
2197        assert_eq!(
2198            Decimal256::percent(400).saturating_pow(2u32),
2199            Decimal256::percent(1600)
2200        );
2201        assert_eq!(Decimal256::MAX.saturating_pow(2u32), Decimal256::MAX);
2202    }
2203
2204    #[test]
2205    fn decimal256_rounding() {
2206        assert_eq!(Decimal256::one().floor(), Decimal256::one());
2207        assert_eq!(Decimal256::percent(150).floor(), Decimal256::one());
2208        assert_eq!(Decimal256::percent(199).floor(), Decimal256::one());
2209        assert_eq!(Decimal256::percent(200).floor(), Decimal256::percent(200));
2210        assert_eq!(Decimal256::percent(99).floor(), Decimal256::zero());
2211
2212        assert_eq!(Decimal256::one().ceil(), Decimal256::one());
2213        assert_eq!(Decimal256::percent(150).ceil(), Decimal256::percent(200));
2214        assert_eq!(Decimal256::percent(199).ceil(), Decimal256::percent(200));
2215        assert_eq!(Decimal256::percent(99).ceil(), Decimal256::one());
2216        assert_eq!(Decimal256(Uint256::from(1u128)).ceil(), Decimal256::one());
2217    }
2218
2219    #[test]
2220    #[should_panic(expected = "attempt to ceil with overflow")]
2221    fn decimal256_ceil_panics() {
2222        let _ = Decimal256::MAX.ceil();
2223    }
2224
2225    #[test]
2226    fn decimal256_checked_ceil() {
2227        assert_eq!(
2228            Decimal256::percent(199).checked_ceil(),
2229            Ok(Decimal256::percent(200))
2230        );
2231        assert_eq!(Decimal256::MAX.checked_ceil(), Err(RoundUpOverflowError));
2232    }
2233
2234    #[test]
2235    fn decimal256_to_uint_floor_works() {
2236        let d = Decimal256::from_str("12.000000000000000001").unwrap();
2237        assert_eq!(d.to_uint_floor(), Uint256::new(12));
2238        let d = Decimal256::from_str("12.345").unwrap();
2239        assert_eq!(d.to_uint_floor(), Uint256::new(12));
2240        let d = Decimal256::from_str("12.999").unwrap();
2241        assert_eq!(d.to_uint_floor(), Uint256::new(12));
2242        let d = Decimal256::from_str("0.98451384").unwrap();
2243        assert_eq!(d.to_uint_floor(), Uint256::new(0));
2244
2245        let d = Decimal256::from_str("75.0").unwrap();
2246        assert_eq!(d.to_uint_floor(), Uint256::new(75));
2247        let d = Decimal256::from_str("0.0").unwrap();
2248        assert_eq!(d.to_uint_floor(), Uint256::new(0));
2249
2250        let d = Decimal256::MAX;
2251        assert_eq!(
2252            d.to_uint_floor(),
2253            Uint256::from_str("115792089237316195423570985008687907853269984665640564039457")
2254                .unwrap()
2255        );
2256
2257        // Does the same as the old workaround `Uint256::one() * my_decimal`.
2258        // This block can be deleted as part of https://github.com/CosmWasm/cosmwasm/issues/1485.
2259        let tests = vec![
2260            (
2261                Decimal256::from_str("12.345").unwrap(),
2262                Uint256::from(12u128),
2263            ),
2264            (
2265                Decimal256::from_str("0.98451384").unwrap(),
2266                Uint256::from(0u128),
2267            ),
2268            (
2269                Decimal256::from_str("178.0").unwrap(),
2270                Uint256::from(178u128),
2271            ),
2272            (Decimal256::MIN, Uint256::from(0u128)),
2273            (
2274                Decimal256::MAX,
2275                Uint256::MAX / Decimal256::DECIMAL_FRACTIONAL,
2276            ),
2277        ];
2278        for (my_decimal, expected) in tests.into_iter() {
2279            assert_eq!(my_decimal.to_uint_floor(), expected);
2280        }
2281    }
2282
2283    #[test]
2284    fn decimal256_to_uint_ceil_works() {
2285        let d = Decimal256::from_str("12.000000000000000001").unwrap();
2286        assert_eq!(d.to_uint_ceil(), Uint256::new(13));
2287        let d = Decimal256::from_str("12.345").unwrap();
2288        assert_eq!(d.to_uint_ceil(), Uint256::new(13));
2289        let d = Decimal256::from_str("12.999").unwrap();
2290        assert_eq!(d.to_uint_ceil(), Uint256::new(13));
2291
2292        let d = Decimal256::from_str("75.0").unwrap();
2293        assert_eq!(d.to_uint_ceil(), Uint256::new(75));
2294        let d = Decimal256::from_str("0.0").unwrap();
2295        assert_eq!(d.to_uint_ceil(), Uint256::new(0));
2296
2297        let d = Decimal256::MAX;
2298        assert_eq!(
2299            d.to_uint_ceil(),
2300            Uint256::from_str("115792089237316195423570985008687907853269984665640564039458")
2301                .unwrap()
2302        );
2303    }
2304
2305    #[test]
2306    fn decimal256_partial_eq() {
2307        let test_cases = [
2308            ("1", "1", true),
2309            ("0.5", "0.5", true),
2310            ("0.5", "0.51", false),
2311            ("0", "0.00000", true),
2312        ]
2313        .into_iter()
2314        .map(|(lhs, rhs, expected)| (dec(lhs), dec(rhs), expected));
2315
2316        #[allow(clippy::op_ref)]
2317        for (lhs, rhs, expected) in test_cases {
2318            assert_eq!(lhs == rhs, expected);
2319            assert_eq!(&lhs == rhs, expected);
2320            assert_eq!(lhs == &rhs, expected);
2321            assert_eq!(&lhs == &rhs, expected);
2322        }
2323    }
2324
2325    #[test]
2326    fn decimal256_implements_debug() {
2327        let decimal = Decimal256::from_str("123.45").unwrap();
2328        assert_eq!(format!("{decimal:?}"), "Decimal256(123.45)");
2329
2330        let test_cases = ["5", "5.01", "42", "0", "2"];
2331        for s in test_cases {
2332            let decimal256 = Decimal256::from_str(s).unwrap();
2333            let expected = format!("Decimal256({s})");
2334            assert_eq!(format!("{decimal256:?}"), expected);
2335        }
2336    }
2337}