dashu-float 0.4.5

A big float library supporting arbitrary precision, arbitrary base and arbitrary rounding mode
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
use core::convert::TryFrom;
use core::str::FromStr;
use dashu_base::{Approximation::*, ConversionError::*};
use dashu_float::{
    round::{
        mode::{HalfAway, Zero},
        Rounding::*,
    },
    DBig, FBig,
};

mod helper_macros;

type FBin = FBig;
type FHex = FBig<Zero, 16>;

#[test]
#[rustfmt::skip::macros(fbig)]
fn test_base_change() {
    // binary -> decimal
    // 5 decimal digits precision < 20 binary digits precision
    assert_eq!(fbig!(0x12345).with_rounding::<HalfAway>().to_decimal(), Exact(dbig!(74565)));
    assert_eq!(
        fbig!(-0x12345p1).with_rounding::<HalfAway>().to_decimal(),
        Exact(dbig!(-149130))
    );
    assert_eq!(
        fbig!(0x12345p100).with_rounding::<HalfAway>().to_decimal(),
        Inexact(dbig!(945224e29), AddOne)
    );
    assert_eq!(
        fbig!(0x12345p-1).with_rounding::<HalfAway>().to_decimal(),
        Exact(dbig!(372825e-1))
    );
    assert_eq!(
        fbig!(-0x12345p-100)
            .with_rounding::<HalfAway>()
            .to_decimal(),
        Inexact(dbig!(-588214e-31), NoOp)
    );
    assert_eq!(FBig::<HalfAway, 2>::INFINITY.to_decimal(), Inexact(DBig::INFINITY, NoOp));
    assert_eq!(
        FBig::<HalfAway, 2>::NEG_INFINITY.to_decimal(),
        Inexact(DBig::NEG_INFINITY, NoOp)
    );

    assert_eq!(
        fbig!(0x12345)
            .with_rounding::<HalfAway>()
            .with_base_and_precision::<10>(10),
        Exact(dbig!(74565))
    );
    assert_eq!(
        fbig!(-0x12345p1)
            .with_rounding::<HalfAway>()
            .with_base_and_precision::<10>(10),
        Exact(dbig!(-149130))
    );
    assert_eq!(
        fbig!(0x12345p100)
            .with_rounding::<HalfAway>()
            .with_base_and_precision::<10>(10),
        Inexact(dbig!(9452236701e25), AddOne)
    );
    assert_eq!(
        fbig!(0x12345p-1)
            .with_rounding::<HalfAway>()
            .with_base_and_precision::<10>(10),
        Exact(dbig!(372825e-1))
    );
    assert_eq!(
        fbig!(-0x12345p-100)
            .with_rounding::<HalfAway>()
            .with_base_and_precision::<10>(10),
        Inexact(dbig!(-5882141340e-35), SubOne)
    );

    // decimal -> binary
    // 16 binary digits precision < 5 decimal digits precision
    assert_eq!(dbig!(12345).with_rounding::<Zero>().to_binary(), Exact(fbig!(0x3039)));
    assert_eq!(dbig!(-12345e1).with_rounding::<Zero>().to_binary(), Exact(fbig!(-0xf11dp1)));
    assert_eq!(
        dbig!(12345e100).with_rounding::<Zero>().to_binary(),
        Inexact(fbig!(0xdc78p330), NoOp)
    );
    assert_eq!(dbig!(12345e-1).with_rounding::<Zero>().to_binary(), Exact(fbig!(0x9a5p-1)));
    assert_eq!(
        dbig!(-12345e-100).with_rounding::<Zero>().to_binary(),
        Inexact(fbig!(-0xa8c2p-334), NoOp)
    );
    assert_eq!(DBig::INFINITY.to_binary(), Inexact(FBig::INFINITY, NoOp));
    assert_eq!(DBig::NEG_INFINITY.to_binary(), Inexact(FBig::NEG_INFINITY, NoOp));

    assert_eq!(
        dbig!(12345)
            .with_rounding::<Zero>()
            .with_base_and_precision::<2>(30),
        Exact(fbig!(0x3039))
    );
    assert_eq!(
        dbig!(-12345e1)
            .with_rounding::<Zero>()
            .with_base_and_precision::<2>(30),
        Exact(fbig!(-0xf11dp1))
    );
    assert_eq!(
        dbig!(12345e100)
            .with_rounding::<Zero>()
            .with_base_and_precision::<2>(30),
        Inexact(fbig!(0x371e2de9p316), NoOp)
    );
    assert_eq!(
        dbig!(12345e-1)
            .with_rounding::<Zero>()
            .with_base_and_precision::<2>(30),
        Exact(fbig!(0x9a5p-1))
    );
    assert_eq!(
        dbig!(-12345e-100)
            .with_rounding::<Zero>()
            .with_base_and_precision::<2>(30),
        Inexact(fbig!(-0x2a30a4e2p-348), NoOp)
    );

    // binary -> hexadecimal
    assert_eq!(fbig!(0x12345).with_base::<16>(), Exact(FHex::from_parts(ibig!(0x12345), 0)));
    assert_eq!(fbig!(-0x12345p1).with_base::<16>(), Exact(FHex::from_parts(ibig!(-0x2468a), 0)));
    assert_eq!(
        fbig!(0x12345p100).with_base::<16>(),
        Exact(FHex::from_parts(ibig!(0x12345), 25))
    );
    assert_eq!(
        fbig!(0x54321p111).with_base::<16>(),
        Inexact(FHex::from_parts(ibig!(0x2a190), 28), NoOp)
    );
    assert_eq!(fbig!(0x12345p-1).with_base::<16>(), Exact(FHex::from_parts(ibig!(0x91a28), -1)));
    assert_eq!(
        fbig!(-0x12345p-100).with_base::<16>(),
        Exact(FHex::from_parts(ibig!(-0x12345), -25))
    );
    assert_eq!(
        fbig!(-0x12345p-111).with_base::<16>(),
        Exact(FHex::from_parts(ibig!(-0x2468a), -28))
    );
    assert_eq!(FBig::<Zero, 2>::INFINITY.with_base::<16>(), Inexact(FHex::INFINITY, NoOp));
    assert_eq!(
        FBig::<Zero, 2>::NEG_INFINITY.with_base::<16>(),
        Inexact(FHex::NEG_INFINITY, NoOp)
    );

    // hexadecimal -> binary
    assert_eq!(FHex::from_parts(ibig!(0x12345), 0).to_binary(), Exact(fbig!(0x12345)));
    assert_eq!(FHex::from_parts(ibig!(-0x12345), 1).to_binary(), Exact(fbig!(-0x12345p4)));
    assert_eq!(FHex::from_parts(ibig!(0x12345), 100).to_binary(), Exact(fbig!(0x12345p400)));
    assert_eq!(FHex::from_parts(ibig!(0x12345), -1).to_binary(), Exact(fbig!(0x12345p-4)));
    assert_eq!(FHex::from_parts(ibig!(-0x12345), 100).to_binary(), Exact(fbig!(-0x12345p400)));
    assert_eq!(FHex::INFINITY.to_binary(), Inexact(FBig::<Zero, 2>::INFINITY, NoOp));
    assert_eq!(FHex::NEG_INFINITY.to_binary(), Inexact(FBig::<Zero, 2>::NEG_INFINITY, NoOp));
}

