kalc_lib/types/
cdecimal.rs

1use super::{Decimal, NewDeciVal, Pow, Prec, Rt, SpecialValuesDeci, WithValDeci};
2use crate::macros::impls::{
3    dec_c_impl, impl_c_ops, impl_cneg, impl_new_val_cdeci, impl_self_c_ops,
4};
5use fastnum::I512;
6#[cfg(feature = "serde")]
7use serde::{Deserialize, Serialize};
8use std::fmt::{Display, Formatter};
9use std::ops::{Div, Mul};
10#[derive(Copy, Clone, PartialEq)]
11#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
12pub struct CDecimal(pub Decimal, pub Decimal);
13
14impl Prec for CDecimal {
15    fn prec(&self) -> u32 {
16        self.0.prec()
17    }
18    fn set_prec(&mut self, new_prec: u32) {
19        self.0.set_prec(new_prec);
20        self.1.set_prec(new_prec);
21    }
22}
23
24impl From<Decimal> for CDecimal {
25    fn from(value: Decimal) -> Self {
26        Self(value, Decimal::new(value.prec()))
27    }
28}
29
30impl From<(Decimal, Decimal)> for CDecimal {
31    fn from((a, b): (Decimal, Decimal)) -> Self {
32        Self(a, b)
33    }
34}
35
36impl Display for CDecimal {
37    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
38        write!(f, "{}+{}i", self.0, self.1)
39    }
40}
41
42impl Mul<I512> for CDecimal {
43    type Output = Self;
44    fn mul(mut self, rhs: I512) -> Self::Output {
45        self.0 = self.0 * rhs;
46        self.1 = self.1 * rhs;
47        self
48    }
49}
50impl Div<I512> for CDecimal {
51    type Output = Self;
52    fn div(mut self, rhs: I512) -> Self::Output {
53        self.0 = self.0 / rhs;
54        self.1 = self.1 / rhs;
55        self
56    }
57}
58
59impl_c_ops!(CDecimal, CDecimal, Decimal, |x| x);
60impl_new_val_cdeci!(CDecimal);
61dec_c_impl!(CDecimal, Decimal, Decimal::with_val);
62impl_cneg!(CDecimal, CDecimal);
63impl_self_c_ops!(CDecimal);