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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//! Contains traits for checked and rounding operations.

use crate::ArithmeticError;

pub(crate) mod sqrt;

/// Represents `0`.
pub trait Zero {
    /// Represents `0`.
    const ZERO: Self;
}

/// Represents `1`.
pub trait One {
    /// Represents `1`.
    const ONE: Self;
}

/// Represents `MIN` and `MAX` values.
pub trait Bounded {
    /// Represents `MIN`.
    const MIN: Self;
    /// Represents `MAX`.
    const MAX: Self;
}

/// Checked addition.
pub trait CheckedAdd<Rhs = Self> {
    /// Result of addition.
    type Output;
    /// Usually [`ArithmeticError`].
    type Error;

    /// Checked addition. Returns `Err` on overflow.
    ///
    /// ```
    /// # #[cfg(feature = "i64")]
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use fixnum::{FixedPoint, typenum::U9, ops::CheckedAdd};
    ///
    /// type Amount = FixedPoint<i64, U9>;
    ///
    /// let a: Amount = "0.1".parse()?;
    /// let b: Amount = "0.2".parse()?;
    /// let c: Amount = "0.3".parse()?;
    /// assert_eq!(a.cadd(b)?, c);
    /// # Ok(()) }
    /// # #[cfg(not(feature = "i64"))]
    /// # fn main() {}
    /// ```
    fn cadd(self, rhs: Rhs) -> Result<Self::Output, Self::Error>;

    /// Saturating addition. Computes `self + rhs`, saturating at the numeric bounds
    /// ([`MIN`][MIN], [`MAX`][MAX]) instead of overflowing.
    ///
    /// ```
    /// # #[cfg(feature = "i64")]
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use fixnum::{FixedPoint, typenum::U9, ops::{Bounded, RoundMode::*, CheckedAdd}};
    ///
    /// type Amount = FixedPoint<i64, U9>;
    ///
    /// let a: Amount = "1000.00002".parse()?;
    /// let b: Amount = "9222000000".parse()?;
    /// let c: Amount = "9222001000.00002".parse()?;
    /// // 1000.00002 + 9222000000 = 9222001000.00002
    /// assert_eq!(a.saturating_add(b), c);
    ///
    /// // 9222000000 + 9222000000 = MAX
    /// assert_eq!(c.saturating_add(c), Amount::MAX);
    ///
    /// let d: Amount = "-9222000000".parse()?;
    /// // -9222000000 + (-9222000000) = MIN
    /// assert_eq!(d.saturating_add(d), Amount::MIN);
    /// # Ok(()) }
    /// # #[cfg(not(feature = "i64"))]
    /// # fn main() {}
    /// ```
    ///
    /// [MAX]: ./trait.Bounded.html#associatedconstant.MAX
    /// [MIN]: ./trait.Bounded.html#associatedconstant.MIN
    fn saturating_add(self, rhs: Rhs) -> Self::Output
    where
        Self: Sized,
        Rhs: PartialOrd + Zero,
        Self::Output: Bounded,
    {
        let is_rhs_negative = rhs < Rhs::ZERO;
        self.cadd(rhs).unwrap_or({
            if is_rhs_negative {
                Self::Output::MIN
            } else {
                Self::Output::MAX
            }
        })
    }
}

/// Checked subtraction.
pub trait CheckedSub<Rhs = Self> {
    /// Result of subtraction.
    type Output;
    /// Usually [`ArithmeticError`].
    type Error;

    /// Checked subtraction. Returns `Err` on overflow.
    ///
    /// ```
    /// # #[cfg(feature = "i64")]
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use fixnum::{FixedPoint, typenum::U9, ops::CheckedSub};
    ///
    /// type Amount = FixedPoint<i64, U9>;
    ///
    /// let a: Amount = "0.3".parse()?;
    /// let b: Amount = "0.1".parse()?;
    /// let c: Amount = "0.2".parse()?;
    /// assert_eq!(a.csub(b)?, c);
    /// # Ok(()) }
    /// # #[cfg(not(feature = "i64"))]
    /// # fn main() {}
    /// ```
    fn csub(self, rhs: Rhs) -> Result<Self::Output, Self::Error>;