#[test]
#[should_panic]
fn test_base_change_unlimited_precision() {
    let _ = dbig!(1234e-1).with_precision(0).unwrap().with_base::<2>();
}

#[test]
#[rustfmt::skip::macros(fbig, dbig)]
fn test_base_change_exact_unlimited_precision() {
    use dashu_int::IBig;

    type FBinH = FBig<HalfAway, 2>;
    type DBinZ = FBig<Zero, 10>;

    // B=10 → B=2, positive exponent (below threshold)
    assert_eq!(
        dbig!(5e1).with_precision(0).unwrap().with_base::<2>(),
        Exact(FBinH::from_str("0x19p1").unwrap())
    );
    // B=10 → B=2, positive exponent exceeding 32-bit threshold
    assert_eq!(
        dbig!(1e20).with_precision(0).unwrap().with_base::<2>(),
        Exact(FBinH::from_str("0x56BC75E2D631p20").unwrap())
    );
    // B=10 → B=2, negative exponent, exact division by r^|exp|
    assert_eq!(
        dbig!(5e-1).with_precision(0).unwrap().with_base::<2>(),
        Exact(FBinH::from_str("0x1p-1").unwrap())
    );
    assert_eq!(
        dbig!(125e-3).with_precision(0).unwrap().with_base::<2>(),
        Exact(FBinH::from_str("0x1p-3").unwrap())
    );
    assert_eq!(
        dbig!(3125e-5).with_precision(0).unwrap().with_base::<2>(),
        Exact(FBinH::from_str("0x1p-5").unwrap())
    );
    // B=10 → B=2, negative exponent exceeding 32-bit threshold, exact division
    // 5^20 = 95367431640625, and 95367431640625 × 10^(-20) = 2^(-20)
    assert_eq!(
        dbig!(95367431640625e-20)
            .with_precision(0)
            .unwrap()
            .with_base::<2>(),
        Exact(FBinH::from_str("0x1p-20").unwrap())
    );
    // B=2 → B=10, positive exponent (NewB is a multiple of B)
    assert_eq!(
        fbig!(0x1p20).with_precision(0).unwrap().with_base::<10>(),
        Exact(DBinZ::from_str("1048576").unwrap())
    );
    assert_eq!(
        fbig!(0x5p10).with_precision(0).unwrap().with_base::<10>(),
        Exact(DBinZ::from_str("5120").unwrap())
    );

    // --- Other base combinations ---

    // B=6 → B=2 (factor_base(6,2)=(1,3), positive exp)
    // 3 × 6^2 = 108 = 27 × 2^2
    {
        type F6 = FBig<HalfAway, 6>;
        assert_eq!(
            F6::from_parts(IBig::from(3), 2)
                .with_precision(0)
                .unwrap()
                .with_base::<2>(),
            Exact(FBinH::from_parts(IBig::from(27), 2))
        );
    }

    // B=6 → B=2 (factor_base(6,2)=(1,3), negative exp, exact)
    // 9 × 6^(-2) = 9/36 = 0.25 = 1 × 2^(-2)
    {
        type F6 = FBig<HalfAway, 6>;
        assert_eq!(
            F6::from_parts(IBig::from(9), -2)
                .with_precision(0)
                .unwrap()
                .with_base::<2>(),
            Exact(FBinH::from_parts(IBig::from(1), -2))
        );
    }

    // B=12 → B=2 (factor_base(12,2)=(2,3), positive exp)
    // 3 × 12^2 = 432 = 27 × 2^4
    {
        type F12 = FBig<HalfAway, 12>;
        assert_eq!(
            F12::from_parts(IBig::from(3), 2)
                .with_precision(0)
                .unwrap()
                .with_base::<2>(),
            Exact(FBinH::from_parts(IBig::from(27), 4))
        );
    }

    // B=12 → B=3 (factor_base(12,3)=(1,4), positive exp)
    // 5 × 12^2 = 720 = 80 × 3^2
    {
        type F12 = FBig<HalfAway, 12>;
        type F3 = FBig<HalfAway, 3>;
        assert_eq!(
            F12::from_parts(IBig::from(5), 2)
                .with_precision(0)
                .unwrap()
                .with_base::<3>(),
            Exact(F3::from_parts(IBig::from(80), 2))
        );
    }

    // B=12 → B=4 (factor_base(12,4)=(1,3), negative exp, exact)
    // 9 × 12^(-1) = 0.75 = 3 × 4^(-1)
    {
        type F12 = FBig<HalfAway, 12>;
        type F4 = FBig<HalfAway, 4>;
        assert_eq!(
            F12::from_parts(IBig::from(9), -1)
                .with_precision(0)
                .unwrap()
                .with_base::<4>(),
            Exact(F4::from_parts(IBig::from(3), -1))
        );
    }

    // B=3 → B=6 (NewB % B == 0, positive exp)
    // 5 × 3^3 = 135
    {
        type F3 = FBig<HalfAway, 3>;
        type F6 = FBig<HalfAway, 6>;
        assert_eq!(
            F3::from_parts(IBig::from(5), 3)
                .with_precision(0)
                .unwrap()
                .with_base::<6>(),
            Exact(F6::from_parts(IBig::from(135), 0))
        );
    }

    // B=3 → B=9 (power of base: ilog_exact(9,3)=2)
    // 5 × 3^3 = 135 = 15 × 9^1
    {
        type F3 = FBig<HalfAway, 3>;
        type F9 = FBig<HalfAway, 9>;
        assert_eq!(
            F3::from_parts(IBig::from(5), 3)
                .with_precision(0)
                .unwrap()
                .with_base::<9>(),
            Exact(F9::from_parts(IBig::from(15), 1))
        );
    }

    // B=64 → B=8 (power of base: ilog_exact(64,8)=2)
    // 7 × 64^2 = 28672 = 7 × 8^4
    {
        type F64 = FBig<HalfAway, 64>;
        type F8 = FBig<HalfAway, 8>;
        assert_eq!(
            F64::from_parts(IBig::from(7), 2)
                .with_precision(0)
                .unwrap()
                .with_base::<8>(),
            Exact(F8::from_parts(IBig::from(7), 4))
        );
    }
}

