1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
use decimal::d128;
use std::str::FromStr;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::default::Default;
use std::ops::{Add, Div, Mul, Neg, Rem, Sub, AddAssign, SubAssign, MulAssign};
use std::borrow::Borrow;
use std::iter::Sum;

use super::error::Error;


///
/// # Examples:
/// ```
/// # #[macro_use]
/// # extern crate pure_decimal;
///
/// # use std::collections::BTreeMap;
///
/// # fn main() {
///
/// // Use as keys in BTree
/// let mut map = BTreeMap::new();
/// map.insert(dec!(1.0), dec!(1.0));
/// map.insert(dec!(1), dec!(2.0));
///
/// assert!(map.len() == 1);
/// assert!(map.contains_key(&dec!(1.00)));
/// assert!(map.get(&dec!(1.00)) == Some(&dec!(2.0)));
///
/// # }
/// ```
#[derive(Clone, Copy)]
pub struct Decimal(pub(crate) d128);

impl Decimal {
    /// Creates a Decimal with `0` as value
    pub fn zero() -> Self {
        Decimal(d128::zero())
    }

    /// returns the larger of `self` and `other`
    pub fn max<O: AsRef<Decimal>>(self, other: O) -> Decimal {
        Decimal(self.0.max(other.as_ref().0))
    }

    /// returns the smaller of `self` and `other`
    pub fn min<O: AsRef<Decimal>>(self, other: O) -> Decimal {
        Decimal(self.0.min(other.as_ref().0))
    }

    /// returns absolute value of `self`
    pub fn abs(&self) -> Decimal {
        Decimal(self.0.abs())
    }

    /// Calculates the fused multiply-add `self` × `a` + `b` and returns the result. The multiply
    /// is carried out first and is exact, so this operation has only the one, final, rounding.
    pub fn mul_add<O: AsRef<Decimal>>(self, a: O, b: O) -> Decimal {
        Decimal(self.0.mul_add(a.as_ref().0, b.as_ref().0))
    }

    /// returns true if `self` is less than zero
    pub fn is_negative(&self) -> bool {
        self.0.is_negative()
    }

    pub fn is_positive(&self) -> bool {
        self.0.is_positive()
    }

    /// returns true if `self` is zero
    pub fn is_zero(&self) -> bool {
        self.0.is_zero()
    }

    /// see ```[decimal::d128::pow]```
    pub fn pow<O: AsRef<Decimal>>(self, exp: O) -> Decimal {
        Decimal(self.as_ref().0.pow(exp.as_ref().0))
    }
}

impl Default for Decimal {
    fn default() -> Self {
        Self::zero()
    }
}

impl Hash for Decimal {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.0.hash(state);
    }
}

impl FromStr for Decimal {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Error> {
        let val = d128::from_str(s);
        if val.is_ok() {
            let dec = val.unwrap();
            if dec.is_nan() {
                Err(Error::new("NaN is not supported"))
            } else if dec.is_infinite() {
                Err(Error::new("Infinity is not supported"))
            } else {
                Ok(Decimal(dec))
            }
        } else {
            Err(Error::new("Failed to parse"))
        }
    }
}

/// Delegates to d128.
impl fmt::Display for Decimal {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        self.0.fmt(fmt)
    }
}

/// Delegates to d128.
impl fmt::Debug for Decimal {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        self.0.fmt(fmt)
    }
}

/// Delegates to d128.
impl fmt::LowerExp for Decimal {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        self.0.fmt(fmt)
    }
}

/// Delegates to d128.
impl fmt::LowerHex for Decimal {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        self.0.fmt(fmt)
    }
}


impl PartialEq<Decimal> for Decimal {
    fn eq(&self, other: &Decimal) -> bool {
        self.0.eq(&other.0)
    }
}

impl Eq for Decimal {}

impl PartialOrd<Decimal> for Decimal {
    fn partial_cmp(&self, other: &Decimal) -> Option<::std::cmp::Ordering> {
        self.0.partial_cmp(&other.0)
    }
}

