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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
//! This file implements multi-precision rational numbers.
use crate::bigint::{BigInt, RatError, RatErrKind, Nat};
use crate::bigint::bigint::BISign::{Natural, Negative};
use crate::bigint::bigint::BISign;
use crate::bigint::util;
use std::ops::{Neg, Add, Sub, AddAssign, SubAssign, Mul, MulAssign, Div, DivAssign};
use std::cmp::Ordering;
use std::str::FromStr;
use std::fmt::{Debug, Formatter, Display, Binary, Octal, LowerHex, UpperHex};
/// A Rat represents a quotient a/b of arbitrary precision.
/// The zero value for a Rat represents the value 0.
///
/// Operations always take pointer arguments (*Rat) rather
/// than Rat values, and each unique Rat value requires
/// its own unique *Rat pointer. To "copy" a Rat value,
/// an existing (or newly allocated) Rat must be set to
/// a new value using the Rat.Set method; shallow copies
/// of Rats are not supported and may lead to errors.
#[derive(Clone)]
pub struct Rat {
// To make zero values for Rat work w/o initialization,
// a zero value of b (len(b) == 0) acts like b == 1. At
// the earliest opportunity (when an assignment to the Rat
// is made), such uninitialized denominators are set to 1.
// a.neg determines the sign of the Rat, b.neg is ignored.
a: BigInt,
b: BigInt,
}
impl Rat {
pub fn is_nan(&self) -> bool {
self.a.is_nan() || self.b.is_nan()
}
pub fn deep_clone(&self) -> Rat {
Rat {
a: self.a.deep_clone(),
b: self.b.deep_clone(),
}
}
pub fn from_frac(numerator: isize, denominator: isize) -> Result<Rat, RatError> {
if denominator == 0 {
Err(RatError::new(RatErrKind::DenominatorIsZero, ""))
} else {
let mut z = Rat {
a: if denominator < 0 {BigInt::from(-numerator)} else {BigInt::from(numerator)},
b: BigInt::from(denominator.abs()),
};
z.norm();
Ok(z)
}
}
pub fn from_frac_bigint(numerator: &BigInt, denominator: &BigInt) -> Result<Rat, RatError> {
if denominator.nat == 0u32 {
Err(RatError::new(RatErrKind::DenominatorIsZero, ""))
} else {
let mut z = Rat {
a: if denominator.sign == Negative {BigInt::from(-numerator.clone())} else {BigInt::from(numerator.deep_clone())},
b: BigInt::from(denominator.abs()),
};
z.norm();
Ok(z)
}
}
pub fn from_f64(f: f64) -> Rat {
let exp_mask = (1<<11) - 1;
let bits = f.to_bits();
let mut mantissa = bits & ((1 << 52) - 1);
let mut exp = ((bits >> 52) as i64) & exp_mask;
if exp == exp_mask {
// non-finite
Rat {
a: BigInt::nan(),
b: BigInt::from(1u32),
}
} else {
exp -= if exp == 0 {
// denormal
1022
} else {
// normal
mantissa |= 1 << 52;
1023
};
let mut shift = 52 - exp;
while (mantissa & 1) == 0 && shift > 0 {
mantissa >>= 1;
shift -= 1;
}
let mut a= BigInt::from(mantissa);
a.sign = BISign::from(f < 0f64);
let mut b = BigInt::from(1u32);
let s = if shift > 0 {shift as usize} else {(-shift) as usize};
b.nat.shl_inner(&s);
let mut z = Rat {a, b};
z.norm();
z
}
}
/// This method convert the `self` to the nearest `f32` value and a bool indicating
/// whether the result represents `self` exactly. If the magnitude of `self` is too large to
/// be represented by a `f32`, the result is an infinity and the bool is false.
/// The sign of result always matches the sign of `self`, even if `self == 0`.
pub fn to_f32(&self) -> (f32, bool) {
let (f, exact) = if self.is_nan() {
return (f32::NAN, false);
} else if self.b.nat == 0u32 {
Self::quo_to_f32(self.a.nat.clone(), Nat::from(1u32))
} else {
Self::quo_to_f32(self.a.nat.clone(), self.b.nat.clone())
};
if self.a.sign == Negative {
(-f, exact)
} else {
(f, exact)
}
}
/// This method convert the `self` to the nearest `f64` value and a bool indicating
/// whether the result represents `self` exactly. If the magnitude of `self` is too large to
/// be represented by a `f64`, the result is an infinity and the bool is false.
/// The sign of result always matches the sign of `self`, even if `self == 0`.
pub fn to_f64(&self) -> (f64, bool) {
let (f, exact) = if self.is_nan() {
return (f64::NAN, false);
} else if self.b.nat == 0u32 {
Self::quo_to_f64(self.a.nat.clone(), Nat::from(1u32))
} else {
Self::quo_to_f64(self.a.nat.clone(), self.b.nat.clone())
};
if self.a.sign == Negative {
(-f, exact)
} else {
(f, exact)
}
}
/// quotToFloat32 returns the non-negative float32 value
/// nearest to the quotient a/b, using round-to-even in
/// halfway cases. It does not mutate its arguments.
/// Preconditions: b is non-zero; a and b have no common factors.
fn quo_to_f32(a: Nat, b: Nat) -> (f32, bool) {
debug_assert!(!a.is_nan() && !b.is_nan());
debug_assert_ne!(b, 0u32);
let (fsize, msize) = (32, 23);
let (msize1, msize2) = (msize + 1, msize + 2);
let esize = fsize - msize1;
let ebias = (1 << (esize - 1)) - 1;
let (emin, _emax) = (1 - ebias, ebias);
if a == 0u32 {
(0f32, true);
}
let (alen, blen) = (a.bits_len() as isize, b.bits_len() as isize);
// 1. Left-shift A or B such that quotient A/B is in [1<<Msize1, 1<<(Msize2+1)
// (Msize2 bits if A < B when they are left-aligned, Msize2+1 bits if A >= B).
// This is 2 or 3 more than the float32 mantissa field width of Msize:
// - the optional extra bit is shifted away in step 3 below.
// - the high-order 1 is omitted in "normal" representation;
// - the low-order 1 will be used during rounding then discarded.
let mut exp = alen - blen;
let (shift, is_great) = if msize2 > exp {((msize2 - exp) as usize, true)} else {((exp - msize2) as usize, false)};
let (mut a2, mut b2) = (a.deep_clone(), b.deep_clone());
if is_great {a2.shl_inner(&shift);} else {b2.shl_inner(&shift);}
// 2. Compute quotient and remainder (q, r). NB: due to the
// extra shift, the low-order bit of q is logically the
// high-order bit of r.
// mantissa&1 && !haveRem => remainder is exactly half
let (q, r) = (a2.clone() / b2.clone(), a2.clone() % b2.clone());
let (mut mantissa, mut is_have_rem) = (q.as_vec()[0], r > 0u32); // mantissa&1 && !haveRem => remainder is exactly half
// 3. If quotient didn't fit in Msize2 bits, redo division by b2<<1
// (in effect---we accomplish this incrementally).
if (mantissa >> msize2) == 1 {
if mantissa & 1 == 1 {
is_have_rem = true;
}
mantissa >>= 1;
exp += 1;
}
if (mantissa >> msize1) != 1 {
panic!("the numerator of {:#x} and the denominator of {:#x} have the common factor that great than 1", a, b);
}
// 4. Rounding.
if (emin - msize) <= exp && exp <= emin {
// Denormal case; lose 'shift' bits of precision.
let shift = (emin - (exp - 1)) as usize; // [1..Esize1)
let lostbits = mantissa & ((1 << shift) - 1);
is_have_rem = is_have_rem || lostbits != 0;
mantissa >>= shift;
exp = 2 - ebias; // == exp + shift
}
// Round q using round-half-to-even.
let mut exact = !is_have_rem;
if (mantissa & 1) != 0 {
exact = false;
if is_have_rem || (mantissa & 2) != 0 {
mantissa += 1;
if mantissa >= (1 << msize2) {
// Complete rollover 11...1 => 100...0, so shift is safe
mantissa >>= 1;
exp += 1;
}
}
}
mantissa >>= 1; // discard rounding bit. Mantissa now scaled by 1<<Msize1.
let f = util::ldexp(mantissa as f64, exp - msize1) as f32;
if f.is_infinite() {
(f, false)
} else {
(f, exact)
}
}
/// quotToFloat64 returns the non-negative float64 value
/// nearest to the quotient a/b, using round-to-even in
/// halfway cases. It does not mutate its arguments.
/// Preconditions: b is non-zero; a and b have no common factors.
fn quo_to_f64(a: Nat, b: Nat) -> (f64, bool) {
debug_assert!(!a.is_nan() && !b.is_nan());
debug_assert_ne!(b, 0u32);
let (fsize, msize) = (64, 52);
let (msize1, msize2) = (msize + 1, msize + 2);
let esize = fsize - msize1;
let ebias = (1 << (esize - 1)) - 1;
let (emin, _emax) = (1 - ebias, ebias);
if a == 0u32 {
return (0f64, true);
}
let (alen, blen) = (a.bits_len() as isize, b.bits_len() as isize);
// 1. Left-shift A or B such that quotient A/B is in [1<<Msize1, 1<<(Msize2+1)
// (Msize2 bits if A < B when they are left-aligned, Msize2+1 bits if A >= B).
// This is 2 or 3 more than the float64 mantissa field width of Msize:
// - the optional extra bit is shifted away in step 3 below.
// - the high-order 1 is omitted in "normal" representation;
// - the low-order 1 will be used during rounding then discarded.
let mut exp = alen - blen;
let (shift, is_great) = if msize2 > exp {((msize2 - exp) as usize, true)} else {((exp - msize2) as usize, false)};
let (mut a2, mut b2) = (a.deep_clone(), b.deep_clone());
if is_great {a2.shl_inner(&shift);} else {b2.shl_inner(&shift);}
// 2. Compute quotient and remainder (q, r). NB: due to the
// extra shift, the low-order bit of q is logically the
// high-order bit of r.
let (q, r) = (a2.clone() / b2.clone(), a2.clone() % b2.clone());
let (mut mantissa, mut is_have_rem) = if q.as_vec().len() > 1 {
((q.as_vec()[0] as u64) | ((q.as_vec()[1] as u64) << 32), r > 0u32)
} else {
(q.as_vec()[0] as u64, r > 0u32)
}; // mantissa&1 && !haveRem => remainder is exactly half
// 3. If quotient didn't fit in Msize2 bits, redo division by b2<<1
// (in effect---we accomplish this incrementally).
if (mantissa >> msize2) == 1 {
if (mantissa & 1) == 1 {
is_have_rem = true;
}
mantissa >>= 1;
exp += 1;
}
if (mantissa >> msize1) != 1 {
panic!("the numerator of {:#x} and the denominator of {:#x} have the common factor that great than 1", a, b);
}
// 4. Rounding.
if (emin - msize) <= exp && exp <= emin {
// Denormal case; lose 'shift' bits of precision.
let shift = (emin - (exp - 1)) as usize; // [1..Esize1)
let lostbits = mantissa & ((1 << shift) - 1);
is_have_rem = is_have_rem || lostbits != 0;
mantissa >>= shift;
exp = 2 - ebias; // == exp + shift
}
// Round q using round-half-to-even.
let mut exact = !is_have_rem;
if (mantissa & 1) != 0 {
exact = false;
if is_have_rem || (mantissa & 2) != 0 {
mantissa += 1;
if mantissa >= (1 << msize2) {
// Complete rollover 11...1 => 100...0, so shift is safe
mantissa >>= 1;
exp += 1;
}
}
}
mantissa >>= 1; // discard rounding bit. Mantissa now scaled by 1<<Msize1.
let f = util::ldexp(mantissa as f64, exp - msize1);
if f.is_infinite() {
(f, false)
} else {
(f, exact)
}
}
fn norm(&mut self) {
let mut t = true;
if self.a.is_nan() || self.a.nat == 0u32 {
self.a.sign = Natural;
t = false;
}
if self.b.is_nan() || self.b.nat == 0u32 {
self.b.nat.clear();
self.b.nat.as_mut_vec().push(1);
t = false;
}
if t {
let neg = self.a.sign;
self.a.sign = Natural;
self.b.sign = Natural;
let (d, _x, _y) = self.a.gcd(self.b.clone());
if d.nat != 1u32 {
self.a.nat.div_inner(&d.nat);
self.b.nat.div_inner(&d.nat);
}
self.a.sign = neg;
}
}
pub fn abs(&self) -> Rat {
let mut z = self.deep_clone();
z.a.sign = Natural;
z
}
pub(super) fn nan() -> Rat {
Rat {
a: BigInt::nan(),
b: BigInt::from(1u32),
}
}
pub fn inv(&self) -> Result<Rat, RatError> {
if self.is_nan() {
Ok(Self::nan())
} else if self.a.nat == 0u32 {
Err(RatError::new(RatErrKind::NumeratorIsZero, "It cannot to inverse zero"))
} else {
let (mut a, mut b) = (self.b.deep_clone(), self.a.deep_clone());
a.sign = self.a.sign;
b.sign = Natural;
Ok(
Rat {
a,
b,
}
)
}
}
/// Sign returns:
///
/// Some(-1) if self < 0
/// Some(0) if self == 0
/// Some(+1) if self > 0
/// None if self is not a number
///
pub fn signnum(&self) -> Option<isize> {
self.a.signnum()
}
/// whether is a integer number, `None` means `self` is not a number
pub fn is_integer(&self) -> Option<bool> {
if self.is_nan() {
None
} else {
Some(self.a.nat == 0u32 || self.b == 1u32)
}
}
pub fn numerator(&self) -> &BigInt {
&self.a
}
pub fn denominator(&self) -> &BigInt {
&self.b
}
fn clear(&mut self) {
self.a.nat.clear();
}
fn mul_denom(x: &Nat, y: &Nat) -> Nat {
if x == &0u32 && y == &0u32 {
Nat::from(1u32)
} else if x == &0u32 {
y.deep_clone()
} else if y == &0u32 {
x.deep_clone()
} else {
x.clone() * y.clone()
}
}
fn scale_denom(x: &BigInt, f: &Nat) -> BigInt {
if f == &0u32 {
x.deep_clone()
} else {
let n = x.nat.clone() * f.clone();
let mut z = BigInt::from(n);
z.sign = x.sign;
z
}
}
fn cmp_inner(&self, other: &Rat) -> Option<isize> {
if self.is_nan() || other.is_nan() {None}
else {
let a = Self::scale_denom(&self.a, &other.b.nat);
let b = Self::scale_denom(&other.a, &self.b.nat);
Some(if a == b {0} else if a < b {-1} else {1})
}
}
/// self and other must not be a Nan
fn add_inner(&self, other: &Rat) -> Rat {
let (mut a1, a2) = (
Self::scale_denom(&self.a, &other.b.nat),
Self::scale_denom(&other.a, &self.b.nat),
);
a1 += a2;
let b = Self::mul_denom(&self.b.nat, &other.b.nat);
let mut z = Rat {a: a1, b: BigInt {nat: b, sign: Natural}};
z.norm();
z
}
/// self and other must not be a Nan
fn sub_inner(&self, other: &Rat) -> Rat {
let (mut a1, a2) = (
Self::scale_denom(&self.a, &other.b.nat),
Self::scale_denom(&other.a, &self.b.nat),
);
a1 -= a2;
let b = Self::mul_denom(&self.b.nat, &other.b.nat);
let mut z = Rat {a: a1, b: BigInt {nat: b, sign: Natural}};
z.norm();
z
}
fn mul_inner(&self, other: &Rat) -> Rat {
let mut z = if self == other {
let a = self.a.sqr();
let b = if self.b.nat == 0u32 {
BigInt::from(1u32)
} else {
self.b.sqr()
};
Rat {
a,
b,
}
} else {
let a = self.a.clone() * other.a.clone();
let mut b = BigInt::from(Self::mul_denom(&self.b.nat, &other.b.nat));
b.sign = Natural;
Rat {
a,
b,
}
};
z.norm();
z
}
fn div_inner(&self, other: &Rat) -> Rat {
if other.a.nat == 0u32 {
panic!("division by zero");
} else {
let (mut a, b) = (
Self::scale_denom(&self.a, &other.b.nat),
Self::scale_denom(&other.a, &self.b.nat),
);
a.sign = BISign::from(a.sign != b.sign);
let mut z = Rat {
a,
b,
};
z.norm();
z
}
}
}
impl Div for Rat {
type Output = Rat;
fn div(self, rhs: Self) -> Self::Output {
if self.is_nan() || rhs.is_nan() {
Rat::nan()
} else {
self.div_inner(&rhs)
}
}
}
impl DivAssign for Rat {
fn div_assign(&mut self, rhs: Self) {
if self.is_nan() || rhs.is_nan() {
self.clear();
} else {
let z = self.div_inner(&rhs);
*self = z;
}
}
}
impl Mul for Rat {
type Output = Rat;
fn mul(self, rhs: Self) -> Self::Output {
if self.is_nan() || rhs.is_nan() {
Rat::nan()
} else {
self.mul_inner(&rhs)
}
}
}
impl MulAssign for Rat {
fn mul_assign(&mut self, rhs: Self) {
if self.is_nan() {
self.clear();
} else {
let z = self.mul_inner(&rhs);
*self = z;
}
}
}
impl Sub for Rat {
type Output = Rat;
fn sub(self, rhs: Self) -> Self::Output {
if self.is_nan() || rhs.is_nan() {Rat::nan()}
else {self.sub_inner(&rhs)}
}
}
impl Add for Rat {
type Output = Rat;
fn add(self, rhs: Self) -> Self::Output {
if self.is_nan() || rhs.is_nan() {Rat::nan()}
else {self.add_inner(&rhs)}
}
}
impl SubAssign for Rat {
fn sub_assign(&mut self, rhs: Self) {
if self.is_nan() || rhs.is_nan() {
self.clear();
} else {
let z = self.sub_inner(&rhs);
*self = z;
}
}
}
impl AddAssign for Rat {
fn add_assign(&mut self, rhs: Self) {
if self.is_nan() || rhs.is_nan() {
self.clear();
} else {
let z = self.add_inner(&rhs);
*self = z;
}
}
}
impl PartialEq for Rat {
fn eq(&self, other: &Self) -> bool {
match self.cmp_inner(other) {
Some(0) => true,
_ => false,
}
}
}
impl PartialOrd for Rat {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.cmp_inner(other).map(|e| {
if e == 0 {Ordering::Equal}
else if e > 0 {Ordering::Greater}
else {Ordering::Less}
})
}
}
impl Neg for Rat {
type Output = Rat;
fn neg(self) -> Self::Output {
if self.is_nan() {
Self::nan()
} else {
Rat {
a: -self.a,
b: self.b.deep_clone(),
}
}
}
}
impl From<f64> for Rat {
fn from(f: f64) -> Self {
Self::from_f64(f)
}
}
impl From<f32> for Rat {
fn from(f: f32) -> Self {
Self::from_f64(f as f64)
}
}
macro_rules! rat_impl_from_basic {
($T: ty) => {
impl From<$T> for Rat {
fn from(n: $T) -> Self {
Rat {
a: BigInt::from(n),
b: BigInt::from(1u32),
}
}
}
};
($T0: ty, $($T1: ty),+) => {
rat_impl_from_basic!($T0);
rat_impl_from_basic!($($T1),+);
}
}
rat_impl_from_basic!(u8, u16, u32, usize, u64, u128, i8, i16, i32, isize, i64, i128, Nat);
impl From<BigInt> for Rat {
fn from(n: BigInt) -> Self {
Rat {
a: n.deep_clone(),
b: BigInt::from(1u32),
}
}
}
impl FromStr for Rat {
type Err = RatError;
/// parse string like "323290.0392038920", "-235252532/9403503402385403", "0b1000111/0b111111" or "0x3290523/0xab32342", and so on
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.trim();
if s.is_empty() {
Err(RatError::new(RatErrKind::ParseStringWrong, "empty string"))
} else if s.find('.').is_some() {
match f64::from_str(s) {
Ok(f) => {
if f.is_nan() {
Err(RatError::new(RatErrKind::ParseStringWrong, format!("{} is not a number", s)))
} else {
Ok(Rat::from(f))
}
},
Err(e) => {
Err(RatError::new(RatErrKind::ParseStringWrong, e))
}
}
} else {
let slash_idx = s.find('/');
let (a, b) = match slash_idx {
Some(i) => {
(BigInt::from_str(&s[..i]), BigInt::from_str(&s[(i+1)..]))
},
None => {
(BigInt::from_str(s), Ok(BigInt::from(1u32)))
},
};
let mut a = match a {
Ok(i) => i,
Err(e) => { return Err(RatError::new(RatErrKind::ParseStringWrong, e)); }
};
let mut b = match b {
Ok(i) => i,
Err(e) => { return Err(RatError::new(RatErrKind::ParseStringWrong, e)); }
};
if a.is_nan() || b.is_nan() {
Err(RatError::new(RatErrKind::ParseStringWrong, format!("`{}` is not a number", s)))
} else if b.nat == 0u32 {
Err(RatError::new(RatErrKind::DenominatorIsZero, ""))
} else {
a.sign = BISign::from(a.sign != b.sign);
b.sign = Natural;
Ok(Rat {a, b})
}
}
}
}
macro_rules! impl_rat_fmt_inner {
(($TraitName: ident, $FmtStr0: literal, $FmtStr1: literal)) => {
impl $TraitName for Rat {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.is_nan() {
write!(f, "{}", "NaN")
} else {
let (mut a, b) = if f.alternate() {
(format!($FmtStr1, self.a),
format!($FmtStr1, self.b))
} else {
(format!($FmtStr0, self.a),
format!($FmtStr0, self.b))
};
a.push('/');
a.push_str(b.as_str());
write!(f, "{}", a)
}
}
}
};
(($TraitName0: ident, $FmtStr00: literal, $FmtStr01: literal), $(($TraitName1: ident, $FmtStr10: literal, $FmtStr11: literal)),+) => {
impl_rat_fmt_inner!(($TraitName0, $FmtStr00, $FmtStr01));
impl_rat_fmt_inner!($(($TraitName1, $FmtStr10, $FmtStr11)),+);
}
}
impl_rat_fmt_inner!(
(Debug, "{:?}", "{:?}"),
(Display, "{}", "{}"),
(Binary, "{:b}", "{:#b}"),
(Octal, "{:o}", "{:#o}"),
(LowerHex, "{:x}", "{:#x}"),
(UpperHex, "{:X}", "{:#X}")
);
#[cfg(test)]
mod test;