#[test]
fn test_precision_change() {
    assert_eq!(FBin::ZERO.with_precision(1), Exact(FBin::ZERO));
    assert_eq!(FBin::ZERO.with_precision(1).unwrap().precision(), 1);

    assert_eq!(fbig!(0x1234).precision(), 16);
    assert_eq!(fbig!(0x1234).with_precision(0), Exact(fbig!(0x1234)));
    assert_eq!(fbig!(0x1234).with_precision(13), Exact(fbig!(0x1234)));
    assert_eq!(fbig!(0x1234).with_precision(8), Inexact(fbig!(0x91p5), NoOp));
    assert_eq!(fbig!(0x1234).with_precision(4), Inexact(fbig!(0x9p9), NoOp));

    assert_eq!(DBig::ONE.with_precision(1), Exact(DBig::ONE));
    assert_eq!(DBig::ONE.with_precision(1).unwrap().precision(), 1);

    assert_eq!(dbig!(1234).precision(), 4);
    assert_eq!(dbig!(1234).with_precision(0), Exact(dbig!(1234)));
    assert_eq!(dbig!(1234).with_precision(4), Exact(dbig!(1234)));
    assert_eq!(dbig!(1234).with_precision(2), Inexact(dbig!(12e2), NoOp));
}

#[test]
fn test_from_unsigned() {
    assert_eq!(FBin::from(0u8), FBin::ZERO);
    assert_eq!(FBin::from(1u8), FBin::ONE);
    assert_eq!(FBin::from(0x10000u32), FBin::from_parts(ibig!(0x10000), 0));
    assert_eq!(FBin::from(0xffffffffu32), FBin::from_parts(ibig!(0xffffffff), 0));
}

