aboutsummaryrefslogtreecommitdiff
path: root/src/numbers/dynnum.rs
blob: eebfdb99a023c90ea3e944ee2c42a856a3c00178 (plain)
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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
1/*  OpenTally: Open-source election vote counting
2 *  Copyright © 2021 Lee Yingtong Li (RunasSudo)
3 *
4 *  This program is free software: you can redistribute it and/or modify
5 *  it under the terms of the GNU Affero General Public License as published by
6 *  the Free Software Foundation, either version 3 of the License, or
7 *  (at your option) any later version.
8 *
9 *  This program is distributed in the hope that it will be useful,
10 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *  GNU Affero General Public License for more details.
13 *
14 *  You should have received a copy of the GNU Affero General Public License
15 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 */
17
18use super::{Assign, Fixed, GuardedFixed, NativeFloat64, Number, Rational};
19
20use num_traits::{Num, One, Zero};
21//use wasm_bindgen::prelude::wasm_bindgen;
22
23use std::cmp::{Ord, Ordering};
24use std::fmt;
25use std::mem::ManuallyDrop;
26use std::ops::{self, Deref, DerefMut};
27
28/// Represents the underlying implementation for [DynNum]s
29//#[wasm_bindgen]
30pub enum NumKind {
31	/// See [crate::numbers::fixed]
32	Fixed,
33	/// See [crate::numbers::gfixed]
34	GuardedFixed,
35	/// See [crate::numbers::native]
36	NativeFloat64,
37	/// See [crate::numbers::rational_rug] or [crate::numbers::rational_num]
38	Rational,
39}
40
41/// Determines which underlying implementation to use
42static mut KIND: NumKind = NumKind::Fixed;
43
44/// Returns which underlying implementation is in use
45#[inline]
46fn get_kind() -> &'static NumKind {
47	unsafe {
48		return &KIND;
49	}
50}
51
52/// A wrapper for different numeric types using dynamic dispatch
53pub union DynNum {
54	fixed: ManuallyDrop<Fixed>,
55	gfixed: ManuallyDrop<GuardedFixed>,
56	float64: NativeFloat64,
57	rational: ManuallyDrop<Rational>,
58}
59
60impl DynNum {
61	/// Set which underlying implementation to use
62	pub fn set_kind(kind: NumKind) {
63		unsafe {
64			KIND = kind;
65		}
66	}
67}
68
69macro_rules! impl_1arg_nowrap {
70	($self:expr, $arg:expr, $func:ident) => {
71		// Safety: Access only correct union field
72		unsafe {
73			match get_kind() {
74				NumKind::Fixed => {
75					$self.fixed.$func($arg)
76				}
77				NumKind::GuardedFixed => {
78					$self.gfixed.$func($arg)
79				}
80				NumKind::NativeFloat64 => {
81					$self.float64.$func($arg)
82				}
83				NumKind::Rational => {
84					$self.rational.$func($arg)
85				}
86			}
87		}
88	}
89}
90
91macro_rules! impl_assoc_nowrap {
92	($func:ident) => {
93		match get_kind() {
94			NumKind::Fixed => {
95				Fixed::$func()
96			}
97			NumKind::GuardedFixed => {
98				GuardedFixed::$func()
99			}
100			NumKind::NativeFloat64 => {
101				NativeFloat64::$func()
102			}
103			NumKind::Rational => {
104				Rational::$func()
105			}
106		}
107	}
108}
109
110impl Number for DynNum {
111	fn new() -> Self {
112		match get_kind() {
113			NumKind::Fixed => {
114				DynNum { fixed: ManuallyDrop::new(Fixed::new()) }
115			}
116			NumKind::GuardedFixed => {
117				DynNum { gfixed: ManuallyDrop::new(GuardedFixed::new()) }
118			}
119			NumKind::NativeFloat64 => {
120				DynNum { float64: NativeFloat64::new() }
121			}
122			NumKind::Rational => {
123				DynNum { rational: ManuallyDrop::new(Rational::new()) }
124			}
125		}
126	}
127	
128	fn parse(str: &str) -> Self {
129		// Separate implementation required as e.g. Fixed from_str_radix does not support decimals
130		match get_kind() {
131			NumKind::Fixed => {
132				DynNum { fixed: ManuallyDrop::new(Fixed::parse(str)) }
133			}
134			NumKind::GuardedFixed => {
135				DynNum { gfixed: ManuallyDrop::new(GuardedFixed::parse(str)) }
136			}
137			NumKind::NativeFloat64 => {
138				DynNum { float64: NativeFloat64::parse(str) }
139			}
140			NumKind::Rational => {
141				DynNum { rational: ManuallyDrop::new(Rational::parse(str)) }
142			}
143		}
144	}
145	
146	fn describe() -> String { impl_assoc_nowrap!(describe) }
147	fn pow_assign(&mut self, exponent: i32) { impl_1arg_nowrap!(self, exponent, pow_assign) }
148	fn floor_mut(&mut self, dps: usize) { impl_1arg_nowrap!(self, dps, floor_mut) }
149	fn ceil_mut(&mut self, dps: usize) { impl_1arg_nowrap!(self, dps, ceil_mut) }
150	fn round_mut(&mut self, dps: usize) { impl_1arg_nowrap!(self, dps, round_mut) }
151}
152
153#[test]
154fn rounding() {
155	// Must specify scope so references are dropped at the correct time, before KIND is changed
156	{
157		DynNum::set_kind(NumKind::Fixed);
158		Fixed::set_dps(5);
159		let mut x = DynNum::parse("55.557"); x.floor_mut(2); assert_eq!(x, DynNum::parse("55.55"));
160		let mut x = DynNum::parse("55.557"); x.ceil_mut(2); assert_eq!(x, DynNum::parse("55.56"));
161		let mut x = DynNum::parse("55.557"); x.round_mut(2); assert_eq!(x, DynNum::parse("55.56"));
162	}
163	{
164		DynNum::set_kind(NumKind::GuardedFixed);
165		GuardedFixed::set_dps(5);
166		let mut x = DynNum::parse("55.557"); x.floor_mut(2); assert_eq!(x, DynNum::parse("55.55"));
167		let mut x = DynNum::parse("55.557"); x.ceil_mut(2); assert_eq!(x, DynNum::parse("55.56"));
168		let mut x = DynNum::parse("55.557"); x.round_mut(2); assert_eq!(x, DynNum::parse("55.56"));
169	}
170	{
171		DynNum::set_kind(NumKind::NativeFloat64);
172		let mut x = DynNum::parse("55.557"); x.floor_mut(2); assert_eq!(x, DynNum::parse("55.55"));
173		let mut x = DynNum::parse("55.557"); x.ceil_mut(2); assert_eq!(x, DynNum::parse("55.56"));
174		let mut x = DynNum::parse("55.557"); x.round_mut(2); assert_eq!(x, DynNum::parse("55.56"));
175	}
176	{
177		DynNum::set_kind(NumKind::Rational);
178		let mut x = DynNum::parse("55.557"); x.floor_mut(2); assert_eq!(x, DynNum::parse("55.55"));
179		let mut x = DynNum::parse("55.557"); x.ceil_mut(2); assert_eq!(x, DynNum::parse("55.56"));
180		let mut x = DynNum::parse("55.557"); x.round_mut(2); assert_eq!(x, DynNum::parse("55.56"));
181	}
182}
183
184impl Drop for DynNum {
185	fn drop(&mut self) {
186		// Safety: Access only correct union field
187		unsafe {
188			match get_kind() {
189				NumKind::Fixed => {
190					ManuallyDrop::drop(&mut self.fixed);
191				}
192				NumKind::GuardedFixed => {
193					ManuallyDrop::drop(&mut self.gfixed);
194				}
195				NumKind::NativeFloat64 => {}
196				NumKind::Rational => {
197					ManuallyDrop::drop(&mut self.rational);
198				}
199			}
200		}
201	}
202}
203
204macro_rules! impl_0arg_wrap {
205	($self:expr, $func:ident) => {
206		// Safety: Access only correct union field
207		unsafe {
208			match get_kind() {
209				NumKind::Fixed => {
210					DynNum { fixed: ManuallyDrop::new($self.fixed.deref().$func()) }
211				}
212				NumKind::GuardedFixed => {
213					DynNum { gfixed: ManuallyDrop::new($self.gfixed.deref().$func()) }
214				}
215				NumKind::NativeFloat64 => {
216					DynNum { float64: $self.float64.$func() }
217				}
218				NumKind::Rational => {
219					DynNum { rational: ManuallyDrop::new($self.rational.deref().$func()) }
220				}
221			}
222		}
223	}
224}
225
226impl Clone for DynNum {
227	fn clone(&self) -> Self { impl_0arg_wrap!(self, clone) }
228}
229
230impl Num for DynNum {
231	type FromStrRadixErr = Self; // TODO
232	
233	fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
234		match get_kind() {
235			NumKind::Fixed => {
236				Ok(DynNum { fixed: ManuallyDrop::new(Fixed::from_str_radix(str, radix).unwrap()) })
237			}
238			NumKind::GuardedFixed => {
239				Ok(DynNum { gfixed: ManuallyDrop::new(GuardedFixed::from_str_radix(str, radix).unwrap()) })
240			}
241			NumKind::NativeFloat64 => {
242				Ok(DynNum { float64: NativeFloat64::from_str_radix(str, radix).unwrap() })
243			}
244			NumKind::Rational => {
245				Ok(DynNum { rational: ManuallyDrop::new(Rational::from_str_radix(str, radix).unwrap()) })
246			}
247		}
248	}
249}
250
251macro_rules! impl_1other_nowrap {
252	($self:expr, $rhs:expr, $func:ident) => {
253		// Safety: Access only correct union field
254		unsafe {
255			match get_kind() {
256				NumKind::Fixed => {
257					$self.fixed.deref().$func($rhs.fixed.deref())
258				}
259				NumKind::GuardedFixed => {
260					$self.gfixed.deref().$func($rhs.gfixed.deref())
261				}
262				NumKind::NativeFloat64 => {
263					$self.float64.$func(&$rhs.float64)
264				}
265				NumKind::Rational => {
266					$self.rational.deref().$func($rhs.rational.deref())
267				}
268			}
269		}
270	}
271}
272
273macro_rules! impl_1other_nowrap_mut {
274	($self:expr, $rhs:expr, $func:ident) => {
275		// Safety: Access only correct union field
276		unsafe {
277			match get_kind() {
278				NumKind::Fixed => {
279					$self.fixed.deref_mut().$func($rhs.fixed.deref())
280				}
281				NumKind::GuardedFixed => {
282					$self.gfixed.deref_mut().$func($rhs.gfixed.deref())
283				}
284				NumKind::NativeFloat64 => {
285					$self.float64.$func(&$rhs.float64)
286				}
287				NumKind::Rational => {
288					$self.rational.deref_mut().$func($rhs.rational.deref())
289				}
290			}
291		}
292	}
293}
294
295impl Assign for DynNum {
296	fn assign(&mut self, src: Self) { impl_1other_nowrap_mut!(self, src, assign) }
297}
298
299impl Assign<&Self> for DynNum {
300	fn assign(&mut self, src: &Self) { impl_1other_nowrap_mut!(self, src, assign) }
301}
302
303#[test]
304fn assign() {
305	{
306		DynNum::set_kind(NumKind::Fixed);
307		Fixed::set_dps(2);
308		let a = DynNum::parse("123.45");
309		let b = DynNum::parse("678.90");
310		let mut x = a.clone(); x.assign(b.clone()); assert_eq!(x, b);
311		let mut x = a.clone(); x.assign(&b); assert_eq!(x, b);
312	}
313	{
314		DynNum::set_kind(NumKind::GuardedFixed);
315		GuardedFixed::set_dps(2);
316		let a = DynNum::parse("123.45");
317		let b = DynNum::parse("678.90");
318		let mut x = a.clone(); x.assign(b.clone()); assert_eq!(x, b);
319		let mut x = a.clone(); x.assign(&b); assert_eq!(x, b);
320	}
321	{
322		DynNum::set_kind(NumKind::NativeFloat64);
323		let a = DynNum::parse("123.45");
324		let b = DynNum::parse("678.90");
325		let mut x = a.clone(); x.assign(b.clone()); assert_eq!(x, b);
326		let mut x = a.clone(); x.assign(&b); assert_eq!(x, b);
327	}
328	{
329		DynNum::set_kind(NumKind::Rational);
330		let a = DynNum::parse("123.45");
331		let b = DynNum::parse("678.90");
332		let mut x = a.clone(); x.assign(b.clone()); assert_eq!(x, b);
333		let mut x = a.clone(); x.assign(&b); assert_eq!(x, b);
334	}
335}
336
337impl From<usize> for DynNum {
338	fn from(n: usize) -> Self {
339		match get_kind() {
340			NumKind::Fixed => {
341				DynNum { fixed: ManuallyDrop::new(Fixed::from(n)) }
342			}
343			NumKind::GuardedFixed => {
344				DynNum { gfixed: ManuallyDrop::new(GuardedFixed::from(n)) }
345			}
346			NumKind::NativeFloat64 => {
347				DynNum { float64: NativeFloat64::from(n) }
348			}
349			NumKind::Rational => {
350				DynNum { rational: ManuallyDrop::new(Rational::from(n)) }
351			}
352		}
353	}
354}
355
356impl fmt::Display for DynNum {
357	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { impl_1arg_nowrap!(self, f, fmt) }
358}
359
360impl fmt::Debug for DynNum {
361	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
362		// Safety: Access only correct union field
363		unsafe {
364			match get_kind() {
365				NumKind::Fixed => {
366					self.fixed.deref().fmt(f)
367				}
368				NumKind::GuardedFixed => {
369					self.gfixed.deref().fmt(f)
370				}
371				NumKind::NativeFloat64 => {
372					self.float64.fmt(f)
373				}
374				NumKind::Rational => {
375					self.rational.deref().fmt(f)
376				}
377			}
378		}
379	}
380}
381
382#[test]
383fn display_debug() {
384	{
385		DynNum::set_kind(NumKind::Fixed);
386		Fixed::set_dps(2);
387		let x = DynNum::parse("123.4"); assert_eq!(format!("{}", x), "123.40");
388		let x = DynNum::parse("123.4"); assert_eq!(format!("{:?}", x), "Fixed(12340)");
389	}
390	{
391		DynNum::set_kind(NumKind::GuardedFixed);
392		GuardedFixed::set_dps(2);
393		let x = DynNum::parse("123.4"); assert_eq!(format!("{}", x), "123.40");
394		let x = DynNum::parse("123.4"); assert_eq!(format!("{:?}", x), "GuardedFixed(1234000)");
395	}
396	{
397		DynNum::set_kind(NumKind::NativeFloat64);
398		let x = DynNum::parse("123.4"); assert_eq!(format!("{}", x), format!("{}", 123.40_f64));
399		let x = DynNum::parse("123.4"); assert_eq!(format!("{:?}", x), format!("NativeFloat64({})", 123.40_f64));
400	}
401	{
402		DynNum::set_kind(NumKind::Rational);
403		let x = DynNum::parse("123.4"); assert_eq!(format!("{}", x), "617/5");
404		let x = DynNum::parse("123.4"); assert_eq!(format!("{:.2}", x), "123.40");
405		let x = DynNum::parse("123.4"); assert_eq!(format!("{:?}", x), "Rational(617/5)");
406	}
407}
408
409impl PartialEq for DynNum {
410	fn eq(&self, other: &Self) -> bool { impl_1other_nowrap!(self, other, eq) }
411}
412
413impl Eq for DynNum {}
414
415impl PartialOrd for DynNum {
416	fn partial_cmp(&self, other: &Self) -> Option<Ordering> { impl_1other_nowrap!(self, other, partial_cmp) }
417}
418
419impl Ord for DynNum {
420	fn cmp(&self, other: &Self) -> Ordering { impl_1other_nowrap!(self, other, cmp) }
421}
422
423macro_rules! impl_assoc_wrap {
424	($func:ident) => {
425		match get_kind() {
426			NumKind::Fixed => {
427				DynNum { fixed: ManuallyDrop::new(Fixed::$func()) }
428			}
429			NumKind::GuardedFixed => {
430				DynNum { gfixed: ManuallyDrop::new(GuardedFixed::$func()) }
431				}
432			NumKind::NativeFloat64 => {
433				DynNum { float64: NativeFloat64::$func() }
434			}
435			NumKind::Rational => {
436				DynNum { rational: ManuallyDrop::new(Rational::$func()) }
437			}
438		}
439	}
440}
441
442impl One for DynNum {
443	fn one() -> Self { impl_assoc_wrap!(one) }
444}
445
446macro_rules! impl_0arg_nowrap {
447	($self:expr, $func:ident) => {
448		// Safety: Access only correct union field
449		unsafe {
450			match get_kind() {
451				NumKind::Fixed => {
452					$self.fixed.deref().$func()
453				}
454				NumKind::GuardedFixed => {
455					$self.gfixed.deref().$func()
456				}
457				NumKind::NativeFloat64 => {
458					$self.float64.$func()
459				}
460				NumKind::Rational => {
461					$self.rational.deref().$func()
462				}
463			}
464		}
465	}
466}
467
468impl Zero for DynNum {
469	fn zero() -> Self { impl_assoc_wrap!(zero) }
470	
471	fn is_zero(&self) -> bool { impl_0arg_nowrap!(self, is_zero) }
472}
473
474impl ops::Neg for DynNum {
475	type Output = Self;
476	fn neg(self) -> Self::Output { impl_0arg_wrap!(self, neg) }
477}
478
479macro_rules! impl_1other_wrap {
480	($self:expr, $rhs:expr, $func:ident) => {
481		// Safety: Access only correct union field
482		unsafe {
483			match get_kind() {
484				NumKind::Fixed => {
485					DynNum { fixed: ManuallyDrop::new($self.fixed.deref().$func($rhs.fixed.deref())) }
486				}
487				NumKind::GuardedFixed => {
488					DynNum { gfixed: ManuallyDrop::new($self.gfixed.deref().$func($rhs.gfixed.deref())) }
489				}
490				NumKind::NativeFloat64 => {
491					DynNum { float64: $self.float64.$func($rhs.float64) }
492				}
493				NumKind::Rational => {
494					DynNum { rational: ManuallyDrop::new($self.rational.deref().$func($rhs.rational.deref())) }
495				}
496			}
497		}
498	}
499}
500
501impl ops::Add for DynNum {
502	type Output = Self;
503	fn add(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, add) }
504}
505
506impl ops::Sub for DynNum {
507	type Output = Self;
508	fn sub(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, sub) }
509}
510
511impl ops::Mul for DynNum {
512	type Output = Self;
513	fn mul(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, mul) }
514}
515
516impl ops::Div for DynNum {
517	type Output = Self;
518	fn div(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, div) }
519}
520
521impl ops::Rem for DynNum {
522	type Output = Self;
523	fn rem(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, rem) }
524}
525
526#[test]
527fn arith_owned_owned() {
528	{
529		DynNum::set_kind(NumKind::Fixed);
530		Fixed::set_dps(2);
531		let a = DynNum::parse("123.45");
532		let b = DynNum::parse("678.90");
533		assert_eq!(a.clone() + b.clone(), DynNum::parse("802.35"));
534		assert_eq!(a.clone() - b.clone(), DynNum::parse("-555.45"));
535		assert_eq!(a.clone() * b.clone(), DynNum::parse("83810.20")); // = 83810.205 rounds to 83810.20
536		assert_eq!(a.clone() / b.clone(), DynNum::parse("0.18"));
537		assert_eq!(b.clone() % a.clone(), DynNum::parse("61.65"));
538	}
539	{
540		DynNum::set_kind(NumKind::GuardedFixed);
541		GuardedFixed::set_dps(2);
542		let a = DynNum::parse("123.45");
543		let b = DynNum::parse("678.90");
544		assert_eq!(a.clone() + b.clone(), DynNum::parse("802.35"));
545		assert_eq!(a.clone() - b.clone(), DynNum::parse("-555.45"));
546		assert_eq!(a.clone() * b.clone(), DynNum::parse("83810.205")); // Must compare to 3 d.p.s as doesn't meet FACTOR_CMP
547		assert_eq!(a.clone() / b.clone(), DynNum::parse("0.18")); // Meets FACTOR_CMP so compare only 2 d.p.s
548		assert_eq!(b.clone() % a.clone(), DynNum::parse("61.65"));
549	}
550	{
551		DynNum::set_kind(NumKind::NativeFloat64);
552		let a = DynNum::parse("123.45");
553		let b = DynNum::parse("678.90");
554		assert_eq!(a.clone() + b.clone(), DynNum { float64: NativeFloat64::from(123.45_f64 + 678.90_f64) });
555		assert_eq!(a.clone() - b.clone(), DynNum { float64: NativeFloat64::from(123.45_f64 - 678.90_f64) });
556		assert_eq!(a.clone() * b.clone(), DynNum { float64: NativeFloat64::from(123.45_f64 * 678.90_f64) });
557		assert_eq!(a.clone() / b.clone(), DynNum { float64: NativeFloat64::from(123.45_f64 / 678.90_f64) });
558		assert_eq!(b.clone() % a.clone(), DynNum { float64: NativeFloat64::from(678.90_f64 % 123.45_f64) });
559	}
560	{
561		DynNum::set_kind(NumKind::Rational);
562		let a = DynNum::parse("123.45");
563		let b = DynNum::parse("678.90");
564		assert_eq!(a.clone() + b.clone(), DynNum::parse("802.35"));
565		assert_eq!(a.clone() - b.clone(), DynNum::parse("-555.45"));
566		assert_eq!(a.clone() * b.clone(), DynNum::parse("83810.205"));
567		assert_eq!((a.clone() / b.clone()) * b.clone(), a);
568		assert_eq!(b.clone() % a.clone(), DynNum::parse("61.65"));
569	}
570}
571
572impl ops::Add<&Self> for DynNum {
573	type Output = Self;
574	fn add(self, rhs: &Self) -> Self::Output { impl_1other_wrap!(self, rhs, add) }
575}
576
577impl ops::Sub<&Self> for DynNum {
578	type Output = Self;
579	fn sub(self, rhs: &Self) -> Self::Output { impl_1other_wrap!(self, rhs, sub) }
580}
581
582impl ops::Mul<&Self> for DynNum {
583	type Output = Self;
584	fn mul(self, rhs: &Self) -> Self::Output { impl_1other_wrap!(self, rhs, mul) }
585}
586
587impl ops::Div<&Self> for DynNum {
588	type Output = Self;
589	fn div(self, rhs: &Self) -> Self::Output { impl_1other_wrap!(self, rhs, div) }
590}
591
592impl ops::Rem<&Self> for DynNum {
593	type Output = Self;
594	fn rem(self, rhs: &Self) -> Self::Output { impl_1other_wrap!(self, rhs, rem) }
595}
596
597#[test]
598fn arith_owned_ref() {
599	{
600		DynNum::set_kind(NumKind::Fixed);
601		Fixed::set_dps(2);
602		let a = DynNum::parse("123.45");
603		let b = DynNum::parse("678.90");
604		assert_eq!(a.clone() + &b, DynNum::parse("802.35"));
605		assert_eq!(a.clone() - &b, DynNum::parse("-555.45"));
606		assert_eq!(a.clone() * &b, DynNum::parse("83810.20"));
607		assert_eq!(a.clone() / &b, DynNum::parse("0.18"));
608		assert_eq!(b.clone() % &a, DynNum::parse("61.65"));
609	}
610	{
611		DynNum::set_kind(NumKind::GuardedFixed);
612		GuardedFixed::set_dps(2);
613		let a = DynNum::parse("123.45");
614		let b = DynNum::parse("678.90");
615		assert_eq!(a.clone() + &b, DynNum::parse("802.35"));
616		assert_eq!(a.clone() - &b, DynNum::parse("-555.45"));
617		assert_eq!(a.clone() * &b, DynNum::parse("83810.205"));
618		assert_eq!(a.clone() / &b, DynNum::parse("0.18"));
619		assert_eq!(b.clone() % &a, DynNum::parse("61.65"));
620	}
621	{
622		DynNum::set_kind(NumKind::NativeFloat64);
623		let a = DynNum::parse("123.45");
624		let b = DynNum::parse("678.90");
625		assert_eq!(a.clone() + &b, DynNum { float64: NativeFloat64::from(123.45_f64 + 678.90_f64) });
626		assert_eq!(a.clone() - &b, DynNum { float64: NativeFloat64::from(123.45_f64 - 678.90_f64) });
627		assert_eq!(a.clone() * &b, DynNum { float64: NativeFloat64::from(123.45_f64 * 678.90_f64) });
628		assert_eq!(a.clone() / &b, DynNum { float64: NativeFloat64::from(123.45_f64 / 678.90_f64) });
629		assert_eq!(b.clone() % &a, DynNum { float64: NativeFloat64::from(678.90_f64 % 123.45_f64) });
630	}
631	{
632		DynNum::set_kind(NumKind::Rational);
633		let a = DynNum::parse("123.45");
634		let b = DynNum::parse("678.90");
635		assert_eq!(a.clone() + &b, DynNum::parse("802.35"));
636		assert_eq!(a.clone() - &b, DynNum::parse("-555.45"));
637		assert_eq!(a.clone() * &b, DynNum::parse("83810.205"));
638		assert_eq!((a.clone() / &b) * &b, a);
639		assert_eq!(b.clone() % &a, DynNum::parse("61.65"));
640	}
641}
642
643impl ops::AddAssign for DynNum {
644	fn add_assign(&mut self, rhs: Self) { impl_1other_nowrap_mut!(self, rhs, add_assign) }
645}
646
647impl ops::SubAssign for DynNum {
648	fn sub_assign(&mut self, rhs: Self) { impl_1other_nowrap_mut!(self, rhs, sub_assign) }
649}
650
651impl ops::MulAssign for DynNum {
652	fn mul_assign(&mut self, rhs: Self) { impl_1other_nowrap_mut!(self, rhs, mul_assign) }
653}
654
655impl ops::DivAssign for DynNum {
656	fn div_assign(&mut self, rhs: Self) { impl_1other_nowrap_mut!(self, rhs, div_assign) }
657}
658
659impl ops::RemAssign for DynNum {
660	fn rem_assign(&mut self, rhs: Self) { impl_1other_nowrap_mut!(self, rhs, rem_assign) }
661}
662
663#[test]
664fn arithassign_owned() {
665	{
666		DynNum::set_kind(NumKind::Fixed);
667		Fixed::set_dps(2);
668		let a = DynNum::parse("123.45");
669		let b = DynNum::parse("678.90");
670		let mut x = a.clone(); x += b.clone(); assert_eq!(x, DynNum::parse("802.35"));
671		let mut x = a.clone(); x -= b.clone(); assert_eq!(x, DynNum::parse("-555.45"));
672		let mut x = a.clone(); x *= b.clone(); assert_eq!(x, DynNum::parse("83810.20"));
673		let mut x = a.clone(); x /= b.clone(); assert_eq!(x, DynNum::parse("0.18"));
674		let mut x = b.clone(); x %= a.clone(); assert_eq!(x, DynNum::parse("61.65"));
675	}
676	{
677		DynNum::set_kind(NumKind::GuardedFixed);
678		GuardedFixed::set_dps(2);
679		let a = DynNum::parse("123.45");
680		let b = DynNum::parse("678.90");
681		let mut x = a.clone(); x += b.clone(); assert_eq!(x, DynNum::parse("802.35"));
682		let mut x = a.clone(); x -= b.clone(); assert_eq!(x, DynNum::parse("-555.45"));
683		let mut x = a.clone(); x *= b.clone(); assert_eq!(x, DynNum::parse("83810.205"));
684		let mut x = a.clone(); x /= b.clone(); assert_eq!(x, DynNum::parse("0.18"));
685		let mut x = b.clone(); x %= a.clone(); assert_eq!(x, DynNum::parse("61.65"));
686	}
687	{
688		DynNum::set_kind(NumKind::NativeFloat64);
689		let a = DynNum::parse("123.45");
690		let b = DynNum::parse("678.90");
691		let mut x = a.clone(); x += b.clone(); assert_eq!(x, DynNum { float64: NativeFloat64::from(123.45_f64 + 678.90_f64) });
692		let mut x = a.clone(); x -= b.clone(); assert_eq!(x, DynNum { float64: NativeFloat64::from(123.45_f64 - 678.90_f64) });
693		let mut x = a.clone(); x *= b.clone(); assert_eq!(x, DynNum { float64: NativeFloat64::from(123.45_f64 * 678.90_f64) });
694		let mut x = a.clone(); x /= b.clone(); assert_eq!(x, DynNum { float64: NativeFloat64::from(123.45_f64 / 678.90_f64) });
695		let mut x = b.clone(); x %= a.clone(); assert_eq!(x, DynNum { float64: NativeFloat64::from(678.90_f64 % 123.45_f64) });
696	}
697	{
698		DynNum::set_kind(NumKind::Rational);
699		let a = DynNum::parse("123.45");
700		let b = DynNum::parse("678.90");
701		let mut x = a.clone(); x += b.clone(); assert_eq!(x, DynNum::parse("802.35"));
702		let mut x = a.clone(); x -= b.clone(); assert_eq!(x, DynNum::parse("-555.45"));
703		let mut x = a.clone(); x *= b.clone(); assert_eq!(x, DynNum::parse("83810.205"));
704		let mut x = a.clone(); x /= b.clone(); x *= &b; assert_eq!(x, a);
705		let mut x = b.clone(); x %= a.clone(); assert_eq!(x, DynNum::parse("61.65"));
706	}
707}
708
709impl ops::AddAssign<&Self> for DynNum {
710	fn add_assign(&mut self, rhs: &Self) { impl_1other_nowrap_mut!(self, rhs, add_assign) }
711}
712
713impl ops::SubAssign<&Self> for DynNum {
714	fn sub_assign(&mut self, rhs: &Self) { impl_1other_nowrap_mut!(self, rhs, sub_assign) }
715}
716
717impl ops::MulAssign<&Self> for DynNum {
718	fn mul_assign(&mut self, rhs: &Self) { impl_1other_nowrap_mut!(self, rhs, mul_assign) }
719}
720
721impl ops::DivAssign<&Self> for DynNum {
722	fn div_assign(&mut self, rhs: &Self) { impl_1other_nowrap_mut!(self, rhs, div_assign) }
723}
724
725impl ops::RemAssign<&Self> for DynNum {
726	fn rem_assign(&mut self, rhs: &Self) { impl_1other_nowrap_mut!(self, rhs, rem_assign) }
727}
728
729#[test]
730fn arithassign_ref() {
731	{
732		DynNum::set_kind(NumKind::Fixed);
733		Fixed::set_dps(2);
734		let a = DynNum::parse("123.45");
735		let b = DynNum::parse("678.90");
736		let mut x = a.clone(); x += &b; assert_eq!(x, DynNum::parse("802.35"));
737		let mut x = a.clone(); x -= &b; assert_eq!(x, DynNum::parse("-555.45"));
738		let mut x = a.clone(); x *= &b; assert_eq!(x, DynNum::parse("83810.20"));
739		let mut x = a.clone(); x /= &b; assert_eq!(x, DynNum::parse("0.18"));
740		let mut x = b.clone(); x %= &a; assert_eq!(x, DynNum::parse("61.65"));
741	}
742	{
743		DynNum::set_kind(NumKind::GuardedFixed);
744		GuardedFixed::set_dps(2);
745		let a = DynNum::parse("123.45");
746		let b = DynNum::parse("678.90");
747		let mut x = a.clone(); x += &b; assert_eq!(x, DynNum::parse("802.35"));
748		let mut x = a.clone(); x -= &b; assert_eq!(x, DynNum::parse("-555.45"));
749		let mut x = a.clone(); x *= &b; assert_eq!(x, DynNum::parse("83810.205"));
750		let mut x = a.clone(); x /= &b; assert_eq!(x, DynNum::parse("0.18"));
751		let mut x = b.clone(); x %= &a; assert_eq!(x, DynNum::parse("61.65"));
752	}
753	{
754		DynNum::set_kind(NumKind::NativeFloat64);
755		let a = DynNum::parse("123.45");
756		let b = DynNum::parse("678.90");
757		let mut x = a.clone(); x += &b; assert_eq!(x, DynNum { float64: NativeFloat64::from(123.45_f64 + 678.90_f64) });
758		let mut x = a.clone(); x -= &b; assert_eq!(x, DynNum { float64: NativeFloat64::from(123.45_f64 - 678.90_f64) });
759		let mut x = a.clone(); x *= &b; assert_eq!(x, DynNum { float64: NativeFloat64::from(123.45_f64 * 678.90_f64) });
760		let mut x = a.clone(); x /= &b; assert_eq!(x, DynNum { float64: NativeFloat64::from(123.45_f64 / 678.90_f64) });
761		let mut x = b.clone(); x %= &a; assert_eq!(x, DynNum { float64: NativeFloat64::from(678.90_f64 % 123.45_f64) });
762	}
763	{
764		DynNum::set_kind(NumKind::Rational);
765		let a = DynNum::parse("123.45");
766		let b = DynNum::parse("678.90");
767		let mut x = a.clone(); x += &b; assert_eq!(x, DynNum::parse("802.35"));
768		let mut x = a.clone(); x -= &b; assert_eq!(x, DynNum::parse("-555.45"));
769		let mut x = a.clone(); x *= &b; assert_eq!(x, DynNum::parse("83810.205"));
770		let mut x = a.clone(); x /= &b; x *= &b; assert_eq!(x, a);
771		let mut x = b.clone(); x %= &a; assert_eq!(x, DynNum::parse("61.65"));
772	}
773}
774
775impl ops::Neg for &DynNum {
776	type Output = DynNum;
777	fn neg(self) -> Self::Output { impl_0arg_wrap!(self, neg) }
778}
779
780impl ops::Add<Self> for &DynNum {
781	type Output = DynNum;
782	fn add(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, add) }
783}
784
785impl ops::Sub<Self> for &DynNum {
786	type Output = DynNum;
787	fn sub(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, sub) }
788}
789
790impl ops::Mul<Self> for &DynNum {
791	type Output = DynNum;
792	fn mul(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, mul) }
793}
794
795impl ops::Div<Self> for &DynNum {
796	type Output = DynNum;
797	fn div(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, div) }
798}
799
800impl ops::Rem<Self> for &DynNum {
801	type Output = DynNum;
802	fn rem(self, rhs: Self) -> Self::Output { impl_1other_wrap!(self, rhs, rem) }
803}
804
805#[test]
806fn arith_ref_ref() {
807	{
808		DynNum::set_kind(NumKind::Fixed);
809		Fixed::set_dps(2);
810		let a = DynNum::parse("123.45");
811		let b = DynNum::parse("678.90");
812		assert_eq!(&a + &b, DynNum::parse("802.35"));
813		assert_eq!(&a - &b, DynNum::parse("-555.45"));
814		assert_eq!(&a * &b, DynNum::parse("83810.20"));
815		assert_eq!(&a / &b, DynNum::parse("0.18"));
816		assert_eq!(&b % &a, DynNum::parse("61.65"));
817	}
818	{
819		DynNum::set_kind(NumKind::GuardedFixed);
820		GuardedFixed::set_dps(2);
821		let a = DynNum::parse("123.45");
822		let b = DynNum::parse("678.90");
823		assert_eq!(&a + &b, DynNum::parse("802.35"));
824		assert_eq!(&a - &b, DynNum::parse("-555.45"));
825		assert_eq!(&a * &b, DynNum::parse("83810.205"));
826		assert_eq!(&a / &b, DynNum::parse("0.18"));
827		assert_eq!(&b % &a, DynNum::parse("61.65"));
828	}
829	{
830		DynNum::set_kind(NumKind::NativeFloat64);
831		let a = DynNum::parse("123.45");
832		let b = DynNum::parse("678.90");
833		assert_eq!(&a + &b, DynNum { float64: NativeFloat64::from(123.45_f64 + 678.90_f64) });
834		assert_eq!(&a - &b, DynNum { float64: NativeFloat64::from(123.45_f64 - 678.90_f64) });
835		assert_eq!(&a * &b, DynNum { float64: NativeFloat64::from(123.45_f64 * 678.90_f64) });
836		assert_eq!(&a / &b, DynNum { float64: NativeFloat64::from(123.45_f64 / 678.90_f64) });
837		assert_eq!(&b % &a, DynNum { float64: NativeFloat64::from(678.90_f64 % 123.45_f64) });
838	}
839	{
840		DynNum::set_kind(NumKind::Rational);
841		let a = DynNum::parse("123.45");
842		let b = DynNum::parse("678.90");
843		assert_eq!(&a + &b, DynNum::parse("802.35"));
844		assert_eq!(&a - &b, DynNum::parse("-555.45"));
845		assert_eq!(&a * &b, DynNum::parse("83810.205"));
846		assert_eq!((&a / &b) * &b, a);
847		assert_eq!(&b % &a, DynNum::parse("61.65"));
848	}
849}
850
851/*
852impl ops::Add<&&Rational> for &Rational {
853	
854}
855
856impl ops::Sub<&&Rational> for &Rational {
857	
858}
859
860impl ops::Mul<&&Rational> for &Rational {
861	
862}
863
864impl ops::Div<&&Rational> for &Rational {
865	
866}
867
868impl ops::Rem<&&Rational> for &Rational {
869	
870}
871*/
Contact (issues, pull requests, etc.) at git@yingtongli.me. Generated by cgit.