impl Ord for Decimal {
    fn cmp(&self, other: &Decimal) -> ::std::cmp::Ordering {
        let res = self.0.partial_cmp(&other.0);
        match res {
            None => panic!("Ordering not possible. Possible bug"),
            Some(ord) => ord,
        }
    }
}

impl From<i32> for Decimal {
    fn from(val: i32) -> Decimal {
        Decimal(d128::from(val))
    }
}

/// Converts an u32 to d128. The result is exact and no error is possible.
impl From<u32> for Decimal {
    fn from(val: u32) -> Decimal {
        Decimal(d128::from(val))
    }
}

/// Converts an u64 to d128. The result is exact and no error is possible.
impl From<u64> for Decimal {
    fn from(val: u64) -> Decimal {
        Decimal(d128::from(val))
    }
}

/// Converts an i64 to d128. The result is exact and no error is possible.
impl From<i64> for Decimal {
    fn from(val: i64) -> Decimal {
        Decimal(d128::from(val))
    }
}

impl AsRef<Decimal> for Decimal {
    fn as_ref(&self) -> &Decimal {
        self
    }
}

macro_rules! unary_op {
    ($(#[$attr:meta])* impl $op:ident, $method:ident) => {
        $(#[$attr])*
        impl $op for Decimal {
            type Output = Decimal;

            fn $method(self) -> Decimal {
                Decimal((self.0).$method())
            }
        }

        impl<'a> $op for &'a Decimal {
            type Output = Decimal;

            fn $method(self) -> Decimal {
                Decimal((self.0).$method())
            }
        }
    }
}

unary_op!(impl Neg, neg);

macro_rules! binary_op {
    ($(#[$attr:meta])* impl $op:ident, $method:ident, $t:ident) => {
        $(#[$attr])*
        impl $op<$t> for $t {
            type Output = $t;

            fn $method(self, other: $t) -> $t {
                Decimal((self.0).$method(other.0))
            }
        }

        impl<'a> $op<$t> for &'a $t {
            type Output = $t;

            fn $method(self, other: $t) -> $t {
                Decimal((self.0).$method(other.0))
            }
        }

        impl<'a> $op<&'a$t> for $t {
            type Output = $t;

            fn $method(self, other: &'a $t) -> $t {
                Decimal((self.0).$method(other.0))
            }
        }

        impl<'a, 'b> $op<&'a $t> for &'b $t {
            type Output = $t;

            fn $method(self, other: &'a $t) -> $t {
                Decimal((self.0).$method(other.0))
            }
        }
    }
}

binary_op!(impl Add, add, Decimal);
binary_op!(impl Sub, sub, Decimal);
binary_op!(impl Mul, mul, Decimal);

macro_rules! guarded_binary_op {
    ($(#[$attr:meta])* impl $op:ident, $method:ident, $t:ident) => {


        $(#[$attr])*
        impl $op<$t> for $t {
            type Output = ::std::result::Result<$t, Error>;

            /// Returns `Ok(Decimal)` if result is finite `Err` otherwise
            fn $method(self, other: $t) -> ::std::result::Result<$t, Error> {
                let val = (self.0).$method(other.0);
                if val.is_finite() {
                    Ok(Decimal(val))
                }else{
                 Err(Error::new("Only finite value are supported"))
                }
            }
        }

        impl<'a> $op<$t> for &'a $t {
            type Output = ::std::result::Result<$t, Error>;

            /// Returns `Ok(Decimal)` if result is finite `Err` otherwise
            fn $method(self, other: $t) -> ::std::result::Result<$t, Error> {
                let val = (self.0).$method(other.0);
                if val.is_finite() {
                    Ok(Decimal(val))
                }else{
                 Err(Error::new("Only finite value are supported"))
                }
            }
        }

        impl<'a> $op<&'a$t> for $t {
            type Output = ::std::result::Result<$t, Error>;

            /// Returns `Ok(Decimal)` if result is finite `Err` otherwise
            fn $method(self, other: &'a $t) -> ::std::result::Result<$t, Error> {
                let val = (self.0).$method(other.0);
                if val.is_finite() {
                    Ok(Decimal(val))
                }else{
                 Err(Error::new("Only finite value are supported"))
                }
            }
        }

        impl<'a, 'b> $op<&'a $t> for &'b $t {
            type Output = ::std::result::Result<$t, Error>;

            /// Returns `Ok(Decimal)` if result is finite `Err` otherwise
            fn $method(self, other: &'a $t) -> ::std::result::Result<$t, Error> {
                let val = (self.0).$method(other.0);
                if val.is_finite() {
                    Ok(Decimal(val))
                }else{
                 Err(Error::new("Only finite value are supported"))
                }
            }
        }
    }
}

guarded_binary_op!(impl Div, div, Decimal);
guarded_binary_op!(impl Rem, rem, Decimal);


macro_rules! unary_assign_op {
    ($(#[$attr:meta])* impl $op:ident, $method:ident, $t:ident) => {
        $(#[$attr])*
        impl $op<$t> for $t {
            fn $method(&mut self, other: $t) {
                (self.0).$method(other.0);
            }
        }
    }
}

unary_assign_op!(impl AddAssign, add_assign, Decimal);
unary_assign_op!(impl SubAssign, sub_assign, Decimal);
unary_assign_op!(impl MulAssign, mul_assign, Decimal);


impl<T> Sum<T> for Decimal where T: Borrow<Decimal> {
    fn sum<I: IntoIterator<Item=T>>(iter: I) -> Decimal {
        iter.into_iter()
            .fold(Decimal::zero(), |acc, val| acc + val.borrow())
    }
}



#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn default() {
        use std::str::FromStr;

        assert_eq!(Decimal::zero(), Decimal::default());
        assert_eq!(Decimal::zero(), Default::default());
        assert_eq!(Decimal::zero(), Decimal::from_str("0").unwrap());
        assert_eq!(Decimal::zero(), Decimal::from_str("0.0").unwrap());
    }

    #[test]
    fn unary_op() {
        assert_eq!(dec!(-1.1), -dec!(1.1));
        assert_eq!(dec!(-1.1), -&dec!(1.1));
    }

    #[test]
    fn utility_functions() {
        assert!(dec!(0.0).is_zero());
        assert_ne!(true,dec!(0.0).is_negative());
        assert_ne!(true,dec!(0.0).is_positive());

        assert_ne!(true,dec!(0.1).is_zero());
        assert_ne!(true,dec!(0.1).is_negative());
        assert!(dec!(0.1).is_positive());

        assert_ne!(true,dec!(-0.1).is_zero());
        assert!(dec!(-0.1).is_negative());
        assert_ne!(true,dec!(-0.1).is_positive());

    }

    #[test]
    fn binary_op() {
        assert_eq!(dec!(3.33), dec!(1.11) + dec!(2.22));
        assert_eq!(dec!(3.33), &dec!(1.11) + dec!(2.22));
        assert_eq!(dec!(3.33), dec!(1.11) + &dec!(2.22));
        assert_eq!(dec!(3.33), &dec!(1.11) + &dec!(2.22));
        //assert_eq!(dec!(5) << 2, dec!(500));
        //assert_eq!(dec!(500) >> 1, dec!(50));
    }

    #[test]
    fn as_ref_operand() {
        assert_eq!(dec!(1.1), dec!(1.1).min(dec!(2.2)));
        assert_eq!(dec!(1.1), dec!(1.1).min(&dec!(2.2)));
    }


    #[test]
    fn assign_op() {
        let mut x = dec!(1);
        x += dec!(2);
        assert_eq!(x, dec!(3));
        x *= dec!(3);
        assert_eq!(x, dec!(9));
        x -= dec!(1);
        assert_eq!(x, dec!(8));
    }

    #[test]
    fn test_sum() {
        let decimals = vec![dec!(1), dec!(2), dec!(3), dec!(4)];
        assert_eq!(dec!(10), decimals.iter().sum());
        assert_eq!(dec!(10), decimals.into_iter().sum());
    }
}