#[test]
fn test_to_unsigned() {
    assert_eq!(u8::try_from(FBin::ZERO), Ok(0u8));
    assert_eq!(u8::try_from(FBin::ONE), Ok(1u8));
    assert_eq!(u8::try_from(FBin::ONE << 1), Ok(2u8));
    assert_eq!(u8::try_from(FBin::ONE >> 1), Err(LossOfPrecision));
    assert_eq!(u8::try_from(FBin::NEG_ONE), Err(OutOfBounds));
    assert_eq!(u8::try_from(FBin::from_parts(u8::MAX.into(), 0)), Ok(u8::MAX));
    assert_eq!(u8::try_from(FBin::ONE << 8), Err(OutOfBounds));
    assert_eq!(u128::try_from(FBin::from_parts(u128::MAX.into(), 0)), Ok(u128::MAX));
    assert_eq!(u128::try_from(FBin::ONE << 128), Err(OutOfBounds));
    assert_eq!(u8::try_from(FBin::INFINITY), Err(OutOfBounds));
    assert_eq!(u128::try_from(FBin::NEG_INFINITY), Err(OutOfBounds));
}

#[test]
fn test_from_signed() {
    assert_eq!(FBin::from(0i8), FBin::ZERO);
    assert_eq!(FBin::from(1i8), FBin::ONE);
    assert_eq!(FBin::from(-1i8), FBin::NEG_ONE);
    assert_eq!(FBin::from(-0x10000i32), FBin::from_parts(ibig!(-0x10000), 0));
    assert_eq!(FBin::from(i32::MIN), FBin::from_parts(ibig!(-0x80000000), 0));
}

#[test]
fn test_to_signed() {
    assert_eq!(i8::try_from(FBin::ZERO), Ok(0i8));
    assert_eq!(i8::try_from(FBin::ONE), Ok(1i8));
    assert_eq!(i8::try_from(FBin::NEG_ONE << 1), Ok(-2i8));
    assert_eq!(i8::try_from(FBin::ONE >> 1), Err(LossOfPrecision));
    assert_eq!(i8::try_from(FBin::from_parts(i8::MAX.into(), 0)), Ok(i8::MAX));
    assert_eq!(i8::try_from(FBin::ONE << 7), Err(OutOfBounds));
    assert_eq!(i128::try_from(FBin::from_parts(i128::MAX.into(), 0)), Ok(i128::MAX));
    assert_eq!(i128::try_from(FBin::from_parts(i128::MIN.into(), 0)), Ok(i128::MIN));
    assert_eq!(i128::try_from(FBin::ONE << 127), Err(OutOfBounds));
    assert_eq!(i8::try_from(FBin::INFINITY), Err(OutOfBounds));
    assert_eq!(i128::try_from(FBin::NEG_INFINITY), Err(OutOfBounds));
}