    /// Saturating subtraction. Computes `self - rhs`, saturating at the numeric bounds
    /// ([`MIN`][MIN], [`MAX`][MAX]) instead of overflowing.
    ///
    /// ```
    /// # #[cfg(feature = "i64")]
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use fixnum::{FixedPoint, typenum::U9, ops::{Bounded, RoundMode::*, CheckedSub}};
    ///
    /// type Amount = FixedPoint<i64, U9>;
    ///
    /// let a: Amount = "9222001000.00002".parse()?;
    /// let b: Amount = "9222000000".parse()?;
    /// let c: Amount = "1000.00002".parse()?;
    /// // 9222001000.00002 - 9222000000 = 1000.00002
    /// assert_eq!(a.saturating_sub(b), c);
    ///
    /// let d: Amount = "-9222000000".parse()?;
    /// // 9222000000 - (-9222000000) = MAX
    /// assert_eq!(b.saturating_sub(d), Amount::MAX);
    ///
    /// // -9222000000 - 9222000000 = MIN
    /// assert_eq!(d.saturating_sub(b), Amount::MIN);
    /// # Ok(()) }
    /// # #[cfg(not(feature = "i64"))]
    /// # fn main() {}
    /// ```
    ///
    /// [MAX]: ./trait.Bounded.html#associatedconstant.MAX
    /// [MIN]: ./trait.Bounded.html#associatedconstant.MIN
    fn saturating_sub(self, rhs: Rhs) -> Self::Output
    where
        Self: Sized,
        Rhs: PartialOrd + Zero,
        Self::Output: Bounded,
    {
        let is_rhs_negative = rhs < Rhs::ZERO;
        self.csub(rhs).unwrap_or({
            if is_rhs_negative {
                Self::Output::MAX
            } else {
                Self::Output::MIN
            }
        })
    }
}

/// Checked multiplication.
pub trait CheckedMul<Rhs = Self> {
    /// Result of multiplication.
    type Output;
    /// Usually [`ArithmeticError`].
    type Error;

    /// Checked multiplication. Returns `Err` on overflow.
    /// This is multiplication without rounding, hence it's available only when at least one operand is integer.
    ///
    /// ```
    /// # #[cfg(feature = "i64")]
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use fixnum::{FixedPoint, typenum::U9, ops::CheckedMul};
    ///
    /// type Amount = FixedPoint<i64, U9>;
    ///
    /// let a: Amount = "0.000000001".parse()?;
    /// let b: Amount = "0.000000012".parse()?;
    /// assert_eq!(a.cmul(12)?, b);
    /// assert_eq!(12.cmul(a)?, b);
    /// # Ok(()) }
    /// # #[cfg(not(feature = "i64"))]
    /// # fn main() {}
    /// ```
    fn cmul(self, rhs: Rhs) -> Result<Self::Output, Self::Error>;

    /// Saturating multiplication. Computes `self * rhs`, saturating at the numeric bounds
    /// ([`MIN`][MIN], [`MAX`][MAX]) instead of overflowing.
    /// This is multiplication without rounding, hence it's available only when at least one operand is integer.
    ///
    /// ```
    /// # #[cfg(feature = "i64")]
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use fixnum::{FixedPoint, typenum::U9, ops::{Zero, Bounded, RoundMode::*, CheckedMul}};
    ///
    /// type Amount = FixedPoint<i64, U9>;
    ///
    /// let a: Amount = "0.000000001".parse()?;
    /// let b: Amount = "0.000000012".parse()?;
    /// assert_eq!(a.saturating_mul(12), b);
    /// assert_eq!(12.saturating_mul(a), b);
    ///
    /// // i64::MAX * 1e-9 = MAX
    /// assert_eq!(a.saturating_mul(i64::MAX), Amount::MAX);
    ///
    /// let c: Amount = "-1.000000001".parse()?;
    /// // -1.000000001 * (SaturatingCeil) MAX = MIN
    /// assert_eq!(c.saturating_mul(i64::MAX), Amount::MIN);
    /// # Ok(()) }
    /// # #[cfg(not(feature = "i64"))]
    /// # fn main() {}
    /// ```
    ///
    /// [FixedPoint]: ../struct.FixedPoint.html
    /// [MAX]: ./trait.Bounded.html#associatedconstant.MAX
    /// [MIN]: ./trait.Bounded.html#associatedconstant.MIN
    /// [RoundMode]: ./enum.RoundMode.html
    fn saturating_mul(self, rhs: Rhs) -> Self::Output
    where
        Self: PartialOrd + Zero + Sized,
        Rhs: PartialOrd + Zero,
        Self::Output: Bounded,
    {
        let is_lhs_negative = self < Self::ZERO;
        let is_rhs_negative = rhs < Rhs::ZERO;
        self.cmul(rhs).unwrap_or({
            if is_lhs_negative == is_rhs_negative {
                Self::Output::MAX
            } else {
                Self::Output::MIN
            }
        })
    }
}

