Decimal

Struct Decimal 

Source
pub struct Decimal {
    sign: Sign,
    integer: Vec<u64>,
    decimal: Vec<u64>,
}

Fields§

§sign: Sign§integer: Vec<u64>§decimal: Vec<u64>

Implementations§

Source§

impl Decimal

Source

fn cmp_absolute_parts(&self, other: &Self) -> Ordering

Examples found in repository?
crates/competitive/src/num/decimal.rs (line 99)
97    fn cmp(&self, other: &Self) -> Ordering {
98        self.sign.cmp(&other.sign).then_with(|| match self.sign {
99            Sign::Minus => other.cmp_absolute_parts(self),
100            Sign::Zero => Ordering::Equal,
101            Sign::Plus => self.cmp_absolute_parts(other),
102        })
103    }
More examples
Hide additional examples
crates/competitive/src/num/decimal/addsub.rs (line 93)
92fn sub_absolute_parts_gte(lhs: &Decimal, rhs: &mut Decimal) {
93    debug_assert!(matches!(lhs.cmp_absolute_parts(rhs), Ordering::Greater));
94
95    let mut borrow = false;
96
97    // decimal part
98    let rhs_decimal_len = rhs.decimal.len();
99    if lhs.decimal.len() > rhs_decimal_len {
100        for (l, r) in lhs.decimal[..rhs_decimal_len]
101            .iter()
102            .rev()
103            .zip(rhs.decimal.iter_mut().rev())
104        {
105            borrow = sub_borrow(borrow, *l, *r, r);
106        }
107        rhs.decimal
108            .extend_from_slice(&lhs.decimal[rhs_decimal_len..]);
109    } else {
110        for r in rhs.decimal[lhs.decimal.len()..].iter_mut().rev() {
111            borrow = sub_borrow(borrow, 0, *r, r);
112        }
113        for (l, r) in lhs
114            .decimal
115            .iter()
116            .rev()
117            .zip(rhs.decimal[..lhs.decimal.len()].iter_mut().rev())
118        {
119            borrow = sub_borrow(borrow, *l, *r, r);
120        }
121    }
122
123    // integer part
124    let rhs_integer_len = rhs.integer.len();
125    if lhs.integer.len() > rhs_integer_len {
126        for (l, r) in lhs.integer[..rhs_integer_len]
127            .iter()
128            .zip(rhs.integer.iter_mut())
129        {
130            borrow = sub_borrow(borrow, *l, *r, r);
131        }
132        rhs.integer
133            .extend_from_slice(&lhs.integer[rhs_integer_len..]);
134        if borrow {
135            for r in rhs.integer[rhs_integer_len..].iter_mut() {
136                borrow = sub_borrow(borrow, *r, 0, r);
137                if !borrow {
138                    break;
139                }
140            }
141        }
142    } else {
143        debug_assert_eq!(lhs.integer.len(), rhs_integer_len);
144        for (l, r) in lhs.integer.iter().zip(&mut rhs.integer) {
145            borrow = sub_borrow(borrow, *l, *r, r);
146        }
147    }
148
149    assert!(
150        !borrow,
151        "Cannot subtract lhs from rhs because lhs is smaller than rhs"
152    );
153
154    rhs.normalize();
155}
Source

fn normalize(&mut self)

Examples found in repository?
crates/competitive/src/num/decimal/addsub.rs (line 77)
17fn add_absolute_parts(lhs: &mut Decimal, rhs: &Decimal) {
18    let mut carry = false;
19
20    // decimal part
21    let lhs_decimal_len = lhs.decimal.len();
22    if lhs_decimal_len < rhs.decimal.len() {
23        for (l, r) in lhs
24            .decimal
25            .iter_mut()
26            .rev()
27            .zip(rhs.decimal[..lhs_decimal_len].iter().rev())
28        {
29            carry = add_carry(carry, *l, *r, l);
30        }
31        lhs.decimal
32            .extend_from_slice(&rhs.decimal[lhs_decimal_len..]);
33    } else {
34        for (l, r) in lhs.decimal[..rhs.decimal.len()]
35            .iter_mut()
36            .rev()
37            .zip(rhs.decimal.iter().rev())
38        {
39            carry = add_carry(carry, *l, *r, l);
40        }
41    }
42
43    // integer part
44    let lhs_integer_len = lhs.integer.len();
45    if lhs_integer_len < rhs.integer.len() {
46        for (l, r) in lhs.integer.iter_mut().zip(&rhs.integer[..lhs_integer_len]) {
47            carry = add_carry(carry, *l, *r, l);
48        }
49        lhs.integer
50            .extend_from_slice(&rhs.integer[lhs_integer_len..]);
51        if carry {
52            for l in lhs.integer[lhs_integer_len..].iter_mut() {
53                carry = add_carry(carry, *l, 0, l);
54                if !carry {
55                    break;
56                }
57            }
58        }
59    } else {
60        for (l, r) in lhs.integer.iter_mut().zip(&rhs.integer) {
61            carry = add_carry(carry, *l, *r, l);
62        }
63        if carry {
64            for l in lhs.integer[rhs.integer.len()..].iter_mut() {
65                carry = add_carry(carry, *l, 0, l);
66                if !carry {
67                    break;
68                }
69            }
70        }
71    }
72
73    if carry {
74        lhs.integer.push(carry as u64);
75    }
76
77    lhs.normalize();
78}
79
80fn sub_borrow(borrow: bool, lhs: u64, rhs: u64, out: &mut u64) -> bool {
81    let (sum, borrow1) = lhs.overflowing_sub(rhs);
82    let (mut sum, borrow2) = sum.overflowing_sub(borrow as u64);
83    let borrow = borrow1 || borrow2;
84    if borrow {
85        sum = sum.wrapping_add(RADIX);
86    }
87    *out = sum;
88    borrow
89}
90
91// assume |lhs| >= |rhs|
92fn sub_absolute_parts_gte(lhs: &Decimal, rhs: &mut Decimal) {
93    debug_assert!(matches!(lhs.cmp_absolute_parts(rhs), Ordering::Greater));
94
95    let mut borrow = false;
96
97    // decimal part
98    let rhs_decimal_len = rhs.decimal.len();
99    if lhs.decimal.len() > rhs_decimal_len {
100        for (l, r) in lhs.decimal[..rhs_decimal_len]
101            .iter()
102            .rev()
103            .zip(rhs.decimal.iter_mut().rev())
104        {
105            borrow = sub_borrow(borrow, *l, *r, r);
106        }
107        rhs.decimal
108            .extend_from_slice(&lhs.decimal[rhs_decimal_len..]);
109    } else {
110        for r in rhs.decimal[lhs.decimal.len()..].iter_mut().rev() {
111            borrow = sub_borrow(borrow, 0, *r, r);
112        }
113        for (l, r) in lhs
114            .decimal
115            .iter()
116            .rev()
117            .zip(rhs.decimal[..lhs.decimal.len()].iter_mut().rev())
118        {
119            borrow = sub_borrow(borrow, *l, *r, r);
120        }
121    }
122
123    // integer part
124    let rhs_integer_len = rhs.integer.len();
125    if lhs.integer.len() > rhs_integer_len {
126        for (l, r) in lhs.integer[..rhs_integer_len]
127            .iter()
128            .zip(rhs.integer.iter_mut())
129        {
130            borrow = sub_borrow(borrow, *l, *r, r);
131        }
132        rhs.integer
133            .extend_from_slice(&lhs.integer[rhs_integer_len..]);
134        if borrow {
135            for r in rhs.integer[rhs_integer_len..].iter_mut() {
136                borrow = sub_borrow(borrow, *r, 0, r);
137                if !borrow {
138                    break;
139                }
140            }
141        }
142    } else {
143        debug_assert_eq!(lhs.integer.len(), rhs_integer_len);
144        for (l, r) in lhs.integer.iter().zip(&mut rhs.integer) {
145            borrow = sub_borrow(borrow, *l, *r, r);
146        }
147    }
148
149    assert!(
150        !borrow,
151        "Cannot subtract lhs from rhs because lhs is smaller than rhs"
152    );
153
154    rhs.normalize();
155}