#[test]
#[rustfmt::skip::macros(fbig)]
fn test_from_f32() {
    assert_eq!(FBin::try_from(0f32).unwrap(), fbig!(0x0));
    assert_eq!(FBin::try_from(-0f32).unwrap(), fbig!(0x0));
    assert_eq!(FBin::try_from(1f32).unwrap(), fbig!(0x1));
    assert_eq!(FBin::try_from(-1f32).unwrap(), fbig!(-0x1));
    assert_eq!(FBin::try_from(-1f32).unwrap().precision(), 24);
    assert_eq!(FBin::try_from(1234f32).unwrap(), fbig!(0x4d2));
    assert_eq!(FBin::try_from(-1234f32).unwrap(), fbig!(-0x4d2));
    assert_eq!(12.34f32.to_bits(), 0x414570a4); // exact value: 12.340000152587890625
    assert_eq!(FBin::try_from(12.34f32).unwrap(), fbig!(0x315c29p-18));
    assert_eq!(FBin::try_from(-12.34f32).unwrap(), fbig!(-0x315c29p-18));
    assert_eq!(1e-40_f32.to_bits(), 0x000116c2); // subnormal
    assert_eq!(FBin::try_from(1e-40_f32).unwrap(), fbig!(0x116c2p-149));
    assert_eq!(FBin::try_from(-1e-40_f32).unwrap(), fbig!(-0x116c2p-149));
    assert_eq!(FBin::try_from(-1e-40_f32).unwrap().precision(), 17);
    assert_eq!(FBin::try_from(f32::INFINITY).unwrap(), FBin::INFINITY);
    assert_eq!(FBin::try_from(f32::NEG_INFINITY).unwrap(), FBin::NEG_INFINITY);
    assert!(FBin::try_from(f32::NAN).is_err());
}

#[test]
#[rustfmt::skip::macros(fbig)]
fn test_from_f64() {
    assert_eq!(FBin::try_from(0f64).unwrap(), fbig!(0x0));
    assert_eq!(FBin::try_from(-0f64).unwrap(), fbig!(0x0));
    assert_eq!(FBin::try_from(1f64).unwrap(), fbig!(0x1));
    assert_eq!(FBin::try_from(-1f64).unwrap(), fbig!(-0x1));
    assert_eq!(FBin::try_from(-1f64).unwrap().precision(), 53);
    assert_eq!(FBin::try_from(1234f64).unwrap(), fbig!(0x4d2));
    assert_eq!(FBin::try_from(-1234f64).unwrap(), fbig!(-0x4d2));
    assert_eq!(12.34f64.to_bits(), 0x4028ae147ae147ae); // exact value: 12.339999999999999857891452847979962825775146484375
    assert_eq!(FBin::try_from(12.34f64).unwrap(), fbig!(0xc570a3d70a3d7p-48));
    assert_eq!(FBin::try_from(-12.34f64).unwrap(), fbig!(-0xc570a3d70a3d7p-48));
    assert_eq!(1e-308_f64.to_bits(), 0x000730d67819e8d2); // subnormal
    assert_eq!(FBin::try_from(1e-308_f64).unwrap(), fbig!(0x730d67819e8d2p-1074));
    assert_eq!(FBin::try_from(-1e-308_f64).unwrap(), fbig!(-0x730d67819e8d2p-1074));
    assert_eq!(FBin::try_from(-1e-308_f64).unwrap().precision(), 51);
    assert_eq!(FBin::try_from(f64::INFINITY).unwrap(), FBin::INFINITY);
    assert_eq!(FBin::try_from(f64::NEG_INFINITY).unwrap(), FBin::NEG_INFINITY);
    assert!(FBin::try_from(f64::NAN).is_err());
}

#[test]
#[rustfmt::skip::macros(fbig)]
fn test_to_int() {
    assert_eq!(fbig!(0x0).to_int(), Exact(ibig!(0)));
    assert_eq!(fbig!(0x1).to_int(), Exact(ibig!(1)));
    assert_eq!(fbig!(-0x1).to_int(), Exact(ibig!(-1)));
    assert_eq!(fbig!(0x1234).to_int(), Exact(ibig!(0x1234)));
    assert_eq!(fbig!(0x1234p-4).to_int(), Inexact(ibig!(0x123), NoOp));
    assert_eq!(fbig!(-0x1234p-8).to_int(), Inexact(ibig!(-0x12), NoOp));
    assert_eq!(fbig!(0x1234p-16).to_int(), Inexact(ibig!(0), NoOp));
    assert_eq!(fbig!(0x1234p4).to_int(), Exact(ibig!(0x1234) << 4));
    assert_eq!(fbig!(-0x1234p8).to_int(), Exact(ibig!(-0x1234) << 8));

    assert_eq!(dbig!(0).to_int(), Exact(ibig!(0)));
    assert_eq!(dbig!(1).to_int(), Exact(ibig!(1)));
    assert_eq!(dbig!(-1).to_int(), Exact(ibig!(-1)));
    assert_eq!(dbig!(1234).to_int(), Exact(ibig!(1234)));
    assert_eq!(dbig!(1234e-1).to_int(), Inexact(ibig!(123), NoOp));
    assert_eq!(dbig!(-1234e-2).to_int(), Inexact(ibig!(-12), NoOp));
    assert_eq!(dbig!(1234e-4).to_int(), Inexact(ibig!(0), NoOp));
    assert_eq!(dbig!(1234e1).to_int(), Exact(ibig!(12340)));
    assert_eq!(dbig!(-1234e2).to_int(), Exact(ibig!(-123400)));
    assert_eq!(dbig!(255e-1).to_int(), Inexact(ibig!(26), AddOne));
    assert_eq!(dbig!(-255e-1).to_int(), Inexact(ibig!(-26), SubOne));
}

