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