Trait Implementations§

Source§

impl Add<&Decimal> for &Decimal

Source§

type Output = Decimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Decimal) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<&Decimal> for Decimal

Source§

type Output = Decimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Decimal) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<Decimal> for &Decimal

Source§

type Output = Decimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Decimal) -> Self::Output

Performs the + operation. Read more
Source§

impl Add for Decimal

Source§

type Output = Decimal

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Decimal) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign<&Decimal> for Decimal

Source§

fn add_assign(&mut self, rhs: &Decimal)

Performs the += operation. Read more
Source§

impl AddAssign for Decimal

Source§

fn add_assign(&mut self, rhs: Decimal)

Performs the += operation. Read more
Source§

impl Clone for Decimal

Source§

fn clone(&self) -> Decimal

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Decimal

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Decimal

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Decimal

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<f32> for Decimal

Source§

fn from(val: f32) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Decimal

Source§

fn from(val: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i128> for Decimal

Source§

fn from(val: i128) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Decimal

Source§

fn from(val: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Decimal

Source§

fn from(val: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Decimal

Source§

fn from(val: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Decimal

Source§

fn from(val: i8) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for Decimal

Source§

fn from(val: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for Decimal

Source§

fn from(val: u128) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Decimal

Source§

fn from(val: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Decimal

Source§

fn from(val: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Decimal

Source§

fn from(val: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Decimal

Source§

fn from(val: u8) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for Decimal

Source§

fn from(val: usize) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Decimal

Source§

type Err = ParseDecimalError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Decimal

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl IterScan for Decimal

Source§

type Output = Decimal

Source§

fn scan<'a, I: Iterator<Item = &'a str>>(iter: &mut I) -> Option<Self::Output>

Source§

impl Neg for Decimal

Source§

type Output = Decimal

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl One for Decimal

Source§

fn one() -> Self

Source§

fn is_one(&self) -> bool
where Self: PartialEq,

Source§

fn set_one(&mut self)

Source§

impl Ord for Decimal

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Decimal

Source§

fn eq(&self, other: &Decimal) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Decimal

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub<&Decimal> for &Decimal

Source§

type Output = Decimal

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &Decimal) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<&Decimal> for Decimal

Source§

type Output = Decimal

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &Decimal) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<Decimal> for &Decimal

Source§

type Output = Decimal

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Decimal) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for Decimal

Source§

type Output = Decimal

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Decimal) -> Self::Output

Performs the - operation. Read more
Source§

impl SubAssign<&Decimal> for Decimal

Source§

fn sub_assign(&mut self, rhs: &Decimal)

Performs the -= operation. Read more
Source§

impl SubAssign for Decimal

Source§

fn sub_assign(&mut self, rhs: Decimal)

Performs the -= operation. Read more
Source§

impl Zero for Decimal

Source§

fn zero() -> Self

Source§

fn is_zero(&self) -> bool

Source§

fn set_zero(&mut self)

Source§

impl Eq for Decimal

Source§

impl StructuralPartialEq for Decimal

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AsTotalOrd for T
where T: PartialOrd,

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PartialOrdExt for T
where T: PartialOrd,

Source§

fn chmin(&mut self, other: T)

Source§

fn chmax(&mut self, other: T)

Source§

fn minmax(self, other: T) -> (T, T)

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.