#[test]
#[should_panic]
fn test_inf_to_int() {
    let _ = DBig::INFINITY.to_int();
}

#[test]
#[rustfmt::skip::macros(fbig)]
fn test_to_f32() {
    let fbig_cases = [
        // fbig value, f32 value, conversion error
        (fbig!(0x0), Exact(0.), None),
        (fbig!(0x1), Exact(1.), None),
        (fbig!(-0x1), Exact(-1.), None),
        (fbig!(-0x1234), Exact(-4660.), None),
        (fbig!(0x1234p-3), Exact(582.5), None),
        (fbig!(0x1234p-16), Exact(0.07110596), None),
        // exact value: 3.8549410571968246689670642581279215812574412414193147924379... × 10^-21
        (fbig!(0x123456789p-100), Inexact(3.8549407e-21, NoOp), Some(LossOfPrecision)),
        (fbig!(-0x987654321p50), Inexact(-4.607888e25, NoOp), Some(LossOfPrecision)),
    ];
    for (big, small, reason) in fbig_cases {
        assert_eq!(big.to_f32(), small);
        if let Some(reason) = reason {
            assert_eq!(f32::try_from(big), Err(reason));
        } else {
            assert_eq!(f32::try_from(big), Ok(small.value()));
        }
    }

    // some Repr cases, all round to even
    assert_eq!(fbig!(0x123456789p-100).repr().to_f32(), Inexact(3.854941e-21, AddOne));

    // boundary cases
    assert_eq!(FBig::<Zero, 2>::INFINITY.to_f32(), Inexact(f32::INFINITY, NoOp));
    assert_eq!(FBig::<Zero, 2>::NEG_INFINITY.to_f32(), Inexact(f32::NEG_INFINITY, NoOp));
    assert_eq!(f32::try_from(FBig::<Zero, 2>::INFINITY), Err(LossOfPrecision));
    assert_eq!(f32::try_from(FBig::<Zero, 2>::NEG_INFINITY), Err(LossOfPrecision));
    assert_eq!(fbig!(0x1p200).to_f32(), Inexact(f32::INFINITY, AddOne)); // overflow
    assert_eq!(fbig!(-0x1p200).to_f32(), Inexact(f32::NEG_INFINITY, SubOne));
    assert_eq!(f32::try_from(fbig!(0x1p200)), Err(OutOfBounds));
    assert_eq!(f32::try_from(fbig!(-0x1p200)), Err(OutOfBounds));
    assert_eq!(fbig!(0x1p128).to_f32(), Inexact(f32::INFINITY, AddOne)); // boundary for overflow
    assert_eq!(fbig!(-0x1p128).to_f32(), Inexact(f32::NEG_INFINITY, SubOne));
    assert_eq!(f32::try_from(fbig!(0x1p128)), Err(OutOfBounds));
    assert_eq!(f32::try_from(fbig!(-0x1p128)), Err(OutOfBounds));
    assert_eq!(fbig!(0xffffffffp96).to_f32(), Inexact(f32::MAX, NoOp));
    assert_eq!(fbig!(0xffffffffp96).repr().to_f32(), Inexact(f32::INFINITY, AddOne));
    assert_eq!(f32::try_from(fbig!(0xffffffffp96)), Err(LossOfPrecision));
    assert_eq!(f32::try_from(fbig!(0xffffffffp96).into_repr()), Err(OutOfBounds));
    assert_eq!(fbig!(-0xffffffffp96).to_f32(), Inexact(f32::MIN, NoOp));
    assert_eq!(fbig!(-0xffffffffp96).repr().to_f32(), Inexact(f32::NEG_INFINITY, SubOne));
    assert_eq!(f32::try_from(fbig!(0xffffffffp96)), Err(LossOfPrecision));
    assert_eq!(f32::try_from(fbig!(0xffffffffp96).into_repr()), Err(OutOfBounds));
    assert_eq!(fbig!(0x1p-140).to_f32(), Exact(f32::from_bits(0x200))); // subnormal
    assert_eq!(fbig!(-0x1p-140).to_f32(), Exact(-f32::from_bits(0x200)));
    assert_eq!(fbig!(0x1p-149).to_f32(), Exact(f32::from_bits(0x1)));
    assert_eq!(fbig!(-0x1p-149).to_f32(), Exact(-f32::from_bits(0x1)));
    assert_eq!(f32::try_from(fbig!(0x1p-149)).unwrap(), f32::from_bits(0x1));
    assert_eq!(f32::try_from(fbig!(-0x1p-149)).unwrap(), -f32::from_bits(0x1));
    assert_eq!(fbig!(0x1p-150).to_f32(), Inexact(0f32, NoOp)); // boundary for underflow
    assert_eq!(fbig!(-0x1p-150).to_f32(), Inexact(-0f32, NoOp));
    assert_eq!(f32::try_from(fbig!(0x1p-150)), Err(LossOfPrecision));
    assert_eq!(f32::try_from(fbig!(-0x1p-150)), Err(LossOfPrecision));
    assert_eq!(fbig!(0xffffffffp-182).to_f32(), Inexact(0f32, NoOp));
    assert_eq!(fbig!(-0xffffffffp-182).to_f32(), Inexact(-0f32, NoOp));
}