/// Mode of rounding.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum RoundMode {
    /// Rounds up. The result is equal or greater than a mathematical result.
    Ceil = 1,
    /// Rounds to nearest representable value.
    /// The result is nearest to a mathematical result.
    Nearest = 0,
    /// Rounds down. The result is equal or less than a mathematical result.
    Floor = -1,
}

/// Rounding multiplication.
pub trait RoundingMul<Rhs = Self> {
    /// Result of multiplication.
    type Output;
    /// Usually [`ArithmeticError`].
    type Error;

    /// Checked rounded multiplication. Returns `Err` on overflow.
    /// Because of provided [`RoundMode`][RoundMode] it's possible to perform across the [`FixedPoint`][FixedPoint]
    /// values.
    ///
    /// ```
    /// # #[cfg(feature = "i64")]
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use fixnum::{FixedPoint, typenum::U9, ops::{Zero, RoundingMul, RoundMode::*}};
    ///
    /// type Amount = FixedPoint<i64, U9>;
    ///
    /// let a: Amount = "0.000000001".parse()?;
    /// let b: Amount = "0.000000002".parse()?;
    /// // 1e-9 * (Ceil) 2e-9 = 1e-9
    /// assert_eq!(a.rmul(b, Ceil)?, a);
    /// assert_eq!(b.rmul(a, Ceil)?, a);
    /// // 1e-9 * (Floor) 2e-9 = 0
    /// assert_eq!(a.rmul(b, Floor)?, Amount::ZERO);
    /// assert_eq!(b.rmul(a, Floor)?, Amount::ZERO);
    /// # Ok(()) }
    /// # #[cfg(not(feature = "i64"))]
    /// # fn main() {}
    /// ```
    ///
    /// [FixedPoint]: ../struct.FixedPoint.html
    /// [RoundMode]: ./enum.RoundMode.html
    fn rmul(self, rhs: Rhs, mode: RoundMode) -> Result<Self::Output, Self::Error>;

    /// Saturating rounding multiplication. Computes `self * rhs`, saturating at the numeric bounds
    /// ([`MIN`][MIN], [`MAX`][MAX]) instead of overflowing.
    /// Because of provided [`RoundMode`][RoundMode] it's possible to perform across the [`FixedPoint`][FixedPoint]
    /// values.
    ///
    /// ```
    /// # #[cfg(feature = "i64")]
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use fixnum::{FixedPoint, typenum::U9, ops::{Zero, Bounded, RoundMode::*, RoundingMul}};
    ///
    /// type Amount = FixedPoint<i64, U9>;
    ///
    /// let a: Amount = "0.000000001".parse()?;
    /// let b: Amount = "0.000000002".parse()?;
    /// // 1e-9 * (SaturatingCeil) 2e9 = 1e-9
    /// assert_eq!(a.saturating_rmul(b, Ceil), a);
    /// // 1e-9 * (SaturatingFloor) 2e9 = 0
    /// assert_eq!(a.saturating_rmul(b, Floor), Amount::ZERO);
    ///
    /// // MIN * (SaturatingFloor) MIN = MAX
    /// assert_eq!(Amount::MIN.saturating_rmul(Amount::MIN, Floor), Amount::MAX);
    ///
    /// let c: Amount = "-1.000000001".parse()?;
    /// // -1.000000001 * (SaturatingCeil) MAX = MIN
    /// assert_eq!(c.saturating_rmul(Amount::MAX, Ceil), Amount::MIN);
    /// # Ok(()) }
    /// # #[cfg(not(feature = "i64"))]
    /// # fn main() {}
    /// ```
    ///
    /// [FixedPoint]: ../struct.FixedPoint.html
    /// [MAX]: ./trait.Bounded.html#associatedconstant.MAX
    /// [MIN]: ./trait.Bounded.html#associatedconstant.MIN
    /// [RoundMode]: ./enum.RoundMode.html
    fn saturating_rmul(self, rhs: Rhs, round_mode: RoundMode) -> Self::Output
    where
        Self: PartialOrd + Zero + Sized,
        Rhs: PartialOrd + Zero,
        Self::Output: Bounded,
    {
        let is_lhs_negative = self < Self::ZERO;
        let is_rhs_negative = rhs < Rhs::ZERO;
        self.rmul(rhs, round_mode).unwrap_or({
            if is_lhs_negative == is_rhs_negative {
                Self::Output::MAX
            } else {
                Self::Output::MIN
            }
        })
    }
}

