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
use std::cmp::Ordering;
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
use num_traits::{One, Zero};
use smallvec::SmallVec;
use crate::{Negateable, NonZero, NonZeroUbig, Sign, Ubig};
use crate::integer::big::ops::building_blocks::is_well_formed_non_zero;
use crate::integer::big::ops::non_zero::{add_assign, mul_non_zero, subtracting_cmp};
use crate::integer::big::ops::normalize::simplify_fraction_without_info;
use crate::rational::big::{Big, NonZeroBig};
mod small;
impl<const S: usize> Add<Ubig<S>> for Big<S> {
type Output = Self;
#[inline]
fn add(mut self, rhs: Ubig<S>) -> Self::Output {
AddAssign::add_assign(&mut self, rhs);
self
}
}
impl<const S: usize> Add<&Ubig<S>> for Big<S> {
type Output = Self;
#[inline]
fn add(mut self, rhs: &Ubig<S>) -> Self::Output {
AddAssign::add_assign(&mut self, rhs);
self
}
}
impl<const S: usize> AddAssign<Ubig<S>> for Big<S> {
#[inline]
fn add_assign(&mut self, rhs: Ubig<S>) {
// TODO(PERFORMANCE): Utilize ownership of `rhs`.
AddAssign::add_assign(self, &rhs);
}
}
impl<const S: usize> AddAssign<&Ubig<S>> for Big<S> {
#[inline]
fn add_assign(&mut self, rhs: &Ubig<S>) {
if rhs.is_not_zero() {
unsafe {
// SAFETY: rhs is non zero
self.add_assign_int_non_zero(rhs);
}
}
}
}
impl<const S: usize> Add<NonZeroUbig<S>> for Big<S> {
type Output = Self;
#[inline]
fn add(mut self, rhs: NonZeroUbig<S>) -> Self::Output {
AddAssign::add_assign(&mut self, rhs);
self
}
}
impl<const S: usize> Add<&NonZeroUbig<S>> for Big<S> {
type Output = Self;
#[inline]
fn add(mut self, rhs: &NonZeroUbig<S>) -> Self::Output {
AddAssign::add_assign(&mut self, rhs);
self
}
}
impl<const S: usize> AddAssign<NonZeroUbig<S>> for Big<S> {
#[inline]
fn add_assign(&mut self, rhs: NonZeroUbig<S>) {
// TODO(PERFORMANCE): Utilize ownership of `rhs`.
AddAssign::add_assign(self, &rhs);
}
}
impl<const S: usize> AddAssign<&NonZeroUbig<S>> for Big<S> {
#[inline]
fn add_assign(&mut self, rhs: &NonZeroUbig<S>) {
unsafe {
// SAFETY: rhs is non zero
self.add_assign_int_non_zero(rhs);
}
}
}
impl<const S: usize> Big<S> {
#[inline]
unsafe fn add_assign_int_non_zero(&mut self, rhs: &[usize]) {
debug_assert!(is_well_formed_non_zero(rhs));
match self.sign {
Sign::Positive => {
// SAFETY: A `NonZeroUbig` is never empty and always well formed, and the caller
// guarantees the same of `rhs`. The `inner_mut` hands out the numerator's words;
// adding a magnitude to a non zero numerator leaves it well formed and non zero,
// and leaves the fraction in lowest terms, because the added term is a multiple of
// the denominator and so shares no new factor with it.
unsafe {
let difference = mul_non_zero::<S>(&self.denominator, rhs);
add_assign(self.numerator.inner_mut(), &difference)
}
},
Sign::Zero => {
// SAFETY: `rhs` is well formed and non zero by the caller's guarantee, so writing
// it into the numerator and setting the sign to match leaves the value well
// formed. The denominator is one for a zero value, so the fraction stays in
// lowest terms.
unsafe { *self.numerator.inner_mut() = SmallVec::from_slice(rhs) };
self.sign = Sign::Positive;
debug_assert!(self.denominator.is_one());
}
Sign::Negative => {
// SAFETY: As in the positive arm. `subtracting_cmp` leaves the numerator holding
// the magnitude of the difference, which may be zero; the match below restores the
// sign, and with it the invariant, for each of the three outcomes.
let ordering = unsafe {
let difference = mul_non_zero::<S>(&self.denominator, rhs);
subtracting_cmp(self.numerator.inner_mut(), &difference)
};
match ordering {
Ordering::Less => self.sign.negate(),
Ordering::Equal => self.set_zero(),
Ordering::Greater => {}
}
}
}
}
}
impl<const S: usize> Sub<Ubig<S>> for Big<S> {
type Output = Self;
#[inline]
fn sub(mut self, rhs: Ubig<S>) -> Self::Output {
SubAssign::sub_assign(&mut self, rhs);
self
}
}
impl<const S: usize> Sub<&Ubig<S>> for Big<S> {
type Output = Self;
#[inline]
fn sub(mut self, rhs: &Ubig<S>) -> Self::Output {
SubAssign::sub_assign(&mut self, rhs);
self
}
}
impl<const S: usize> SubAssign<Ubig<S>> for Big<S> {
#[inline]
fn sub_assign(&mut self, rhs: Ubig<S>) {
// TODO(PERFORMANCE): Utilize ownership of `rhs`.
SubAssign::sub_assign(self, &rhs);
}
}
impl<const S: usize> SubAssign<&Ubig<S>> for Big<S> {
#[inline]
fn sub_assign(&mut self, rhs: &Ubig<S>) {
if rhs.is_not_zero() {
unsafe {
// SAFETY: rhs is non zero
self.sub_assign_int_non_zero(rhs);
}
}
}
}
impl<const S: usize> Sub<NonZeroUbig<S>> for Big<S> {
type Output = Self;
#[inline]
fn sub(mut self, rhs: NonZeroUbig<S>) -> Self::Output {
SubAssign::sub_assign(&mut self, rhs);
self
}
}
impl<const S: usize> Sub<&NonZeroUbig<S>> for Big<S> {
type Output = Self;
#[inline]
fn sub(mut self, rhs: &NonZeroUbig<S>) -> Self::Output {
SubAssign::sub_assign(&mut self, rhs);
self
}
}
impl<const S: usize> SubAssign<NonZeroUbig<S>> for Big<S> {
#[inline]
fn sub_assign(&mut self, rhs: NonZeroUbig<S>) {
// TODO(PERFORMANCE): Utilize ownership of `rhs`.
SubAssign::sub_assign(self, &rhs);
}
}
impl<const S: usize> SubAssign<&NonZeroUbig<S>> for Big<S> {
#[inline]
fn sub_assign(&mut self, rhs: &NonZeroUbig<S>) {
unsafe {
// SAFETY: rhs is non zero
self.sub_assign_int_non_zero(rhs);
}
}
}
impl<const S: usize> Big<S> {
#[inline]
unsafe fn sub_assign_int_non_zero(&mut self, rhs: &[usize]) {
debug_assert!(is_well_formed_non_zero(rhs));
match self.sign {
Sign::Positive => {
// SAFETY: A `NonZeroUbig` is never empty and always well formed, and the caller
// guarantees the same of `rhs`. The `inner_mut` hands out the numerator's words;
// `subtracting_cmp` leaves it holding the magnitude of the difference, which may be
// zero, and the match below restores the sign, and with it the invariant, for each
// of the three outcomes.
let ordering = unsafe {
let difference = mul_non_zero::<S>(&self.denominator, rhs);
subtracting_cmp(self.numerator.inner_mut(), &difference)
};
match ordering {
Ordering::Less => self.sign.negate(),
Ordering::Equal => self.set_zero(),
Ordering::Greater => {}
}
}
Sign::Zero => {
// SAFETY: `rhs` is well formed and non zero by the caller's guarantee, so writing
// it into the numerator and setting the sign to match leaves the value well
// formed. The denominator is one for a zero value, so the fraction stays in
// lowest terms.
unsafe { *self.numerator.inner_mut() = SmallVec::from_slice(rhs) };
self.sign = Sign::Negative;
debug_assert!(self.denominator.is_one());
}
Sign::Negative => {
// SAFETY: As in the positive arm. Adding a magnitude to a non zero numerator leaves
// it well formed and non zero, and leaves the fraction in lowest terms, because the
// added term is a multiple of the denominator.
unsafe {
let difference = mul_non_zero::<S>(&self.denominator, rhs);
add_assign(self.numerator.inner_mut(), &difference)
}
},
}
}
}
impl<const S: usize> Mul<Ubig<S>> for Big<S> {
type Output = Self;
#[inline]
fn mul(mut self, rhs: Ubig<S>) -> Self::Output {
MulAssign::mul_assign(&mut self, rhs);
self
}
}
impl<const S: usize> MulAssign<Ubig<S>> for Big<S> {
#[inline]
fn mul_assign(&mut self, rhs: Ubig<S>) {
if !rhs.is_zero() {
if self.is_not_zero() {
// SAFETY: Every operand is well formed and not empty: `rhs` is non zero (by its type, or by
// the check just above), and `self` is not zero, so its numerator is not either.
// The two `inner_mut` calls hand out the words behind the lowest terms invariant,
// which `mul_assign_int_owning` restores before returning.
unsafe {
mul_assign_int_owning(
self.numerator.inner_mut(),
self.denominator.inner_mut(),
rhs.into_inner(),
);
}
}
} else {
self.set_zero();
}
}
}
impl<const S: usize> Mul<NonZeroUbig<S>> for Big<S> {
type Output = Self;
#[inline]
fn mul(mut self, rhs: NonZeroUbig<S>) -> Self::Output {
MulAssign::mul_assign(&mut self, rhs);
self
}
}
impl<const S: usize> MulAssign<NonZeroUbig<S>> for Big<S> {
#[inline]
fn mul_assign(&mut self, rhs: NonZeroUbig<S>) {
if self.is_not_zero() {
// SAFETY: Every operand is well formed and not empty: `rhs` is non zero (by its type, or by the
// check just above), and `self` is not zero, so its numerator is not either. The two
// `inner_mut` calls hand out the words behind the lowest terms invariant, which
// `mul_assign_int_owning` restores before returning.
unsafe {
mul_assign_int_owning(
self.numerator.inner_mut(),
self.denominator.inner_mut(),
rhs.into_inner(),
);
}
}
}
}
impl<const S: usize> Mul<NonZeroUbig<S>> for NonZeroBig<S> {
type Output = Self;
#[inline]
fn mul(mut self, rhs: NonZeroUbig<S>) -> Self::Output {
MulAssign::mul_assign(&mut self, rhs);
self
}
}
impl<const S: usize> MulAssign<NonZeroUbig<S>> for NonZeroBig<S> {
#[inline]
fn mul_assign(&mut self, rhs: NonZeroUbig<S>) {
// SAFETY: Every operand is well formed and not empty: `rhs` is non zero (by its type, or by the
// check just above), and a `NonZeroBig` is never zero, so its numerator is not either. The
// two `inner_mut` calls hand out the words behind the lowest terms invariant, which
// `mul_assign_int_owning` restores before returning.
unsafe {
mul_assign_int_owning(
self.numerator.inner_mut(),
self.denominator.inner_mut(),
rhs.into_inner(),
);
}
}
}
impl<const S: usize> Mul<&NonZeroUbig<S>> for NonZeroBig<S> {
type Output = Self;
#[inline]
fn mul(mut self, rhs: &NonZeroUbig<S>) -> Self::Output {
MulAssign::mul_assign(&mut self, rhs);
self
}
}
impl<const S: usize> MulAssign<&NonZeroUbig<S>> for NonZeroBig<S> {
#[inline]
fn mul_assign(&mut self, rhs: &NonZeroUbig<S>) {
// SAFETY: Every operand is well formed and not empty: `rhs` is non zero (by its type, or by the
// check just above), and a `NonZeroBig` is never zero, so its numerator is not either. The
// two `inner_mut` calls hand out the words behind the lowest terms invariant, which
// `mul_assign_int` restores before returning.
unsafe {
mul_assign_int(
self.numerator.inner_mut(),
self.denominator.inner_mut(),
rhs.inner(),
);
}
}
}
impl<const S: usize> MulAssign<&NonZeroUbig<S>> for Big<S> {
#[inline]
fn mul_assign(&mut self, rhs: &NonZeroUbig<S>) {
if self.is_not_zero() {
// SAFETY: Every operand is well formed and not empty: `rhs` is non zero (by its type, or by the
// check just above), and `self` is not zero, so its numerator is not either. The two
// `inner_mut` calls hand out the words behind the lowest terms invariant, which
// `mul_assign_int` restores before returning.
unsafe {
mul_assign_int(
self.numerator.inner_mut(),
self.denominator.inner_mut(),
rhs.inner(),
);
}
}
}
}
impl<const S: usize> Div<NonZeroUbig<S>> for Big<S> {
type Output = Self;
#[inline]
fn div(mut self, rhs: NonZeroUbig<S>) -> Self::Output {
DivAssign::div_assign(&mut self, rhs);
self
}
}
impl<const S: usize> DivAssign<NonZeroUbig<S>> for Big<S> {
#[inline]
fn div_assign(&mut self, rhs: NonZeroUbig<S>) {
if self.is_not_zero() {
// SAFETY: Every operand is well formed and not empty: `rhs` is non zero (by its type, or by the
// check just above), and `self` is not zero, so its numerator is not either. The two
// `inner_mut` calls hand out the words behind the lowest terms invariant, which
// `mul_assign_int_owning` restores before returning. The numerator and denominator are
// passed the other way around, because dividing a fraction by an integer is multiplying
// its denominator by it.
unsafe {
mul_assign_int_owning(
self.denominator.inner_mut(),
self.numerator.inner_mut(),
rhs.into_inner(),
);
}
}
}
}
impl<const S: usize> DivAssign<&NonZeroUbig<S>> for Big<S> {
#[inline]
fn div_assign(&mut self, rhs: &NonZeroUbig<S>) {
if self.is_not_zero() {
// SAFETY: Every operand is well formed and not empty: `rhs` is non zero (by its type, or by the
// check just above), and `self` is not zero, so its numerator is not either. The two
// `inner_mut` calls hand out the words behind the lowest terms invariant, which
// `mul_assign_int` restores before returning. The numerator and denominator are passed
// the other way around, because dividing a fraction by an integer is multiplying its
// denominator by it.
unsafe {
mul_assign_int(
self.denominator.inner_mut(),
self.numerator.inner_mut(),
rhs.inner(),
);
}
}
}
}
impl<const S: usize> Div<Ubig<S>> for Big<S> {
type Output = Self;
#[inline]
fn div(mut self, rhs: Ubig<S>) -> Self::Output {
DivAssign::div_assign(&mut self, rhs);
self
}
}
impl<const S: usize> DivAssign<Ubig<S>> for Big<S> {
#[inline]
fn div_assign(&mut self, rhs: Ubig<S>) {
if !rhs.is_zero() {
if self.is_not_zero() {
// SAFETY: Every operand is well formed and not empty: `rhs` is non zero (by its type, or by
// the check just above), and `self` is not zero, so its numerator is not either.
// The two `inner_mut` calls hand out the words behind the lowest terms invariant,
// which `mul_assign_int_owning` restores before returning. The numerator and
// denominator are passed the other way around, because dividing a fraction by an
// integer is multiplying its denominator by it.
unsafe {
mul_assign_int_owning(
self.denominator.inner_mut(),
self.numerator.inner_mut(),
rhs.into_inner(),
);
}
}
} else {
panic!("attempt to divide by zero");
}
}
}
impl<const S: usize> Div<NonZeroUbig<S>> for NonZeroBig<S> {
type Output = Self;
#[inline]
fn div(mut self, rhs: NonZeroUbig<S>) -> Self::Output {
DivAssign::div_assign(&mut self, rhs);
self
}
}
impl<const S: usize> DivAssign<NonZeroUbig<S>> for NonZeroBig<S> {
#[inline]
fn div_assign(&mut self, rhs: NonZeroUbig<S>) {
// SAFETY: Every operand is well formed and not empty: `rhs` is non zero (by its type, or by the
// check just above), and a `NonZeroBig` is never zero, so its numerator is not either. The
// two `inner_mut` calls hand out the words behind the lowest terms invariant, which
// `mul_assign_int_owning` restores before returning. The numerator and denominator are
// passed the other way around, because dividing a fraction by an integer is multiplying its
// denominator by it.
unsafe {
mul_assign_int_owning(
self.denominator.inner_mut(),
self.numerator.inner_mut(),
rhs.into_inner(),
);
}
}
}
impl<const S: usize> Div<&NonZeroUbig<S>> for NonZeroBig<S> {
type Output = Self;
#[inline]
fn div(mut self, rhs: &NonZeroUbig<S>) -> Self::Output {
DivAssign::div_assign(&mut self, rhs);
self
}
}
impl<const S: usize> DivAssign<&NonZeroUbig<S>> for NonZeroBig<S> {
#[inline]
fn div_assign(&mut self, rhs: &NonZeroUbig<S>) {
// SAFETY: Every operand is well formed and not empty: `rhs` is non zero (by its type, or by the
// check just above), and a `NonZeroBig` is never zero, so its numerator is not either. The
// two `inner_mut` calls hand out the words behind the lowest terms invariant, which
// `mul_assign_int` restores before returning. The numerator and denominator are passed the
// other way around, because dividing a fraction by an integer is multiplying its
// denominator by it.
unsafe {
mul_assign_int(
self.denominator.inner_mut(),
self.numerator.inner_mut(),
rhs.inner(),
);
}
}
}
#[inline]
unsafe fn mul_assign_int<const S: usize>(
left_numerator: &mut SmallVec<[usize; S]>, left_denominator: &mut SmallVec<[usize; S]>,
right: &[usize],
) {
debug_assert!(is_well_formed_non_zero(left_numerator));
debug_assert!(is_well_formed_non_zero(left_denominator));
debug_assert!(is_well_formed_non_zero(right));
// SAFETY: All three operands are well formed and not empty, so the product is too, which is
// what the simplification then needs of both of its operands.
unsafe {
*left_numerator = mul_non_zero(left_numerator, right);
simplify_fraction_without_info(left_numerator, left_denominator);
}
}
#[inline]
unsafe fn mul_assign_int_owning<const S: usize>(
left_numerator: &mut SmallVec<[usize; S]>, left_denominator: &mut SmallVec<[usize; S]>,
mut right: SmallVec<[usize; S]>,
) {
debug_assert!(is_well_formed_non_zero(left_numerator));
debug_assert!(is_well_formed_non_zero(left_denominator));
debug_assert!(is_well_formed_non_zero(&right));
// SAFETY: All three operands are well formed and not empty. The simplification cancels the
// denominator against the integer before the multiplication rather than after, and leaves both
// of them well formed and not empty, so the product that follows is well defined too.
unsafe {
simplify_fraction_without_info(left_denominator, &mut right);
*left_numerator = mul_non_zero(left_numerator, &right);
}
}
#[cfg(test)]
mod test {
use std::ops::DivAssign;
use num_traits::{One, Zero};
use crate::{NonZeroUbig, RationalBig, Ubig};
use crate::rational::big::NonZeroBig;
use crate::RB;
#[test]
fn add() {
assert_eq!(RB!(2, 3) + NonZeroUbig::new(2).unwrap(), RB!(8, 3));
assert_eq!(RB!(-2, 3) + NonZeroUbig::new(2).unwrap(), RB!(4, 3));
assert_eq!(RB!(2, 3) + Ubig::zero(), RB!(2, 3));
assert_eq!(RB!(2, 3) - NonZeroUbig::new(2).unwrap(), RB!(-4, 3));
assert_eq!(RB!(0) - NonZeroUbig::one(), RB!(-1));
assert_eq!(RB!(-2, 3) - NonZeroUbig::new(2).unwrap(), RB!(-8, 3));
}
#[test]
fn mul_assign() {
assert_eq!(RB!(2, 3) * NonZeroUbig::new(2).unwrap(), RB!(4, 3));
assert_eq!(RB!(2, 3) * Ubig::zero(), RB!(0));
assert_eq!(RB!(2, 3) / NonZeroUbig::new(2).unwrap(), RB!(1, 3));
assert_eq!(RB!(0) * NonZeroUbig::one(), RB!(0));
}
#[test]
#[should_panic]
#[allow(unused_must_use)]
fn mul_panic() {
RationalBig::one() / Ubig::zero();
}
/// `-a / b + n` can cancel exactly: `subtracting_cmp` then returns `Ordering::Equal` and leaves
/// the numerator empty, so the sign has to be reset as well.
#[test]
fn add_int_cancelling_to_zero() {
let mut x = RB!(-5);
x += NonZeroUbig::new(5).unwrap();
assert!(x.is_zero());
assert_eq!(x, RB!(0));
assert_eq!(format!("{:?}", x), "0");
let mut y = RB!(-5);
y += Ubig::new(5);
assert!(y.is_zero());
assert_eq!(y, RB!(0));
assert_eq!(format!("{:?}", y), "0");
}
/// See `add_int_cancelling_to_zero`, the mirrored case `a / b - n`.
#[test]
fn sub_int_cancelling_to_zero() {
let mut x = RB!(5);
x -= NonZeroUbig::new(5).unwrap();
assert!(x.is_zero());
assert_eq!(x, RB!(0));
assert_eq!(format!("{:?}", x), "0");
let mut y = RB!(5);
y -= Ubig::new(5);
assert!(y.is_zero());
assert_eq!(y, RB!(0));
assert_eq!(format!("{:?}", y), "0");
}
/// Adding to zero has to set the sign, just like subtracting from zero does.
#[test]
fn add_int_to_zero_sets_sign() {
let mut x = RB!(0);
x += NonZeroUbig::new(5).unwrap();
assert!(!x.is_zero());
assert_eq!(x, RB!(5));
assert_eq!(format!("{:?}", x), "5");
let mut y = RB!(0);
y += Ubig::new(5);
assert!(!y.is_zero());
assert_eq!(y, RB!(5));
assert_eq!(format!("{:?}", y), "5");
}
/// `Div` on `NonZeroBig` should divide, not multiply.
#[test]
fn div_non_zero_big_by_non_zero_ubig() {
let two = NonZeroUbig::new(2).unwrap();
let mut expected = NonZeroBig::<8>::one();
DivAssign::div_assign(&mut expected, two.clone());
let owned = NonZeroBig::<8>::one() / two.clone();
assert!(owned == expected);
assert!(owned * two.clone() == NonZeroBig::<8>::one());
let borrowed = NonZeroBig::<8>::one() / &two;
assert!(borrowed == expected);
assert!(borrowed * &two == NonZeroBig::<8>::one());
}
/// Dividing zero by a `Ubig` should be a no-op instead of touching the empty numerator.
#[test]
fn div_zero_by_ubig() {
let mut x = RB!(0);
x /= Ubig::<8>::new(2);
assert!(x.is_zero());
assert_eq!(x, RB!(0));
assert_eq!(format!("{:?}", x), "0");
let y = RB!(0) / Ubig::<8>::new(2);
assert!(y.is_zero());
assert_eq!(y, RB!(0));
}
}