#[test]
#[rustfmt::skip::macros(fbig)]
fn test_to_f64() {
    let fbig_cases = [
        // fbig value, f64 value, conversion error
        (fbig!(0x0), Exact(0.), None),
        (fbig!(0x1), Exact(1.), None),
        (fbig!(-0x1), Exact(-1.), None),
        (fbig!(-0x1234), Exact(-4660.), None),
        (fbig!(0x1234p-3), Exact(582.5), None),
        (fbig!(0x1234p-16), Exact(0.07110595703125), None),
        (fbig!(0x123456789p-100), Exact(3.854941057196825e-21), None),
        (fbig!(-0x987654321p50), Exact(-4.607887924007194e25), None),
        // exact value: 3.3436283752161326232549204599099774676691163414240905497490... × 10^-39
        (
            fbig!(0x1234567890123456789p-200),
            Inexact(3.3436283752161324e-39, NoOp),
            Some(LossOfPrecision),
        ),
        // exact value: 72310453210697978489701299687443815627510656356042859969687735028883143242326999040
        (
            fbig!(-0x9876543210987654321p200),
            Inexact(-7.231045321069797e82, NoOp),
            Some(LossOfPrecision),
        ),
    ];
    for (big, small, reason) in fbig_cases {
        assert_eq!(big.to_f64(), small);
        if let Some(reason) = reason {
            assert_eq!(f64::try_from(big), Err(reason));
        } else {
            assert_eq!(f64::try_from(big), Ok(small.value()));
        }
    }

    assert_eq!(FBig::<Zero, 2>::INFINITY.to_f64(), Inexact(f64::INFINITY, NoOp));
    assert_eq!(FBig::<Zero, 2>::NEG_INFINITY.to_f64(), Inexact(f64::NEG_INFINITY, NoOp));
    assert_eq!(f64::try_from(FBig::<Zero, 2>::INFINITY), Err(LossOfPrecision));
    assert_eq!(f64::try_from(FBig::<Zero, 2>::NEG_INFINITY), Err(LossOfPrecision));
    assert_eq!(fbig!(0x1p2000).to_f64(), Inexact(f64::INFINITY, AddOne)); // overflow
    assert_eq!(fbig!(-0x1p2000).to_f64(), Inexact(f64::NEG_INFINITY, SubOne));
    assert_eq!(f64::try_from(fbig!(0x1p2000)), Err(OutOfBounds));
    assert_eq!(f64::try_from(fbig!(-0x1p2000)), Err(OutOfBounds));
    assert_eq!(fbig!(0x1p1024).to_f64(), Inexact(f64::INFINITY, AddOne)); // boundary for overflow
    assert_eq!(fbig!(-0x1p1024).to_f64(), Inexact(f64::NEG_INFINITY, SubOne));
    assert_eq!(f64::try_from(fbig!(0x1p1024)), Err(OutOfBounds));
    assert_eq!(f64::try_from(fbig!(-0x1p1024)), Err(OutOfBounds));
    assert_eq!(fbig!(0xffffffffffffffffp960).to_f64(), Inexact(f64::MAX, NoOp));
    assert_eq!(fbig!(-0xffffffffffffffffp960).to_f64(), Inexact(-f64::MAX, NoOp));
    assert_eq!(fbig!(0x1p-1060).to_f64(), Exact(f64::from_bits(0x4000))); // subnormal
    assert_eq!(fbig!(-0x1p-1060).to_f64(), Exact(-f64::from_bits(0x4000)));
    assert_eq!(fbig!(0x1p-1074).to_f64(), Exact(f64::from_bits(0x1)));
    assert_eq!(fbig!(-0x1p-1074).to_f64(), Exact(-f64::from_bits(0x1)));
    assert_eq!(fbig!(0x1p-1075).to_f64(), Inexact(0f64, NoOp)); // boundary for underflow
    assert_eq!(fbig!(-0x1p-1075).to_f64(), Inexact(-0f64, NoOp));
    assert_eq!(f64::try_from(fbig!(0x1p-1075)), Err(LossOfPrecision));
    assert_eq!(f64::try_from(fbig!(-0x1p-1075)), Err(LossOfPrecision));
    assert_eq!(fbig!(0xffffffffffffffffp-1139).to_f64(), Inexact(0f64, NoOp));
    assert_eq!(fbig!(-0xffffffffffffffffp-1139).to_f64(), Inexact(-0f64, NoOp));
    assert_eq!(f64::try_from(fbig!(0xffffffffffffffffp-1139)), Err(LossOfPrecision));
    assert_eq!(f64::try_from(fbig!(-0xffffffffffffffffp-1139)), Err(LossOfPrecision));
}

