uninum 0.1.1

A robust, ergonomic unified number type for Rust with automatic overflow handling, type promotion, and cross-type consistency.
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
//! Tests for mixed operations and edge cases

use uninum::{Number, num};

/// Tests mixed operations with special float values
#[test]
fn test_mixed_operations_with_special_floats() {
    let inf = num!(f64::INFINITY);
    let neg_inf = num!(f64::NEG_INFINITY);
    let nan = num!(f64::NAN);
    let normal = Number::from(42);

    // Test special float + normal
    let result = &inf + &normal;
    assert!(result.is_infinite());
    assert!(result.is_pos_inf());

    let result = &neg_inf + &normal;
    assert!(result.is_infinite());
    assert!(result.is_neg_inf());

    let result = &nan + &normal;
    assert!(result.is_nan());

    // Test normal + special float
    let result = &normal + &inf;
    assert!(result.is_infinite());
    assert!(result.is_pos_inf());

    let result = &normal + &neg_inf;
    assert!(result.is_infinite());
    assert!(result.is_neg_inf());

    let result = &normal + &nan;
    assert!(result.is_nan());

    // Test special float multiplication
    let result = &inf * &normal;
    assert!(result.is_infinite());
    assert!(result.is_pos_inf());

    let result = &neg_inf * &normal;
    assert!(result.is_infinite());
    assert!(result.is_neg_inf());

    let result = &nan * &normal;
    assert!(result.is_nan());

    // Test special float division
    let result = &inf / &normal;
    assert!(result.is_infinite());
    assert!(result.is_pos_inf());

    let result = &neg_inf / &normal;
    assert!(result.is_infinite());
    assert!(result.is_neg_inf());

    let result = &nan / &normal;
    assert!(result.is_nan());

    // Test normal division by zero (should be infinity)
    let result = &normal / &Number::from(0);
    assert!(result.is_infinite());
    assert!(result.is_pos_inf());

    // Test zero divided by zero (should be NaN)
    let result = &Number::from(0) / &Number::from(0);
    assert!(result.is_nan());
}

#[cfg(feature = "decimal")]
#[test]
fn test_mixed_operations_with_decimal() {
    use rust_decimal::Decimal;

    let decimal = Number::from(Decimal::from(42));
    let inf = num!(f64::INFINITY);
    let neg_inf = num!(f64::NEG_INFINITY);
    let nan = num!(f64::NAN);

    // Decimal with special floats promotes to F64
    let result = &decimal + &inf;
    assert!(result.try_get_f64().is_some());
    assert!(result.is_pos_inf());

    let result = &decimal + &neg_inf;
    assert!(result.try_get_f64().is_some());
    assert!(result.is_neg_inf());

    let result = &decimal + &nan;
    assert!(result.try_get_f64().is_some());
    assert!(result.is_nan());

    let result = &decimal * &inf;
    assert!(result.try_get_f64().is_some());
    assert!(result.is_pos_inf());

    let result = &decimal * &neg_inf;
    assert!(result.try_get_f64().is_some());
    assert!(result.is_neg_inf());

    let result = &decimal * &nan;
    assert!(result.try_get_f64().is_some());
    assert!(result.is_nan());

    let result = &decimal / &inf;
    assert!(result.try_get_f64().is_some());
    assert_eq!(result, num!(0.0));

    let result = &decimal / &neg_inf;
    assert!(result.try_get_f64().is_some());
    assert_eq!(result, num!(-0.0));

    let result = &decimal / &nan;
    assert!(result.try_get_f64().is_some());
    assert!(result.is_nan());
}

