tcal_rs 0.2.0

Number theory functions library - Rust port of libqalculate number theory module
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
//! # Rounding and Absolute Value Functions
//!
//! This module provides comprehensive rounding operations and absolute value computations.
//!
//! ## Rounding Modes
//!
//! Different applications require different rounding strategies. This module supports
//! nine standard rounding modes defined in IEEE 754 and mathematical practice.
//!
//! ### Rounding Mode Reference
//!
//! | Mode | Description | Example at 0.5 | Example at 1.5 |
//! |------|-------------|-----------------|-----------------|
//! | `HalfToEven` | Round to nearest even (Banker's) | 0 | 2 |
//! | `HalfAwayFromZero` | Round away from zero | 1 | 2 |
//! | `HalfTowardZero` | Round toward zero | 0 | 1 |
//! | `HalfUp` | Round up (toward +∞) | 1 | 2 |
//! | `HalfDown` | Round down (toward -∞) | 0 | 1 |
//! | `Up` | Ceiling (toward +∞) | 1 | 2 |
//! | `Down` | Floor (toward -∞) | 0 | 1 |
//! | `TowardZero` | Truncate | 0 | 1 |
//! | `AwayFromZero` | Away from zero | 1 | 2 |
//!
//! ## Mathematical Functions
//!
//! ### Floor Function ⌊x⌋
//! Greatest integer less than or equal to x:
//! ```text
//! ⌊2.3⌋ = 2, ⌊-2.3⌋ = -3
//! ```
//!
//! ### Ceiling Function ⌈x⌉
//! Smallest integer greater than or equal to x:
//! ```text
//! ⌈2.3⌉ = 3, ⌈-2.3⌉ = -2
//! ```
//!
//! ### Signum Function sgn(x)
//! Sign of a number:
//! ```text
//! sgn(x) = -1 if x < 0, 0 if x = 0, 1 if x > 0
//! ```
//!
//! ## Applications
//!
//! - **Financial**: Banker's rounding (HalfToEven) for unbiased calculations
//! - **Statistics**: Various modes for confidence intervals
//! - **Graphics**: Rounding for pixel alignment
//! - **Scientific**: Different modes for error propagation

/// # Rounding Mode Enumeration
///
/// Defines nine standard rounding modes for the `round` function.
///
/// ## Mode Descriptions
///
/// ### `HalfToEven` (Banker's Rounding)
/// - At exactly 0.5, rounds to the nearest even integer
/// - Minimizes cumulative error in statistical calculations
/// - Default mode in IEEE 754 and many programming languages
/// - Example: 2.5 → 2, 3.5 → 4
///
/// ### `HalfAwayFromZero`
/// - At exactly 0.5, always rounds away from zero
/// - Common in everyday arithmetic ("round half up" for positive)
/// - Example: 2.5 → 3, -2.5 → -3
///
/// ### `HalfTowardZero`
/// - At exactly 0.5, always rounds toward zero
/// - Less commonly used
/// - Example: 2.5 → 2, -2.5 → -2
///
/// ### `HalfUp`
/// - At exactly 0.5, rounds toward +∞
/// - Example: 2.5 → 3, -2.5 → -2
///
/// ### `HalfDown`
/// - At exactly 0.5, rounds toward -∞
/// - Example: 2.5 → 2, -2.5 → -3
///
/// ### `Up` (Ceiling)
/// - Always rounds toward +∞
/// - Equivalent to the `ceil` function
/// - Example: 2.3 → 3, -2.3 → -2
///
/// ### `Down` (Floor)
/// - Always rounds toward -∞
/// - Equivalent to the `floor` function
/// - Example: 2.3 → 2, -2.3 → -3
///
/// ### `TowardZero`
/// - Always rounds toward zero (truncation)
/// - Removes fractional part
/// - Example: 2.3 → 2, -2.3 → -2
///
/// ### `AwayFromZero`
/// - Always rounds away from zero
/// - Example: 2.3 → 3, -2.3 → -3
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RoundingMode {
    /// Round half to even (banker's rounding)
    #[default]
    HalfToEven = 0,
    /// Round half away from zero
    HalfAwayFromZero = 1,
    /// Round half toward zero
    HalfTowardZero = 2,
    /// Round half up (toward positive infinity)
    HalfUp = 3,
    /// Round half down (toward negative infinity)
    HalfDown = 4,
    /// Round toward positive infinity (ceiling)
    Up = 5,
    /// Round toward negative infinity (floor)
    Down = 6,
    /// Round toward zero (truncation)
    TowardZero = 7,
    /// Round away from zero
    AwayFromZero = 8,
}