#[test]
fn test_from_ibig() {
    assert_eq!(FBin::from(ibig!(0)), fbig!(0));
    assert_eq!(FBin::from(ibig!(1)), fbig!(1));
    assert_eq!(FBin::from(ibig!(-1)), fbig!(-1));
    assert_eq!(FBin::from(ibig!(-0x1234)), fbig!(-0x1234));

    // digits test
    assert_eq!(FBin::from(ibig!(-0x1234)).precision(), 13);
    assert_eq!(FBin::from_parts(ibig!(0x1234), 12).precision(), 13);
    assert_eq!(DBig::from(ibig!(-1230)).precision(), 4);
    assert_eq!(DBig::from_parts(ibig!(0x1234), 12).precision(), 4);
    assert_eq!(FHex::from(ibig!(-0x1230)).precision(), 4);
    assert_eq!(FHex::from_parts(ibig!(0x1230), 12).precision(), 4);

    // use addition to test the digits (#28)
    assert_eq!(DBig::from(ubig!(10)) + DBig::from(ubig!(5)), DBig::from(ubig!(15)));
}

#[test]
fn test_dbig_to_f64() {
    // Regression test: DBig with significand exceeding 53 bits converting to f64

    // Small values that fit exactly in f64
    assert_eq!(dbig!(0).to_f64(), Exact(0.0));
    assert_eq!(dbig!(1).to_f64(), Exact(1.0));
    assert_eq!(dbig!(-1).to_f64(), Exact(-1.0));
    assert_eq!(dbig!(12345).to_f64(), Exact(12345.0));
    assert_eq!(dbig!(5e-1).to_f64(), Exact(0.5)); // 0.5 is exact in binary
    assert_eq!(dbig!(1e20).to_f64(), Exact(1e20)); // 5^20 is roughly 46 bits, so it's exact in f64

    // Values requiring rounding (more than 53 bits of precision in base 10)
    assert_eq!(
        dbig!(9007199254740993).to_f64(), // 2^53 + 1: tie, HalfAway rounds away from zero
        Inexact(9007199254740994.0, AddOne)
    );
    assert_eq!(dbig!(88888888888888888).to_f64(), Inexact(88888888888888888.0, AddOne));

    // Infinity
    assert_eq!(DBig::INFINITY.to_f64(), Inexact(f64::INFINITY, NoOp));
    assert_eq!(DBig::NEG_INFINITY.to_f64(), Inexact(f64::NEG_INFINITY, NoOp));
}

#[test]
fn test_dbig_to_f32() {
    assert_eq!(dbig!(0).to_f32(), Exact(0.0f32));
    assert_eq!(dbig!(1).to_f32(), Exact(1.0f32));
    assert_eq!(dbig!(-1).to_f32(), Exact(-1.0f32));

    // 1e10 fits exactly in f32 (24 mantissa bits, exponent fits)
    assert_eq!(dbig!(1e10).to_f32(), Exact(1e10f32));

    // 1e20 requires more than 24 bits of precision in binary
    assert_eq!(dbig!(1e20).to_f32(), Inexact(1e20f32, AddOne));

    // Values requiring rounding (more than 24 bits of precision in base 10)
    assert_eq!(dbig!(16777217).to_f32(), Inexact(16777218.0f32, AddOne)); // 2^24 + 1: tie, HalfAway rounds away from zero
    assert_eq!(dbig!(88888888888888888).to_f32(), Inexact(88888888888888888.0f32, AddOne));

    // Infinity
    assert_eq!(DBig::INFINITY.to_f32(), Inexact(f32::INFINITY, NoOp));
    assert_eq!(DBig::NEG_INFINITY.to_f32(), Inexact(f32::NEG_INFINITY, NoOp));
}