#[cfg(feature = "decimal")]
#[test]
fn test_mixed_operations_decimal_zero_handling() {
    use rust_decimal::Decimal;

    let positive = Number::from(Decimal::new(42, 1)); // 4.2
    let negative = Number::from(Decimal::new(-42, 1)); // -4.2
    let zero_int = Number::from(0u64);

    let pos_div_zero = positive.clone() / zero_int.clone();
    assert!(
        matches!(pos_div_zero, Number::F64(ref f) if f.0.is_sign_positive() && f.0.is_infinite()),
        "Decimal / 0 should yield +∞ via mixed_operation_div"
    );

    let neg_div_zero = negative.clone() / zero_int.clone();
    assert!(
        matches!(neg_div_zero, Number::F64(ref f) if f.0.is_sign_negative() && f.0.is_infinite()),
        "Negative Decimal / 0 should yield -∞"
    );

    let zero_decimal = Number::from(Decimal::new(0, 0));
    let zero_over_zero = zero_decimal.clone() / zero_int.clone();
    assert!(
        matches!(zero_over_zero, Number::F64(ref f) if f.0.is_nan()),
        "0 / 0 should yield NaN"
    );

    let remainder_nan = positive % zero_int;
    assert!(
        matches!(remainder_nan, Number::F64(ref f) if f.0.is_nan()),
        "Decimal % 0 should yield NaN"
    );
}

#[cfg(feature = "decimal")]
#[test]
fn test_mixed_operations_decimal_conversion_failure_fallback() {
    let huge_float = Number::from(f64::MAX);
    let small_int = Number::from(2u64);

    for operation in [
        huge_float.clone() + small_int.clone(),
        huge_float.clone() - small_int.clone(),
        huge_float.clone() * small_int.clone(),
        huge_float.clone() / small_int.clone(),
        huge_float % small_int,
    ] {
        assert!(
            operation.try_get_decimal().is_none(),
            "out-of-range floats should not convert to Decimal"
        );
        assert!(
            operation.try_get_f64().is_some(),
            "conversion failure should fall back to F64"
        );
    }
}

/// Tests complex mixed type operations
#[test]
fn test_complex_mixed_operations() {
    // Test promotion chain: U64 -> I64 -> F64/Decimal
    let small_u32 = Number::from(255u64);
    let large_u32 = Number::from(u64::from(u32::MAX));

    // U64 + U64 stays U64 if no overflow
    let result = &small_u32 + &large_u32;
    match result {
        n if n.try_get_u64().is_some() => assert_eq!(n.try_get_u64().unwrap(), 4294967550u64),
        n if n.try_get_i64().is_some() => assert_eq!(n.try_get_i64().unwrap(), 4294967550i64),
        #[cfg(feature = "decimal")]
        n if n.try_get_decimal().is_some() => {
            assert_eq!(n.try_get_decimal().unwrap().to_string(), "4294967550");
        }
        _ => panic!("Unexpected type"),
    }

    // Signed + Unsigned type interactions
    let signed_max = Number::from(i64::from(i32::MAX));
    let unsigned_max = Number::from(u64::from(u32::MAX));

    let result = &signed_max + &unsigned_max;
    #[cfg(feature = "decimal")]
    assert!(
        result.try_get_u64().is_some() || result.try_get_decimal().is_some(),
        "Result should be U64 or Decimal"
    );
    #[cfg(not(feature = "decimal"))]
    assert!(
        result.try_get_u64().is_some()
            || result.try_get_i64().is_some()
            || result.try_get_f64().is_some(),
        "Result should be U64, I64, or F64"
    );

    let negative = Number::from(-1000i64);
    let positive = Number::from(500u64);

    let result = &negative + &positive;
    match result {
        n if n.try_get_i64().is_some() => assert_eq!(n.try_get_i64().unwrap(), -500),
        #[cfg(not(feature = "decimal"))]
        n if n.try_get_f64().is_some() => assert_eq!(n.try_get_f64().unwrap(), -500.0),
        #[cfg(feature = "decimal")]
        n if n.try_get_decimal().is_some() => {
            assert_eq!(n.try_get_decimal().unwrap().to_string(), "-500");
        }
        _ => panic!("Unexpected type"),
    }

    let small_unsigned = Number::from(10u64);
    let large_signed = Number::from(100i64);

    let result = &small_unsigned - &large_signed;
    match result {
        n if n.try_get_i64().is_some() => assert_eq!(n.try_get_i64().unwrap(), -90),
        #[cfg(not(feature = "decimal"))]
        n if n.try_get_f64().is_some() => assert_eq!(n.try_get_f64().unwrap(), -90.0),
        #[cfg(feature = "decimal")]
        n if n.try_get_decimal().is_some() => {
            assert_eq!(n.try_get_decimal().unwrap().to_string(), "-90");
        }
        _ => panic!("Unexpected type"),
    }
}