/// Computes the absolute value of a rational number.
///
/// # Arguments
///
/// * `numerator` - Numerator of the rational number
/// * `denominator` - Denominator of the rational number
///
/// # Returns
///
/// Absolute value as (numerator, denominator)
///
/// # Examples
///
/// ```
/// use tcal_rs::abs;
///
/// assert_eq!(abs(7, 3), (7, 3));
/// assert_eq!(abs(-7, 3), (7, 3));
/// ```
pub fn abs(numerator: i64, denominator: i64) -> (i64, i64) {
    (numerator.abs(), denominator.abs())
}

/// Computes the absolute value of an integer.
///
/// # Examples
///
/// ```
/// use tcal_rs::abs_integer;
///
/// assert_eq!(abs_integer(-42), 42);
/// ```
pub fn abs_integer(n: i64) -> i64 {
    n.abs()
}

/// Computes the absolute value of an i128 integer.
pub fn abs_integer_i128(n: i128) -> i128 {
    n.abs()
}

/// # Ceiling Function ⌈x⌉
///
/// Computes the ceiling of a rational number, which is the smallest integer
/// greater than or equal to the number (rounds toward +∞).
///
/// ## Mathematical Definition
/// ```text
/// ⌈x⌉ = min{k ∈ ℤ : k ≥ x}
/// ```
///
/// For a rational number a/b:
/// ```text
/// ⌈a/b⌉ = a/b if a is divisible by b
/// ⌈a/b⌉ = ⌊a/b⌋ + 1 otherwise
/// ```
///
/// ## Examples
/// ```
/// use tcal_rs::ceil;
///
/// // Positive numbers
/// assert_eq!(ceil(7, 3), 3);    // 2.33... → 3
/// assert_eq!(ceil(6, 3), 2);    // Exactly 2
///
/// // Negative numbers (rounds toward +∞, i.e., less negative)
/// assert_eq!(ceil(-7, 3), -2);  // -2.33... → -2
/// assert_eq!(ceil(-6, 3), -2);  // Exactly -2
///
/// // Edge cases
/// assert_eq!(ceil(1, 2), 1);    // 0.5 → 1
/// assert_eq!(ceil(-1, 2), 0);   // -0.5 → 0
/// ```
///
/// # Arguments
/// * `numerator` - Numerator of the rational number
/// * `denominator` - Denominator of the rational number (non-zero)
///
/// # Returns
/// The ceiling as an i64
pub fn ceil(numerator: i64, denominator: i64) -> i64 {
    let mut result = numerator / denominator;
    let remainder = numerator % denominator;

    if remainder > 0 && denominator > 0 || remainder < 0 && denominator < 0 {
        result += 1;
    }

    result
}

/// Computes the ceiling of a float.
pub fn ceil_float(f: f64) -> i64 {
    if f > 0.0 && f.fract() != 0.0 {
        f as i64 + 1
    } else {
        f as i64
    }
}

/// # Floor Function ⌊x⌋
///
/// Computes the floor of a rational number, which is the greatest integer
/// less than or equal to the number (rounds toward -∞).
///
/// ## Mathematical Definition
/// ```text
/// ⌊x⌋ = max{k ∈ ℤ : k ≤ x}
/// ```
///
/// For a rational number a/b:
/// ```text
/// ⌊a/b⌋ = a/b if a is divisible by b
/// ⌊a/b⌋ = ⌈a/b⌉ - 1 otherwise
/// ```
///
/// ## Examples
/// ```
/// use tcal_rs::floor;
///
/// // Positive numbers
/// assert_eq!(floor(7, 3), 2);    // 2.33... → 2
/// assert_eq!(floor(6, 3), 2);    // Exactly 2
///
/// // Negative numbers (rounds toward -∞, i.e., more negative)
/// assert_eq!(floor(-7, 3), -3);  // -2.33... → -3
/// assert_eq!(floor(-6, 3), -2);  // Exactly -2
///
/// // Edge cases
/// assert_eq!(floor(1, 2), 0);    // 0.5 → 0
/// assert_eq!(floor(-1, 2), -1);  // -0.5 → -1
/// ```
///
/// # Arguments
/// * `numerator` - Numerator of the rational number
/// * `denominator` - Denominator of the rational number (non-zero)
///
/// # Returns
/// The floor as an i64
pub fn floor(numerator: i64, denominator: i64) -> i64 {
    let mut result = numerator / denominator;
    let remainder = numerator % denominator;

    if remainder < 0 && denominator > 0 || remainder > 0 && denominator < 0 {
        result -= 1;
    }

    result
}

