fixed-num 0.2.0

A high-precision, high-performance fixed-point decimal type.
Documentation
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
480
481
482
483
484
pub use std::ops::Add;
pub use std::ops::Sub;
pub use std::ops::Mul;
pub use std::ops::Div;
pub use std::ops::Rem;
pub use std::ops::AddAssign;
pub use std::ops::SubAssign;
pub use std::ops::MulAssign;
pub use std::ops::DivAssign;
pub use std::ops::Neg;

// ==============
// === Traits ===
// ==============

pub mod traits {
    pub use std::ops::Add as _;
    pub use std::ops::Sub as _;
    pub use std::ops::Mul as _;
    pub use std::ops::Div as _;
    pub use std::ops::Rem as _;
    pub use std::ops::AddAssign as _;
    pub use std::ops::SubAssign as _;
    pub use std::ops::MulAssign as _;
    pub use std::ops::DivAssign as _;
    pub use std::ops::Neg as _;
    pub use super::HasMax as _;
    pub use super::HasMin as _;
    pub use super::Signum as _;
    pub use super::Abs as _;
    pub use super::UncheckedAdd as _;
    pub use super::CheckedAdd as _;
    pub use super::SaturatingAdd as _;
    pub use super::UncheckedSub as _;
    pub use super::CheckedSub as _;
    pub use super::SaturatingSub as _;
    pub use super::UncheckedMul as _;
    pub use super::CheckedMul as _;
    pub use super::SaturatingMul as _;
    pub use super::UncheckedDiv as _;
    pub use super::CheckedDiv as _;
    pub use super::SaturatingDiv as _;
    pub use super::Trunc as _;
    pub use super::TruncTo as _;
    pub use super::Floor as _;
    pub use super::FloorTo as _;
    pub use super::Ceil as _;
    pub use super::CeilTo as _;
    pub use super::Round as _;
    pub use super::RoundTo as _;
    pub use super::UncheckedSqrt as _;
    pub use super::CheckedSqrt as _;
    pub use super::UncheckedPow as _;
    pub use super::CheckedPow as _;
    pub use super::UncheckedLog10Floor as _;
    pub use super::CheckedLog10Floor as _;
    pub use super::UncheckedLn as _;
    pub use super::CheckedLn as _;
}

// ==============
// === HasMax ===
// ==============

/// ✅ Checks if `self` is the maximum value.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait HasMax: Sized {
    const MAX: Self;
    #[allow(clippy::wrong_self_convention)]
    fn is_max(self) -> bool;
}

// ==============
// === HasMin ===
// ==============

/// ✅ Checks if `self` is the minimum value.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait HasMin: Sized {
    const MIN: Self;
    #[allow(clippy::wrong_self_convention)]
    fn is_min(self) -> bool;
}

// ==============
// === Signum ===
// ==============

/// ✅ The sign of the number.
///
/// Returns:
/// - `1.0` if positive,
/// - `0.0` if zero,
/// - `-1.0` if negative.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait Signum {
    fn signum(self) -> Self;
    fn signum_i128(self) -> i128;
}

// ===========
// === Abs ===
// ===========

/// ✅ The absolute value of `self`.
///
/// # Panics
///
/// This function never panics. If the value is the minimum representable number, it returns the
/// nearest valid value (e.g. `Self::MAX`).
#[cfg_attr(nightly, const_trait)]
pub trait Abs {
    fn abs(self) -> Self;
}

// ===========
// === Add ===
// ===========

/// Addition without checking for overflow.
///
/// # Panics
///
/// Panics if the result overflows.
#[cfg_attr(nightly, const_trait)]
pub trait UncheckedAdd<Rhs = Self> {
    type Output;
    fn unchecked_add(self, rhs: Rhs) -> Self::Output;
}

/// ✅ Checked addition. Returns `None` if the result overflows.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait CheckedAdd<Rhs = Self> {
    type Output;
    fn checked_add(self, rhs: Rhs) -> Option<Self::Output>;
}

/// ✅ Saturating addition. Clamps the result on overflow.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait SaturatingAdd<Rhs = Self> {
    type Output;
    fn saturating_add(self, rhs: Rhs) -> Self::Output;
}

// ===========
// === Sub ===
// ===========

/// Subtraction without checking for overflow.
///
/// # Panics
///
/// Panics if the result overflows.
#[cfg_attr(nightly, const_trait)]
pub trait UncheckedSub<Rhs = Self> {
    type Output;
    fn unchecked_sub(self, rhs: Rhs) -> Self::Output;
}

/// ✅ Checked subtraction. Returns `None` if the result overflows.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait CheckedSub<Rhs = Self> {
    type Output;
    fn checked_sub(self, rhs: Rhs) -> Option<Self::Output>;
}

/// ✅ Saturating subtraction. Clamps the result on overflow.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait SaturatingSub<Rhs = Self> {
    type Output;
    fn saturating_sub(self, rhs: Rhs) -> Self::Output;
}

// ===========
// === Mul ===
// ===========

/// Multiplication without checking for overflow.
///
/// # Panics
///
/// Panics if the result overflows.
#[cfg_attr(nightly, const_trait)]
pub trait UncheckedMul<Rhs = Self> {
    type Output;
    fn unchecked_mul(self, rhs: Rhs) -> Self::Output;
}

/// ✅ Checked multiplication. Returns `None` if the result overflows.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait CheckedMul<Rhs = Self> {
    type Output;
    fn checked_mul(self, rhs: Rhs) -> Option<Self::Output>;
}