/// Rounding division.
pub trait RoundingDiv<Rhs = Self> {
    /// Result of division.
    type Output;
    /// Usually [`ArithmeticError`].
    type Error;

    /// Checked rounded division. Returns `Err` on overflow or attempt to divide by zero.
    /// Because of provided [`RoundMode`][RoundMode] it's possible to perform across the [`FixedPoint`][FixedPoint]
    /// values.
    ///
    /// ```
    /// # #[cfg(feature = "i64")]
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// use fixnum::{FixedPoint, typenum::U9, ops::{Zero, RoundingDiv, RoundMode::*}};
    ///
    /// type Amount = FixedPoint<i64, U9>;
    ///
    /// let a: Amount = "0.000000001".parse()?;
    /// let b: Amount = "1000000000".parse()?;
    /// // 1e-9 / (Ceil) 1e9 = 1e-9
    /// assert_eq!(a.rdiv(b, Ceil)?, a);
    /// // 1e-9 / (Floor) 1e9 = 0
    /// assert_eq!(a.rdiv(b, Floor)?, Amount::ZERO);
    /// # Ok(()) }
    /// # #[cfg(not(feature = "i64"))]
    /// # fn main() {}
    /// ```
    ///
    /// [FixedPoint]: ../struct.FixedPoint.html
    /// [RoundMode]: ./enum.RoundMode.html
    fn rdiv(self, rhs: Rhs, mode: RoundMode) -> Result<Self::Output, Self::Error>;
}

// Impls for primitives.

macro_rules! impl_for_ints {
    ($( $int:ty ),+ $(,)?) => {
        $( impl_for_ints!(@single $int); )*
    };
    (@single $int:ty) => {
        impl Zero for $int {
            const ZERO: Self = 0;
        }

        impl One for $int {
            const ONE: Self = 1;
        }

        impl Bounded for $int {
            const MIN: Self = <$int>::MIN;
            const MAX: Self = <$int>::MAX;
        }

        impl CheckedAdd for $int {
            type Output = $int;
            type Error = ArithmeticError;

            #[inline]
            fn cadd(self, rhs: Self) -> Result<Self::Output, Self::Error> {
                self.checked_add(rhs).ok_or(ArithmeticError::Overflow)
            }

            #[inline]
            fn saturating_add(self, rhs: Self) -> Self::Output {
                <$int>::saturating_add(self, rhs)
            }
        }

        impl CheckedSub for $int {
            type Output = $int;
            type Error = ArithmeticError;

            #[inline]
            fn csub(self, rhs: Self) -> Result<Self::Output, Self::Error> {
                self.checked_sub(rhs).ok_or(ArithmeticError::Overflow)
            }

            #[inline]
            fn saturating_sub(self, rhs: Self) -> Self::Output {
                <$int>::saturating_sub(self, rhs)
            }
        }

        impl CheckedMul for $int {
            type Output = $int;
            type Error = ArithmeticError;

            #[inline]
            fn cmul(self, rhs: Self) -> Result<Self::Output, Self::Error> {
                self.checked_mul(rhs).ok_or(ArithmeticError::Overflow)
            }

            #[inline]
            fn saturating_mul(self, rhs: Self) -> Self::Output {
                <$int>::saturating_mul(self, rhs)
            }
        }

        impl RoundingDiv for $int {
            type Output = $int;
            type Error = ArithmeticError;

            #[inline]
            fn rdiv(self, rhs: Self, mode: RoundMode) -> Result<Self::Output, Self::Error> {
                if rhs == 0 {
                    return Err(ArithmeticError::DivisionByZero);
                }

                let mut result = self / rhs;
                let loss = self - result * rhs;

                if loss != 0 {
                    let sign = self.signum() * rhs.signum();

                    let add_signed_one = if mode == RoundMode::Nearest {
                        let loss_abs = loss.abs();
                        loss_abs + loss_abs >= rhs.abs()
                    } else {
                        mode as i32 == sign as i32
                    };

                    if add_signed_one {
                        result = result.checked_add(sign).ok_or(ArithmeticError::Overflow)?;
                    }
                }

                Ok(result)
            }
        }
    };
}

impl_for_ints!(i8, i16, i32, i64, i128); // TODO: unsigned?