/// Computes the floor of a float.
pub fn floor_float(f: f64) -> i64 {
    if f < 0.0 && f.fract() != 0.0 {
        f as i64 - 1
    } else {
        f as i64
    }
}

/// Computes the truncation of a rational number (round toward zero).
///
/// # Arguments
///
/// * `numerator` - Numerator of the rational number
/// * `denominator` - Denominator of the rational number
///
/// # Returns
///
/// The truncated value as an i64
///
/// # Examples
///
/// ```
/// use tcal_rs::trunc;
///
/// assert_eq!(trunc(7, 3), 2);
/// assert_eq!(trunc(-7, 3), -2);
/// assert_eq!(trunc(6, 3), 2);
/// ```
pub fn trunc(numerator: i64, denominator: i64) -> i64 {
    numerator / denominator
}

/// Computes the truncation of a float.
pub fn trunc_float(f: f64) -> i64 {
    f as i64
}

/// # General Rounding Function
///
/// Rounds a rational number to the nearest integer using the specified rounding mode.
///
/// ## Algorithm
///
/// 1. Extract integer part and remainder
/// 2. Convert remainder to floating-point fraction for precise comparison
/// 3. Apply the selected rounding mode:
///    - Compare fraction to 0.5
///    - Handle ties (exactly 0.5) according to mode
///    - Apply directional modes (Up, Down, etc.)
///
/// ## Examples
/// ```
/// use tcal_rs::{round, RoundingMode};
///
/// // HalfToEven (Banker's rounding)
/// assert_eq!(round(7, 3, RoundingMode::HalfToEven), 2);  // 2.33... → 2
/// assert_eq!(round(8, 3, RoundingMode::HalfToEven), 3);  // 2.66... → 3
/// assert_eq!(round(5, 2, RoundingMode::HalfToEven), 2);  // 2.5 → 2 (even)
/// assert_eq!(round(7, 2, RoundingMode::HalfToEven), 4);  // 3.5 → 4 (even)
///
/// // HalfAwayFromZero
/// assert_eq!(round(5, 2, RoundingMode::HalfAwayFromZero), 3);  // 2.5 → 3
/// assert_eq!(round(-5, 2, RoundingMode::HalfAwayFromZero), -3); // -2.5 → -3
///
/// // Ceiling (Up)
/// assert_eq!(round(7, 3, RoundingMode::Up), 3);   // Same as ceil(7, 3)
/// assert_eq!(round(-7, 3, RoundingMode::Up), -2); // Same as ceil(-7, 3)
///
/// // Floor (Down)
/// assert_eq!(round(7, 3, RoundingMode::Down), 2);   // Same as floor(7, 3)
/// assert_eq!(round(-7, 3, RoundingMode::Down), -3); // Same as floor(-7, 3)
/// ```
///
/// # Arguments
/// * `numerator` - Numerator of the rational number
/// * `denominator` - Denominator of the rational number (non-zero)
/// * `mode` - Rounding mode to use
///
/// # Returns
/// The rounded value as an i64
pub fn round(numerator: i64, denominator: i64, mode: RoundingMode) -> i64 {
    let integer_part = numerator / denominator;
    let remainder = numerator.abs() % denominator.abs();

    if remainder == 0 {
        return integer_part;
    }

    // Convert to float for precise comparison
    let fraction = (remainder as f64) / (denominator.abs() as f64);

    match mode {
        RoundingMode::HalfToEven => {
            if fraction > 0.5 {
                integer_part + integer_part.signum()
            } else if fraction < 0.5 {
                integer_part
            } else {
                // Exactly at 0.5, round to nearest even
                if integer_part % 2 == 0 {
                    integer_part
                } else {
                    integer_part + integer_part.signum()
                }
            }
        }
        RoundingMode::HalfAwayFromZero => {
            if fraction >= 0.5 {
                integer_part + integer_part.signum()
            } else {
                integer_part
            }
        }
        RoundingMode::HalfTowardZero => {
            if fraction > 0.5 {
                integer_part + integer_part.signum()
            } else {
                integer_part
            }
        }
        RoundingMode::HalfUp => {
            if fraction >= 0.5 {
                integer_part + 1
            } else {
                integer_part
            }
        }
        RoundingMode::HalfDown => {
            if fraction > 0.5 {
                integer_part + 1
            } else {
                integer_part
            }
        }
        RoundingMode::Up => ceil(numerator, denominator),
        RoundingMode::Down => floor(numerator, denominator),
        RoundingMode::TowardZero => trunc(numerator, denominator),
        RoundingMode::AwayFromZero => {
            if integer_part >= 0 {
                ceil(numerator, denominator)
            } else {
                floor(numerator, denominator)
            }
        }
    }
}