/// ✅ Saturating multiplication. Clamps the result on overflow.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait SaturatingMul<Rhs = Self> {
    type Output;
    fn saturating_mul(self, rhs: Rhs) -> Self::Output;
}

// ===========
// === Div ===
// ===========

/// Division without checking for division by zero or overflow.
///
/// # Panics
///
/// Panics if dividing by zero or if the result overflows.
#[cfg_attr(nightly, const_trait)]
pub trait UncheckedDiv<Rhs = Self> {
    type Output;
    fn unchecked_div(self, rhs: Rhs) -> Self::Output;
}

/// ✅ Checked division. Returns `None` on division by zero or overflow.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait CheckedDiv<Rhs = Self> {
    type Output;
    fn checked_div(self, rhs: Rhs) -> Option<Self::Output>;
}

/// ✅ Saturating division. Returns `Self::MAX` or `Self::MIN` if division by zero or overflow
/// occurs.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait SaturatingDiv<Rhs = Self> {
    type Output;
    fn saturating_div(self, rhs: Rhs) -> Self::Output;
}

// =============
// === Trunc ===
// =============

/// ✅ Truncates fractional digits, rounding toward zero.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait Trunc {
    fn trunc(self) -> Self;
}

/// ✅ Truncates to the specified number of fractional digits.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait TruncTo {
    fn trunc_to(self, digits: i64) -> Self;
}

// =============
// === Floor ===
// =============

/// ✅ Rounds the number toward negative infinity if the result is representable. If rounding would
/// cause an overflow, returns the original value unchanged.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait Floor {
    fn floor(self) -> Self;
}

/// ✅ Rounds the number toward negative infinity to the specified number of fractional digits. If
/// rounding would cause an overflow, returns the original value unchanged.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait FloorTo {
    fn floor_to(self, digits: i64) -> Self;
}

// ============
// === Ceil ===
// ============

/// ✅ Rounds the number toward positive infinity if the result is representable. If rounding would
/// cause an overflow, returns the original value unchanged.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait Ceil {
    fn ceil(self) -> Self;
}

/// ✅ Rounds the number toward positive infinity to the specified number of fractional digits. If
/// rounding would cause an overflow, returns the original value unchanged.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait CeilTo {
    fn ceil_to(self, digits: i64) -> Self;
}

// =============
// === Round ===
// =============

/// ✅ Rounds the number to the nearest integer, away from zero on tie. If rounding would cause an
/// overflow, returns the nearest representable result instead.
///
/// # Examples
///
/// - `...123.4` -> `...123`
/// - `...123.5` -> `...124`
/// - `...123.6` -> `...124`
/// - `...123.6` -> `...123` if `...124` is not representable.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait Round {
    fn round(self) -> Self;
}

/// ✅ Rounds the number to the nearest value with the specified number of fractional digits, away
/// from zero on tie. If rounding would cause an overflow, returns the closest representable result
/// instead.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait RoundTo {
    fn round_to(self, digits: i64) -> Self;
}

// ============
// === Sqrt ===
// ============

/// Returns the square root of `self` without checking the input.
///
/// # Panics
///
/// Panics if `self` is negative.
#[cfg_attr(nightly, const_trait)]
pub trait UncheckedSqrt {
    fn unchecked_sqrt(self) -> Self;
}

/// ✅ Returns the square root of `self`, or `None` if `self` is negative.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait CheckedSqrt: Sized {
    fn checked_sqrt(self) -> Option<Self>;
}

// ===========
// === Pow ===
// ===========

/// Raises `self` to the power of `exp` without checking for overflow or invalid input.
///
/// # Panics
///
/// Panics on overflow or if `exp` is negative and `self` is zero.
#[cfg_attr(nightly, const_trait)]
pub trait UncheckedPow<Exp = Self> {
    type Output;
    fn unchecked_pow(self, exp: Exp) -> Self::Output;
}

/// ✅ aises `self` to the power of `exp`, returning `None` on overflow or invalid input.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait CheckedPow<Rhs = Self> {
    type Output;
    fn checked_pow(self, exp: Rhs) -> Option<Self::Output>;
}

// ==================
// === Log10Floor ===
// ==================

/// Returns the base-10 logarithm of `self`, rounded down to the nearest integer.
///
/// # Panics
///
/// Panics if `self` is zero or negative.
#[cfg_attr(nightly, const_trait)]
pub trait UncheckedLog10Floor {
    fn unchecked_log10_floor(self) -> Self;
}

/// ✅ Returns the base-10 logarithm of `self`, rounded down to the nearest integer,
/// or `None` if `self` is zero or negative.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait CheckedLog10Floor: Sized {
    fn checked_log10_floor(self) -> Option<Self>;
}

// ==========
// === Ln ===
// ==========

/// Returns the natural logarithm of `self`.
///
/// # Panics
///
/// Panics if `self` is zero or negative.
#[cfg_attr(nightly, const_trait)]
pub trait UncheckedLn {
    fn unchecked_ln(self) -> Self;
}

/// ✅ Returns the natural logarithm of `self`, or `None` if `self` is zero or negative.
///
/// # Panics
///
/// This function never panics.
#[cfg_attr(nightly, const_trait)]
pub trait CheckedLn: Sized {
    fn checked_ln(self) -> Option<Self>;
}