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
//! # Interactions with fixed size ratios
use crate::NonZeroUbig;
use crate::rational::{Rational16, Rational32, Rational64, Rational8};
use crate::rational::big::Big;
use crate::sign::Negateable;
use crate::sign::Sign;
use crate::Ubig;
macro_rules! define_interations {
($small:ident, $module_name:ident) => {
mod $module_name {
use super::*;
mod creation {
use super::*;
impl<const S: usize> From<$small> for Big<S> {
#[inline]
fn from(value: $small) -> Self {
Self {
sign: value.sign,
numerator: Ubig::new(value.numerator as usize),
denominator: unsafe {
// SAFETY: Input denominator is nonzero
NonZeroUbig::new_unchecked(value.denominator as usize)
},
}
}
}
impl<const S: usize> From<&$small> for Big<S> {
#[inline]
fn from(value: &$small) -> Self {
Self {
sign: value.sign,
numerator: Ubig::new(value.numerator as usize),
denominator: unsafe {
// SAFETY: Input denominator is nonzero
NonZeroUbig::new_unchecked(value.denominator as usize)
},
}
}
}
}
mod compare {
use super::*;
impl<const S: usize> PartialEq<$small> for Big<S> {
#[inline]
fn eq(&self, other: &$small) -> bool {
// Compare first with the denominator, we don't have to do a bounds check
// and the probability that its equal is small
*self.denominator.first() == other.denominator as usize &&
match self.sign {
Sign::Zero => other.sign == Sign::Zero,
Sign::Positive | Sign::Negative => {
self.sign == other.sign &&
self.numerator[0] == other.numerator as usize &&
self.numerator.len() == 1 &&
self.denominator.len() == 1
}
}
}
}
}
mod field {
use super::*;
use crate::sign::Sign;
mod add {
use super::*;
use std::ops::{Add, AddAssign};
use crate::rational::big::ops::building_blocks::{add_small, sub_small, SignChange};
impl<const S: usize> Add<$small> for Big<S> {
type Output = Self;
#[inline]
fn add(mut self, rhs: $small) -> Self::Output {
AddAssign::add_assign(&mut self, &rhs);
self
}
}
impl<const S: usize> Add<&$small> for Big<S> {
type Output = Self;
#[inline]
fn add(mut self, rhs: &$small) -> Self::Output {
AddAssign::add_assign(&mut self, rhs);
self
}
}
impl<const S: usize> Add<Option<&$small>> for Big<S> {
type Output = Self;
#[inline]
fn add(self, rhs: Option<&$small>) -> Self::Output {
match rhs {
None => self,
Some(rhs) => Add::add(self, rhs),
}
}
}
impl<const S: usize> Add<&$small> for &Big<S> {
type Output = Big<S>;
#[inline]
fn add(self, rhs: &$small) -> Self::Output {
// TODO(PERFORMANCE): Make sure that this is just as efficient as a native algorithm.
let mut cloned = self.clone();
AddAssign::add_assign(&mut cloned, rhs);
cloned
}
}
impl<const S: usize> Add<Option<&$small>> for &Big<S> {
type Output = Big<S>;
#[inline]
fn add(self, rhs: Option<&$small>) -> Self::Output {
// TODO(PERFORMANCE): Make sure that this is just as efficient as a native algorithm.
let copy = self.clone();
match rhs {
None => copy,
Some(rhs) => Add::add(copy, rhs),
}
}
}
impl<const S: usize> AddAssign<$small> for Big<S> {
#[inline]
fn add_assign(&mut self, rhs: $small) {
AddAssign::add_assign(self, &rhs);
}
}
impl<const S: usize> AddAssign<&$small> for Big<S> {
#[inline]
fn add_assign(&mut self, rhs: &$small) {
match (self.sign, rhs.sign) {
(Sign::Positive, Sign::Positive) | (Sign::Negative, Sign::Negative) => {
unsafe {
// SAFETY: The numbers are non zero
add_small(
self.numerator.inner_mut(),
self.denominator.inner_mut(),
rhs.numerator as usize,
rhs.denominator as usize,
);
}
},
(Sign::Negative, Sign::Positive) | (Sign::Positive, Sign::Negative) => {
unsafe {
// SAFETY: The numbers are non zero
let sign_change = sub_small(
self.numerator.inner_mut(),
self.denominator.inner_mut(),
rhs.numerator as usize,
rhs.denominator as usize,
);
match sign_change {
SignChange::None => {}
SignChange::Flip => self.sign.negate(),
SignChange::Zero => self.sign = Sign::Zero,
}
}
}
(_, Sign::Zero) => {}
(Sign::Zero, _) => {
self.sign = rhs.sign;
debug_assert_eq!(self.numerator.len(), 0);
unsafe {
// SAFETY: Value was empty before and rhs.numerator is non zero
self.numerator.inner_mut().push(rhs.numerator as usize);
}
debug_assert_eq!(self.denominator.len(), 1);
debug_assert_eq!(self.denominator[0], 1);
unsafe {
// SAFETY: Value was one before and rhs.denominator is non zero
*self.denominator.first_mut() = rhs.denominator as usize;
}
}
}
}
}
}
mod sub {
use super::*;
use crate::rational::big::ops::building_blocks::{add_small, sub_small, SignChange};
use std::ops::{Sub, SubAssign};
impl<const S: usize> Sub<$small> for Big<S> {
type Output = Self;
#[inline]
fn sub(mut self, rhs: $small) -> Self::Output {
SubAssign::sub_assign(&mut self, &rhs);
self
}
}
impl<const S: usize> Sub<&$small> for Big<S> {
type Output = Self;
#[inline]
fn sub(mut self, rhs: &$small) -> Self::Output {
SubAssign::sub_assign(&mut self, rhs);
self
}
}
impl<const S: usize> Sub<Option<&$small>> for Big<S> {
type Output = Self;
#[inline]
fn sub(self, rhs: Option<&$small>) -> Self::Output {
match rhs {
None => self,
Some(rhs) => Sub::sub(self, rhs),
}
}
}
impl<const S: usize> Sub<&$small> for &Big<S> {
type Output = Big<S>;
#[inline]
fn sub(self, rhs: &$small) -> Self::Output {
// TODO(PERFORMANCE): Make sure that this is just as efficient as a native algorithm.
let mut cloned = self.clone();
SubAssign::sub_assign(&mut cloned, rhs);
cloned
}
}
impl<const S: usize> Sub<Option<&$small>> for &Big<S> {
type Output = Big<S>;
#[inline]
fn sub(self, rhs: Option<&$small>) -> Self::Output {
// TODO(PERFORMANCE): Make sure that this is just as efficient as a native algorithm.
let copy = self.clone();
match rhs {
None => copy,
Some(rhs) => Sub::sub(copy, rhs),
}
}
}
impl<const S: usize> SubAssign<$small> for Big<S> {
#[inline]
fn sub_assign(&mut self, rhs: $small) {
SubAssign::sub_assign(self, &rhs);
}
}
impl<const S: usize> SubAssign<&$small> for Big<S> {
#[inline]
fn sub_assign(&mut self, rhs: &$small) {
match (self.sign, rhs.sign) {
(Sign::Positive, Sign::Positive) | (Sign::Negative, Sign::Negative) => {
unsafe {
// SAFETY: The numbers are non zero
let sign_change = sub_small(
self.numerator.inner_mut(),
self.denominator.inner_mut(),
rhs.numerator as usize,
rhs.denominator as usize,
);
match sign_change {
SignChange::None => {}
SignChange::Flip => self.sign.negate(),
SignChange::Zero => self.sign = Sign::Zero,
}
}
}
(Sign::Negative, Sign::Positive) | (Sign::Positive, Sign::Negative) => {
unsafe {
// SAFETY: The numbers are non zero
add_small(
self.numerator.inner_mut(),
self.denominator.inner_mut(),
rhs.numerator as usize,
rhs.denominator as usize,
);
}
}
(_, Sign::Zero) => {}
(Sign::Zero, _) => {
self.sign = -rhs.sign;
debug_assert_eq!(self.numerator.len(), 0);
unsafe {
// SAFETY: The rhs numerator is not zero
self.numerator.inner_mut().push(rhs.numerator as usize)
};
debug_assert_eq!(self.denominator.len(), 1);
debug_assert_eq!(self.denominator[0], 1);
unsafe {
// SAFETY: The rhs denominator is not zero
*self.denominator.first_mut() = rhs.denominator as usize;
}
}
}
}
}
}
mod mul {
use super::*;
use std::ops::{Mul, MulAssign};
use crate::rational::big::ops::building_blocks::mul_small;
impl<const S: usize> Mul<$small> for Big<S> {
type Output = Self;
#[inline]
fn mul(self, rhs: $small) -> Self::Output {
Mul::mul(self, &rhs)
}
}
impl<const S: usize> Mul<&$small> for Big<S> {
type Output = Self;
#[inline]
fn mul(mut self, rhs: &$small) -> Self::Output {
MulAssign::mul_assign(&mut self, rhs);
self
}
}
impl<const S: usize> Mul<&$small> for &Big<S> {
type Output = Big<S>;
#[inline]
fn mul(self, rhs: &$small) -> Self::Output {
// TODO(PERFORMANCE): Make sure that this is just as efficient as a native algorithm.
self.clone() * rhs
}
}
impl<const S: usize> Mul<Option<&$small>> for Big<S> {
type Output = Big<S>;
#[inline]
fn mul(mut self, rhs: Option<&$small>) -> Self::Output {
match rhs {
None => {
<Self as num_traits::Zero>::set_zero(&mut self);
self
},
Some(rhs) => Mul::mul(self, rhs),
}
}
}
impl<const S: usize> Mul<Option<&$small>> for &Big<S> {
type Output = Big<S>;
#[inline]
fn mul(self, rhs: Option<&$small>) -> Self::Output {
match rhs {
None => <Self::Output as num_traits::Zero>::zero(),
Some(rhs) => Mul::mul(self, rhs),
}
}
}
impl<const S: usize> MulAssign<$small> for Big<S> {
#[inline]
fn mul_assign(&mut self, rhs: $small) {
MulAssign::mul_assign(self, &rhs);
}
}
impl<const S: usize> MulAssign<&$small> for Big<S> {
#[inline]
fn mul_assign(&mut self, rhs: &$small) {
match (self.sign, rhs.sign) {
(Sign::Positive | Sign::Negative, Sign::Positive | Sign::Negative) => {
self.sign *= rhs.sign;
unsafe {
mul_small(
self.numerator.inner_mut(),
self.denominator.inner_mut(),
rhs.numerator as usize,
rhs.denominator as usize,
)
}
}
(Sign::Positive | Sign::Negative, Sign::Zero) => {
<Self as num_traits::Zero>::set_zero(self);
}
(Sign::Zero, _) => {}
}
}
}
}
mod div {
use super::*;
use std::ops::{Div, DivAssign};
use crate::rational::big::ops::building_blocks::mul_small;
impl<const S: usize> Div<$small> for Big<S> {
type Output = Big<S>;
#[inline]
fn div(mut self, rhs: $small) -> Self::Output {
DivAssign::div_assign(&mut self, rhs);
self
}
}
impl<const S: usize> Div<&$small> for Big<S> {
type Output = Big<S>;
#[inline]
fn div(mut self, rhs: &$small) -> Self::Output {
DivAssign::div_assign(&mut self, rhs);
self
}
}
impl<const S: usize> DivAssign<$small> for Big<S> {
#[inline]
fn div_assign(&mut self, rhs: $small) {
DivAssign::div_assign(self, &rhs);
}
}
// The sign of a quotient is the product of the signs, so `*` is correct here.
#[allow(clippy::suspicious_op_assign_impl)]
impl<const S: usize> DivAssign<&$small> for Big<S> {
#[inline]
fn div_assign(&mut self, rhs: &$small) {
match (self.sign, rhs.sign) {
(Sign::Positive | Sign::Negative, Sign::Positive | Sign::Negative) => {
self.sign *= rhs.sign;
unsafe {
mul_small(
self.numerator.inner_mut(),
self.denominator.inner_mut(),
rhs.denominator as usize,
rhs.numerator as usize,
);
}
}
(_, Sign::Zero) => panic!("attempt to divide by zero"),
(Sign::Zero, _) => {}
}
}
}
}
}
}
}
}
define_interations!(Rational8, rational8);
define_interations!(Rational16, rational16);
define_interations!(Rational32, rational32);
define_interations!(Rational64, rational64);
#[cfg(test)]
mod test {
use num_traits::One;
use smallvec::smallvec;
use crate::{R16, R32, R64, R8, Rational64, RationalBig, RB, Sign, Ubig};
use crate::integer::big::NonZeroUbig;
#[test]
fn test_eq() {
assert_eq!(RB!(0), R64!(0));
assert_eq!(RB!(-2), R64!(-2));
assert_eq!(RB!(854984, 6868468468), R64!(854984, 6868468468));
assert_eq!(RB!(-989888, 4968468421), R64!(-989888, 4968468421));
assert_ne!(RB!(668468498646846546846546898997987_u128), R8!(1));
assert_ne!(RB!(668468498646846546846546898997987.4385484_f64), R8!(1, 2));
}
#[test]
fn test_add_sub() {
assert_eq!(RB!(0) + R8!(0), RB!(0));
assert_eq!(RB!(89) + R8!(1), RB!(90));
assert_eq!(RB!(89) - R16!(1), RB!(88));
assert_eq!(RB!(2, 3) + R16!(4, 9), RB!(10, 9));
assert_eq!(RB!(989, 141) + R8!(1, 141), RB!(990, 141));
assert_eq!(RB!(-84, 3) + R8!(1, 3), RB!(-83, 3));
assert_eq!(RB!(1, 2) + R8!(1, 2), RB!(1));
assert_eq!(RB!(-1, 2) + R8!(1, 2), RB!(0));
assert_eq!(RB!(-1, 2) + R8!(1), RB!(1, 2));
assert_eq!(RB!(7, 2) + R8!(5, 2), RB!(6));
assert_eq!(RB!(-7, 2) + R8!(5, 2), RB!(-1));
assert_eq!(RB!(-7, 2) - R8!(5, 2), RB!(-6));
assert_eq!(
RationalBig {
sign: Sign::Positive,
numerator: unsafe { Ubig::from_inner_unchecked(smallvec![1, 1]) },
denominator: unsafe { NonZeroUbig::from_inner_unchecked(smallvec![2]) },
} + R8!(1, 2),
RationalBig {
sign: Sign::Positive,
numerator: unsafe { Ubig::from_inner_unchecked(smallvec![(1 << 63) + 1]) },
denominator: NonZeroUbig::one(),
},
);
assert_eq!(RB!(2, 3) + R8!(8, 1), RB!(8 * 3 + 2, 3));
assert_eq!(RB!(8) + R8!(16, 3), RB!(8 * 3 + 16, 3));
assert_eq!(RB!(3, 5) + R16!(2, 5), RB!(1));
assert_eq!(RB!(5) - R32!(7), RB!(-2));
assert_eq!(
RationalBig {
sign: Sign::Positive,
numerator: unsafe { Ubig::from_inner_unchecked(smallvec![1, 1]) },
denominator: unsafe { NonZeroUbig::from_inner_unchecked(smallvec![2]) },
} - R8!(1, 2),
RationalBig {
sign: Sign::Positive,
numerator: unsafe { Ubig::from_inner_unchecked(smallvec![1 << 63]) },
denominator: NonZeroUbig::one(),
},
);
assert_eq!(RB!(5, 2) - R8!(2), RB!(1, 2));
assert_eq!(RB!(3) - R8!(1, 2), RB!(5, 2));
assert_eq!(RB!(16, 3) - R8!(11, 4), RB!(16 * 4 - 11 * 3, 12));
assert_eq!(RB!(0) + R8!(1, 2), RB!(1, 2));
assert_eq!(RB!(0) - R8!(3, 4), RB!(-3, 4));
assert_eq!(RB!(-10, 4) + R8!(-10, 8), RB!(-15, 4));
assert_eq!(RB!(-10, 4) + R64!(1, 4), RB!(-9, 4));
assert_eq!(RB!(-10, 8) + R64!(-10, 9), RB!(-5 * 17, 3 * 3 * 2 * 2));
assert_eq!(RB!(-5, 4) + R64!(-5, 4), RB!(-5, 2));
assert_eq!(RB!(-7, 6) + R64!(-7, 6), RB!(-7, 3));
let limit = 10;
for a in -limit..limit {
for b in 1..limit {
for c in -limit..limit {
for d in 1..limit {
assert_eq!(RB!(a, b as u64) + R64!(c, d as u64), RB!(a * d + c * b, b as u64 * d as u64), "{} / {} - {} / {}", a, b, c, d);
}
}
}
}
}
#[test]
fn test_mul_div() {
assert_eq!(RB!(0) * R8!(18), RB!(0));
assert_eq!(RB!(1) * R8!(6), RB!(6));
assert_eq!(RB!(-1) * R64!(65488846), -RB!(65488846));
assert_eq!(RB!(7, 11) * R8!(11, 7), RB!(1));
assert_eq!(RB!(7, 11) * R16!(22, 7), RB!(2));
assert_eq!(RB!(14, 33) * R32!(3, 2), RB!(7, 11));
assert_eq!(RB!(4, 3) * R64!(0), RB!(0));
let limit = 10;
for a in -limit..limit {
for b in 1..limit {
for c in -limit..limit {
for d in 1..limit {
assert_eq!(RB!(a, b as u64) * R64!(c, d as u64), RB!(a * c, (b * d) as u64), "{} / {} * {} / {}", a, b, c, d);
}
}
}
}
}
/// The sign has to take part in the comparison.
///
/// It used to be dropped for non-zero values, so `-2` compared equal to `2`.
#[test]
fn eq_small_respects_sign() {
assert_eq!(RB!(2) == R64!(2), true);
assert_eq!(RB!(-2) == R64!(2), false);
assert_eq!(RB!(2) == R64!(-2), false);
assert_eq!(RB!(-2) == R64!(-2), true);
assert_eq!(RB!(1, 2) == R8!(1, 2), true);
assert_eq!(RB!(-1, 2) == R8!(1, 2), false);
assert_eq!(RB!(1, 2) == R8!(-1, 2), false);
assert_eq!(RB!(-1, 2) == R8!(-1, 2), true);
assert_eq!(RB!(0) == R32!(0), true);
// Equality has to agree with the ordering it is paired with.
for numerator in -4_i64..=4 {
for denominator in 1_u64..=4 {
let small = Rational64::new(numerator, denominator).unwrap();
let big = RationalBig::from(small);
for other in -4_i64..=4 {
let other = Rational64::new(other, denominator).unwrap();
#[allow(clippy::cmp_owned, reason = "comparing after conversion is what is being tested")]
let converted = big == RationalBig::from(other);
assert_eq!(
big == other,
converted,
"{numerator}/{denominator} vs {other}",
);
}
}
}
}
/// Dividing zero by zero has to fail the same way any other division by zero does.
#[test]
#[should_panic(expected = "attempt to divide by zero")]
fn div_zero_by_zero_panics() {
let _ = RB!(0) / R8!(0);
}
}