/// Signum function - returns the sign of a number.
///
/// Returns:
/// - `-1` if the number is negative
/// - `0` if the number is zero
/// - `1` if the number is positive
///
/// # Examples
///
/// ```
/// use tcal_rs::signum;
///
/// assert_eq!(signum(-5), -1);
/// assert_eq!(signum(0), 0);
/// assert_eq!(signum(5), 1);
/// ```
pub fn signum(n: i64) -> i64 {
    n.signum()
}

/// Signum function for i128.
pub fn signum_i128(n: i128) -> i128 {
    n.signum()
}

/// Signum function for generic integers.
pub fn signum_integer(n: i64) -> i64 {
    if n == 0 {
        0
    } else if n > 0 {
        1
    } else {
        -1
    }
}

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

    #[test]
    fn test_abs() {
        assert_eq!(abs(7, 3), (7, 3));
        assert_eq!(abs(-7, 3), (7, 3));
        assert_eq!(abs(7, -3), (7, 3));
    }

    #[test]
    fn test_abs_integer() {
        assert_eq!(abs_integer(-42), 42);
        assert_eq!(abs_integer(42), 42);
        assert_eq!(abs_integer(0), 0);
    }

    #[test]
    fn test_ceil() {
        assert_eq!(ceil(7, 3), 3);
        assert_eq!(ceil(-7, 3), -2);
        assert_eq!(ceil(6, 3), 2);
        assert_eq!(ceil(-6, 3), -2);
        assert_eq!(ceil(1, 2), 1);
        assert_eq!(ceil(-1, 2), 0);
    }

    #[test]
    fn test_floor() {
        assert_eq!(floor(7, 3), 2);
        assert_eq!(floor(-7, 3), -3);
        assert_eq!(floor(6, 3), 2);
        assert_eq!(floor(-6, 3), -2);
        assert_eq!(floor(1, 2), 0);
        assert_eq!(floor(-1, 2), -1);
    }

    #[test]
    fn test_trunc() {
        assert_eq!(trunc(7, 3), 2);
        assert_eq!(trunc(-7, 3), -2);
        assert_eq!(trunc(6, 3), 2);
        assert_eq!(trunc(1, 2), 0);
        assert_eq!(trunc(-1, 2), 0);
    }

    #[test]
    fn test_round_half_to_even() {
        assert_eq!(round(7, 3, RoundingMode::HalfToEven), 2);
        assert_eq!(round(8, 3, RoundingMode::HalfToEven), 3);
        assert_eq!(round(5, 2, RoundingMode::HalfToEven), 2);
        assert_eq!(round(7, 2, RoundingMode::HalfToEven), 4);
        assert_eq!(round(9, 2, RoundingMode::HalfToEven), 4);
        assert_eq!(round(11, 2, RoundingMode::HalfToEven), 6);
    }

    #[test]
    fn test_round_half_away_from_zero() {
        assert_eq!(round(7, 3, RoundingMode::HalfAwayFromZero), 2);
        assert_eq!(round(8, 3, RoundingMode::HalfAwayFromZero), 3);
        assert_eq!(round(5, 2, RoundingMode::HalfAwayFromZero), 3);
        assert_eq!(round(-5, 2, RoundingMode::HalfAwayFromZero), -3);
    }

    #[test]
    fn test_signum() {
        assert_eq!(signum(-5), -1);
        assert_eq!(signum(0), 0);
        assert_eq!(signum(5), 1);
    }

    #[test]
    fn test_ceil_float() {
        assert_eq!(ceil_float(2.3), 3);
        assert_eq!(ceil_float(-2.3), -2);
        assert_eq!(ceil_float(2.0), 2);
    }

    #[test]
    fn test_floor_float() {
        assert_eq!(floor_float(2.3), 2);
        assert_eq!(floor_float(-2.3), -3);
        assert_eq!(floor_float(2.0), 2);
    }
}