/// Tests cascading operations that trigger multiple type promotions
#[test]
fn test_cascading_type_promotions() {
    let start = Number::from(100u64);
    let multiplier = Number::from(100u64);
    let addend = Number::from(100u64);

    // Start with U64
    let step1 = &start * &multiplier; // 10000
    assert!(step1.try_get_u64().is_some() || step1.try_get_i64().is_some());

    // This may stay in U64 or promote
    let step2 = &step1 * &multiplier; // 1000000
    #[cfg(feature = "decimal")]
    assert!(
        step2.try_get_u64().is_some()
            || step2.try_get_i64().is_some()
            || step2.try_get_decimal().is_some()
    );
    #[cfg(not(feature = "decimal"))]
    assert!(step2.try_get_u64().is_some() || step2.try_get_i64().is_some());

    // Continue the chain
    let step3 = &step2 + &addend; // 1000100
    #[cfg(feature = "decimal")]
    assert!(step3.try_get_u64().is_some() || step3.try_get_decimal().is_some());
    #[cfg(not(feature = "decimal"))]
    assert!(step3.try_get_u64().is_some() || step3.try_get_i64().is_some());

    let large_multiplier = Number::from(10000u64);
    let step4 = &step3 * &large_multiplier; // 10001000000
    #[cfg(feature = "decimal")]
    assert!(step4.try_get_u64().is_some() || step4.try_get_decimal().is_some());
    #[cfg(not(feature = "decimal"))]
    assert!(step4.try_get_u64().is_some() || step4.try_get_i64().is_some());
}

/// Tests operations with NaN, Infinity, and normal values
#[test]
fn test_special_value_propagation() {
    let nan_f64 = num!(f64::NAN);
    let inf_f64 = num!(f64::INFINITY);
    let neg_inf_f64 = num!(f64::NEG_INFINITY);
    let normal = Number::from(42);

    // NaN propagation through operations
    assert!((&nan_f64 + &normal).is_nan());
    assert!((&normal + &nan_f64).is_nan());
    assert!((&nan_f64 - &normal).is_nan());
    assert!((&normal - &nan_f64).is_nan());
    assert!((&nan_f64 * &normal).is_nan());
    assert!((&normal * &nan_f64).is_nan());
    assert!((&nan_f64 / &normal).is_nan());
    assert!((&normal / &nan_f64).is_nan());

    // Infinity propagation
    assert!((&inf_f64 + &normal).is_pos_inf());
    assert!((&normal + &inf_f64).is_pos_inf());
    assert!((&inf_f64 - &normal).is_pos_inf());
    assert!((&normal - &inf_f64).is_neg_inf());
    assert!((&inf_f64 * &normal).is_pos_inf());
    assert!((&normal * &inf_f64).is_pos_inf());
    assert!((&inf_f64 / &normal).is_pos_inf());

    // Normal / Infinity = 0
    let result = &normal / &inf_f64;
    assert_eq!(result.try_get_f64(), Some(0.0));

    let result = &normal / &neg_inf_f64;
    assert_eq!(result.try_get_f64(), Some(-0.0));

    // Infinity - Infinity = NaN
    assert!((&inf_f64 - &inf_f64).is_nan());
    assert!((&neg_inf_f64 - &neg_inf_f64).is_nan());
    assert!((&inf_f64 + &neg_inf_f64).is_nan());

    // Infinity / Infinity = NaN
    assert!((&inf_f64 / &inf_f64).is_nan());
    assert!((&neg_inf_f64 / &neg_inf_f64).is_nan());
    assert!((&inf_f64 / &neg_inf_f64).is_nan());

    // 0 * Infinity = NaN
    let zero = Number::from(0);
    assert!((&zero * &inf_f64).is_nan());
    assert!((&zero * &neg_inf_f64).is_nan());
}

/// Tests operator precedence with mixed types
#[test]
fn test_operator_precedence_mixed_types() {
    // Test that multiplication happens before addition
    let result = Number::from(10) + Number::from(5) * Number::from(2);
    assert_eq!(result, Number::from(20)); // 10 + (5 * 2) = 20

    let num = Number::from(10);
    let result = &num + Number::from(5) * Number::from(2) - #
    assert_eq!(result, Number::from(10)); // 10 + (5 * 2) - 10 = 10

    // Test chained operations
    let result = Number::from(200) + Number::from(100) - Number::from(50);
    assert_eq!(result, Number::from(250)); // 200 + 100 - 50 = 250

    let result = Number::from(10) + num!(2.5) * Number::from(2);
    assert_eq!(result, num!(15.0)); // 10 + (2.5 * 2) = 15.0
}

/// Tests that overflow to F64 preserves correct values
#[test]
fn test_overflow_to_f64_preserves_values() {
    // Test various overflow scenarios
    assert_eq!(
        Number::from(1000u64) * Number::from(2u64),
        Number::from(2000u64)
    );
    assert_eq!(
        Number::from(-10i64) + Number::from(-20i64),
        Number::from(-30i64)
    );

    // Float operations preserve precision where possible
    #[cfg(feature = "decimal")]
    assert!(
        (num!(1.0) + num!(2.0)).try_get_f64().is_some()
            || (num!(1.0) + num!(2.0)).try_get_decimal().is_some()
    );
    #[cfg(not(feature = "decimal"))]
    assert!((num!(1.0) + num!(2.0)).try_get_f64().is_some());
}

/// Tests edge cases with NaN comparisons
#[test]
fn test_nan_edge_cases() {
    let nan = num!(f64::NAN);
    let inf = num!(f64::INFINITY);
    let normal = Number::from(42);

    // In uninum, NaN == NaN returns true (different from IEEE 754)
    assert_eq!(nan.clone(), nan.clone());

    // NaN comparisons in uninum may behave differently from IEEE 754
    // Test that NaN is not equal to normal values
    assert_ne!(nan.clone(), normal.clone());

    // In uninum, NaN comparisons might not follow IEEE 754 exactly
    // The key requirement is that NaN != normal values

    // Operations with NaN
    // x^0 = 1 even when x is NaN (mathematical convention)
    assert_eq!(nan.clone().pow(&Number::from(0)), Number::from(1));
    assert!(inf.pow(&nan).is_nan());
}

/// Tests pow operation with mixed types
#[test]
fn test_pow_mixed_types() {
    // Integer powers
    assert_eq!(Number::from(42).pow(&Number::from(0)), Number::from(1));

    // Float base with integer exponent
    assert_eq!(num!(2.5).pow(&Number::from(2)), num!(6.25));

    // Special cases
    let inf = num!(f64::INFINITY);
    let nan = num!(f64::NAN);
    let zero = Number::from(0);

    // 0^0 is typically 1 in most implementations
    assert_eq!(zero.clone().pow(&zero), Number::from(1));

    // Infinity^n
    assert!(inf.clone().pow(&Number::from(2)).is_pos_inf());
    assert_eq!(inf.clone().pow(&Number::from(-1)).try_get_f64(), Some(0.0));

    // NaN^n is NaN (except n=0)
    assert!(nan.clone().pow(&Number::from(2)).is_nan());
    assert_eq!(nan.pow(&Number::from(0)), Number::from(1));
}