-
Notifications
You must be signed in to change notification settings - Fork 14
/
quantity.h
1425 lines (1185 loc) · 61.2 KB
/
quantity.h
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
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#pragma once
#ifndef SYDEVS_QUANTITY_H_
#define SYDEVS_QUANTITY_H_
#include <sydevs/core/units.h>
#include <cmath>
#include <ostream>
namespace sydevs {
constexpr int64 quantity_limit = int64(1)*1000*1000*1000*1000*1000; ///< The maximum precision multiplier, plus 1.
/**
* @brief A base class for quantities, defining operations that do not depend
* on the dimension.
*/
class quantity_base
{
public:
constexpr bool valid() const; ///< Returns `true` if the `quantity` value is valid.
constexpr bool finite() const; ///< Returns `true` if the `quantity` value is finite.
constexpr int64 multiplier() const; ///< Returns the number that multiples the length precision.
constexpr scale precision() const; ///< Returns the length precision.
constexpr bool fixed() const; ///< Returns `true` if the `quantity` value has a fixed length precision.
protected:
constexpr quantity_base();
explicit constexpr quantity_base(int64 multiplier);
constexpr quantity_base(int64 multiplier, scale precision);
constexpr quantity_base(int64 multiplier, scale precision, bool fixed);
constexpr quantity_base(scale precision, float64 multiplier, int8 fixed);
constexpr quantity_base(const quantity_base&) = default; ///< Copy constructor
quantity_base& operator=(const quantity_base&) = default; ///< Copy assignment
quantity_base(quantity_base&&) = default; ///< Move constructor
quantity_base& operator=(quantity_base&&) = default; ///< Move assignment
~quantity_base() = default; ///< Destructor
static constexpr float64 inf_float64 = std::numeric_limits<float64>::infinity();
static constexpr float64 nan_float64 = std::numeric_limits<float64>::quiet_NaN();
static constexpr int64 inf_int64 = std::numeric_limits<int64>::max();
static constexpr int64 nan_int64 = std::numeric_limits<int64>::min();
static constexpr int64 constexpr_abs(int64 n);
static constexpr float64 constexpr_abs(float64 x);
static constexpr float64 convert_multiplier(int64 multiplier);
static constexpr int64 convert_level(int64 multiplier, int64 level);
static constexpr float64 offset_multiplier(float64 multiplier);
static constexpr int64 truncate_multiplier(float64 multiplier);
static constexpr int64 round_multiplier(float64 multiplier);
static constexpr int64 scale_multiplier(float64 multiplier, float64 factor);
float64 multiplier_;
scale precision_;
int8 fixed_;
};
/**
* @brief A data type template which represents a dimensioned quantity as a
* multiple of a precision level, where the dimensions are supplied by
* a [units](@ref sydevs::units) template parameter and the precision
* level is specified using a value of type [scale](@ref sydevs::scale).
*
* @details
* A `quantity` value allows for the exact specification, addition, and
* subtraction of dimensioned quantities provided that the following conditions
* are met:
*
* - Quantities can be represented as an integer multiple of a precision level.
* - The "multiplier", the number which multiplies the precision, is less than
* 1,000,000,000,000,000 in magnitude (negative quantities are permitted).
*
* A set of `constexpr` user-defined literals provide a means to construct
* `quantity` values, as shown below.
*
* ~~~
* 5_m // 5 meters
* 5_fs // 5 femtoseconds
* 5_um // 5 micrometers
* 6_kg // 6 kilograms
* 6000_Mg // 6000 Megagrams
* ~~~
*
* These user-defined literals correspond with the 7 Standard International (SI)
* base units as represented by [units](@ref sydevs::units) (`_g`, `_m`, `_s`,
* `_A`, `_K`, `_mol`, `_cd`), and may also incorporate an SI prefix (`y`, `z`,
* `a`, `f`, `n`, `p`, `u`, `m`, `k`, `M`, `G`, `T`, `P`, `E`, `Z`, `Y`). Note
* one difference from the SI standard: the gram (`1_g`) is used as the base
* unit for mass, instead of the kilogram (`1_kg`) as in the standard.
*
* There are also 4 additional user-defined literals that produce
* duration-valued quantities with a precision of 1 second and a multiplier
* scaled up by 60 (`?_min`), 60`*`60 (`?_hr`), 24`*`60`*`60 (`?_day`) or
* 365`*`24`*`60`*`60 (`?_yr`). Examples of these literals are below.
*
* ~~~
* 5_min // 300 seconds (5 minutes)
* 7_day // 604,800 seconds (1 week)
* ~~~
*
* Quantity values can also be created by directly invoking one of the three
* constructors: the default constructor producing an invalid quanitity, the
* single-argument constructor producing a quantity with a precision of one base
* unit, and the two-argument constructor where both the multiplier and
* precision level are supplied.
*
* ~~~
* quantity<grams>() // invalid mass quantity
* quantity<grams>(3) // 3 grams
* quantity<meters>(3) // 3 meters
* quantity<seconds>(3, nano) // 3 nanoseconds
* quantity<decltype(_m/_s)>(3) // 3 meters/second
* quantity<decltype(_g*_m/_s/_s)>(3, kilo) // 3 Newtons
* ~~~
*
* Type aliases are provided for quantities of the 7 SI base units.
*
* ~~~
* mass // quantity<grams>
* distance // quantity<meters>
* duration // quantity<seconds>
* electric_current // quantity<amperes>
* thermodynamic_temperature // quantity<kelvins>
* amount_of_substance // quantity<moles>
* luminous_intensity // quantity<candelas>
* ~~~
*
* These can be used in place of `quantity<...>` for quantities based on SI
* base units.
*
* ~~~
* mass() // invalid mass quantity
* mass(3) // 3 grams
* distance(3) // 3 meters
* duration(3, nano) // 3 nanoseconds
* ~~~
*
* One way to construct quantities based on SI derived units is to multiply or
* divide a `quantity` value on the left with a `units` value on the right.
*
* ~~~
* -777_kg*_m/_s/_s // -777 Newtons
* 90_km/_hr // 25 meters per second (90 kilometers per hour)
* ~~~
*
* Also, multiplying or dividing quantities yields a new quantity with different
* units.
*
* ~~~
* 200_m/8_s // 25 meters per second
* ~~~
*
* To remove the units from a quantity, simply divide by the unit. The result is
* a dimensionless quantity value (`quantity<no_units>`) that can be coerced
* into a floating-point number.
*
* ~~~
* 1500_mA/_A // 1.5
* ~~~
*
* One can construct infinite quantities, as well as the maximum representable
* quantity at a given precision level.
*
* ~~~
* duration::inf() // Infinite duration
* mass::max(yocto) // Maximum mass at ys precision (999999999999999_yg)
* ~~~
*
* Precision is a core aspect of `quantity` types. To illustrate, although
* `(7000_us == 7_ms)` evaluates to `true`, the two values are different. The
* left-hand side is stored as a muliplier of 7000 and a time precision of
* microseconds, whereas the right-hand side is stored as a multiplier of 7 and
* a time precision of milliseconds.
*
* By default, `quantity` values resulting from arithmetic operations have
* their precision levels automatically adjusted to minimize rounding error.
* This default behaviour treats `quantity` values as base-1000 floating-point
* numbers. The following examples all involve time values.
*
* ~~~
* 3_s + 475_ms // Result: 3475 milliseconds
* 1_ks + 1_us // Result: 1000000001 microseconds
* 500_ps - 1_ns // Result: -500 picoseconds
* (1.0/3.0)*1_s // Result: 333333333333333 femtoseconds
* (1.0/3.0)*1000_ms // Result: 333333333333333 femtoseconds
* 1000_ms/3.0 // Result: 333333333333333 femtoseconds
* 10_ms/250_us // Result: 40.0
* ~~~
*
* However, using the `fixed_at` member function, one can produce quantities
* with fixed precision levels that remain unchanged through operations, as
* illustrated below. This is in contrast to the `rescaled` member function,
* which neither fixes nor unfixes the precision level.
*
* ~~~
* (1_min + 40_s).fixed_at(micro)/8.0 // Result: 12500000 microseconds
* (1_min + 40_s).fixed_at(milli)/8.0 // Result: 12500 milliseconds
* (1_min + 40_s).fixed_at(unit)/8.0 // Result: 13 seconds
* (1_min + 40_s).rescaled(micro)/8.0 // Result: 12500000 microseconds
* (1_min + 40_s).rescaled(milli)/8.0 // Result: 12500 milliseconds
* (1_min + 40_s).rescaled(unit)/8.0 // Result: 12500 milliseconds
* ~~~
*
* Fixing the precision level prevents round-off errors when adding or
* subtracting quantities. On the other hand, precision may be lost as in the
* above example where 12500 milliseconds is rounded to 13 seconds.
*
* Operations between `quantity` values fixed at different precision levels
* result in invalid quantities.
*
* ~~~
* ((1_s).fixed_at(milli) + (1_s).fixed_at(micro)).valid() // Result: false
* ~~~
*
* However if two `quantity` values have different precision levels but only
* one is fixed, then the fixed precision will be adopted by the result.
*
* ~~~
* ((1_s).fixed_at(micro) + (1_s).rescaled(nano)).precision() // Result: micro
* ~~~
*
* A `quantity` value can be modified using the `+=`, `-=`, `*=`, `/=`
* operators. One must be aware that the member functions `fixed_at`,
* `rescaled`, `refined`, `coarsened`, and `unfixed` return new `quantity`
* values but leave the original unchanged.
*
* A `quantity` value may be output or converted to a string using the
* `operator<<` overload.
*/
template<typename U>
class quantity : public quantity_base
{
template<typename U_> friend class quantity;
public:
/**
* @brief Constructs an invalid `quantity` value.
*/
constexpr quantity();
/**
* @brief Constructs a `quantity` value representing the specified number
* of units with no prefix (i.e. milli, kilo).
*
* @details
* The constructed `quantity` value has the specified `multiplier` and a
* precision level of `unit`. The precision does not remain fixed through
* operations.
*
* @param multiplier The number that mulitplies the default precision level
* to yield the represented quantity.
*/
explicit constexpr quantity(int64 multiplier);
/**
* @brief Constructs a `quantity` value representing the specified multiple
* of the specified precision level.
*
* @details
* The constructed `quantity` value has the specified `multiplier` and
* `precision`. The precision does not remain fixed through operations.
*
* @param multiplier The number that mulitplies the specified precision
* level to yield the represented quantity.
* @param precision The precision level that is multiplied to yield the
* represented quantity.
*/
constexpr quantity(int64 multiplier, scale precision);
constexpr quantity(const quantity&) = default; ///< Copy constructor
quantity& operator=(const quantity&) = default; ///< Copy assignment
quantity(quantity&&) = default; ///< Move constructor
quantity& operator=(quantity&&) = default; ///< Move assignment
~quantity() = default; ///< Destructor
static constexpr quantity inf();
static constexpr quantity max(scale precision);
constexpr const quantity fixed_at(scale precision) const; ///< Returns a new `quantity` value with the length precision changed and fixed.
constexpr const quantity rescaled(scale precision) const; ///< Returns a new `quantity` value with the length precision changed but neither fixed nor unfixed.
constexpr const quantity refined() const; ///< Returns a new `quantity` value with the length precision minimized without losing precision.
constexpr const quantity coarsened() const; ///< Returns a new `quantity` value with the length precision maximized without losing precision.
constexpr const quantity unfixed() const; ///< Returns a new `quantity` value with the length precision unfixed.
quantity& operator+=(quantity rhs); ///< Adds `rhs` to the `quantity` value.
quantity& operator-=(quantity rhs); ///< Subtracts `rhs` from the `quantity` value.
quantity& operator*=(float64 rhs); ///< Multiplies the `quantity` value by `rhs`.
quantity& operator/=(float64 rhs); ///< Divides the `quantity` value by `rhs`.
constexpr const quantity operator+() const; ///< Returns a copy of the `quantity` value.
constexpr const quantity operator-() const; ///< Returns the negation of the `quantity` value.
constexpr const quantity operator+(quantity rhs) const; ///< Returns a new `quantity` value with `rhs` added.
constexpr const quantity operator-(quantity rhs) const; ///< Returns a new `quantity` value with `rhs` subtracted.
constexpr const quantity operator*(float64 rhs) const; ///< Returns a new `quantity` value multiplied by `rhs`.
constexpr const quantity operator/(float64 rhs) const; ///< Returns a new `quantity` value divided by `rhs`.
template<typename U_>
constexpr quantity<decltype(U()*U_())> operator*(quantity<U_> rhs) const; ///< Returns the `quantity` value multiplied by `rhs`.
template<typename U_>
constexpr quantity<decltype(U()/U_())> operator/(quantity<U_> rhs) const; ///< Returns the `quantity` value divided by `rhs`.
template<typename U_>
constexpr quantity<decltype(U()*U_())> operator*(U_ rhs) const; ///< Returns the `quantity` value multiplied by `rhs`.
template<typename U_>
constexpr quantity<decltype(U()/U_())> operator/(U_ rhs) const; ///< Returns the `quantity` value divided by `rhs`.
constexpr bool operator==(quantity rhs) const; ///< Returns `true` if the `quantity` value equals `rhs`.
constexpr bool operator!=(quantity rhs) const; ///< Returns `true` if the `quantity` value does not equal `rhs`.
constexpr bool operator<(quantity rhs) const; ///< Returns `true` if the `quantity` value is less than `rhs`.
constexpr bool operator>(quantity rhs) const; ///< Returns `true` if the `quantity` value is greater than `rhs`.
constexpr bool operator<=(quantity rhs) const; ///< Returns `true` if the `quantity` value is at most `rhs`.
constexpr bool operator>=(quantity rhs) const; ///< Returns `true` if the `quantity` value is at least `rhs`.
private:
constexpr quantity(int64 multiplier, scale precision, bool fixed);
constexpr quantity(scale precision, float64 multiplier, int8 fixed);
constexpr quantity autoscaled() const;
constexpr quantity autorounded() const;
};
/**
* @brief A speciaulization to facilitate the conversion of dimensionless
* quantities into floats.
*/
template<>
class quantity<no_units> : public quantity_base
{
template<typename U_> friend class quantity;
public:
constexpr quantity();
explicit constexpr quantity(int64 multiplier);
constexpr quantity(int64 multiplier, scale precision);
constexpr quantity(const quantity&) = default; ///< Copy constructor
quantity& operator=(const quantity&) = default; ///< Copy assignment
quantity(quantity&&) = default; ///< Move constructor
quantity& operator=(quantity&&) = default; ///< Move assignment
~quantity() = default; ///< Destructor
static constexpr quantity inf();
static constexpr quantity max(scale precision);
constexpr const quantity fixed_at(scale precision) const; ///< Returns a new `quantity` value with the length precision changed and fixed.
constexpr const quantity rescaled(scale precision) const; ///< Returns a new `quantity` value with the length precision changed but neither fixed nor unfixed.
constexpr const quantity refined() const; ///< Returns a new `quantity` value with the length precision minimized without losing precision.
constexpr const quantity coarsened() const; ///< Returns a new `quantity` value with the length precision maximized without losing precision.
constexpr const quantity unfixed() const; ///< Returns a new `quantity` value with the length precision unfixed.
constexpr const quantity operator+() const; ///< Returns a copy of the `quantity` value.
constexpr const quantity operator-() const; ///< Returns the negation of the `quantity` value.
constexpr const quantity operator+(quantity rhs) const; ///< Returns a new `quantity` value with `rhs` added.
constexpr const quantity operator-(quantity rhs) const; ///< Returns a new `quantity` value with `rhs` subtracted.
constexpr const quantity operator*(float64 rhs) const; ///< Returns a new `quantity` value multiplied by `rhs`.
constexpr const quantity operator/(float64 rhs) const; ///< Returns a new `quantity` value divided by `rhs`.
template<typename U_>
constexpr quantity<U_> operator*(quantity<U_> rhs) const; ///< Returns the `quantity` value multiplied by `rhs`.
template<typename U_>
constexpr quantity<decltype(_1/U_())> operator/(quantity<U_> rhs) const; ///< Returns the `quantity` value divided by `rhs`.
template<typename U_>
constexpr quantity<U_> operator*(U_ rhs) const; ///< Returns the `quantity` value multiplied by `rhs`.
template<typename U_>
constexpr quantity<decltype(_1/U_())> operator/(U_ rhs) const; ///< Returns the `quantity` value divided by `rhs`.
constexpr operator float64() const; ///< Returns a floating-point representation of the `quantity` value.
private:
constexpr quantity(int64 multiplier, scale precision, bool fixed);
constexpr quantity(scale precision, float64 multiplier, int8 fixed);
constexpr quantity autoscaled() const;
constexpr quantity autorounded() const;
};
constexpr bool quantity_base::valid() const
{
return multiplier_ == multiplier_;
}
constexpr bool quantity_base::finite() const
{
return multiplier_ < inf_float64 && multiplier_ > -inf_float64;
}
constexpr int64 quantity_base::multiplier() const
{
return !valid() ? nan_int64 :
multiplier_ == inf_float64 ? inf_int64 :
multiplier_ == -inf_float64 ? -inf_int64 :
int64(multiplier_);
}
constexpr scale quantity_base::precision() const
{
return precision_;
}
constexpr bool quantity_base::fixed() const
{
return fixed_ == 1;
}
constexpr quantity_base::quantity_base()
: multiplier_(nan_float64)
, precision_(unit)
, fixed_(0)
{
}
constexpr quantity_base::quantity_base(int64 multiplier)
: multiplier_(convert_multiplier(multiplier))
, precision_(unit)
, fixed_(0)
{
}
constexpr quantity_base::quantity_base(int64 multiplier, scale precision)
: multiplier_(convert_multiplier(multiplier))
, precision_(convert_level(multiplier, precision.level()))
, fixed_(0)
{
}
constexpr quantity_base::quantity_base(int64 multiplier, scale precision, bool fixed)
: multiplier_(convert_multiplier(multiplier))
, precision_(convert_level(multiplier, precision.level()))
, fixed_(fixed)
{
}
constexpr quantity_base::quantity_base(scale precision, float64 multiplier, int8 fixed)
: multiplier_(multiplier)
, precision_(precision)
, fixed_(fixed)
{
}
constexpr int64 quantity_base::constexpr_abs(int64 n)
{
return n < 0 ? -n : n;
}
constexpr float64 quantity_base::constexpr_abs(float64 x)
{
return x < 0 ? -x : x;
}
constexpr float64 quantity_base::convert_multiplier(int64 multiplier)
{
return multiplier == nan_int64 ? nan_float64 :
multiplier >= quantity_limit ? inf_float64 :
multiplier <= -quantity_limit ? -inf_float64 :
float64(multiplier);
}
constexpr int64 quantity_base::convert_level(int64 multiplier, int64 level)
{
return multiplier == nan_int64 || constexpr_abs(multiplier) >= quantity_limit ? 0 :
level;
}
constexpr float64 quantity_base::offset_multiplier(float64 multiplier)
{
return multiplier >= 0 ? multiplier + 0.5 :
multiplier - 0.5;
}
constexpr int64 quantity_base::truncate_multiplier(float64 multiplier)
{
return multiplier >= quantity_limit ? inf_int64 :
multiplier <= -quantity_limit ? -inf_int64 :
int64(multiplier);
}
constexpr int64 quantity_base::round_multiplier(float64 multiplier)
{
return truncate_multiplier(offset_multiplier(multiplier));
}
constexpr int64 quantity_base::scale_multiplier(float64 multiplier, float64 factor)
{
return factor >= 1 ? truncate_multiplier(offset_multiplier(factor*multiplier)) :
truncate_multiplier(factor*offset_multiplier(multiplier));
}
template<typename U>
constexpr quantity<U>::quantity()
: quantity_base()
{
}
constexpr quantity<no_units>::quantity()
: quantity_base()
{
}
template<typename U>
constexpr quantity<U>::quantity(int64 multiplier)
: quantity_base(multiplier)
{
}
constexpr quantity<no_units>::quantity(int64 multiplier)
: quantity_base(multiplier)
{
}
template<typename U>
constexpr quantity<U>::quantity(int64 multiplier, scale precision)
: quantity_base(multiplier, precision)
{
}
constexpr quantity<no_units>::quantity(int64 multiplier, scale precision)
: quantity_base(multiplier, precision)
{
}
template<typename U>
constexpr quantity<U>::quantity(int64 multiplier, scale precision, bool fixed)
: quantity_base(multiplier, precision, fixed)
{
}
constexpr quantity<no_units>::quantity(int64 multiplier, scale precision, bool fixed)
: quantity_base(multiplier, precision, fixed)
{
}
template<typename U>
constexpr quantity<U>::quantity(scale precision, float64 multiplier, int8 fixed)
: quantity_base(precision, multiplier, fixed)
{
}
constexpr quantity<no_units>::quantity(scale precision, float64 multiplier, int8 fixed)
: quantity_base(precision, multiplier, fixed)
{
}
template<typename U>
constexpr quantity<U> quantity<U>::inf()
{
return quantity<U>(quantity_limit);
}
constexpr quantity<no_units> quantity<no_units>::inf()
{
return quantity<no_units>(quantity_limit);
}
template<typename U>
constexpr quantity<U> quantity<U>::max(scale precision)
{
return quantity<U>(quantity_limit - 1, precision);
}
constexpr quantity<no_units> quantity<no_units>::max(scale precision)
{
return quantity<no_units>(quantity_limit - 1, precision);
}
template<typename U>
constexpr const quantity<U> quantity<U>::fixed_at(scale precision) const
{
return !valid() ? quantity<U>() :
quantity<U>(scale_multiplier(multiplier_, precision_/precision), precision, int8(1));
}
constexpr const quantity<no_units> quantity<no_units>::fixed_at(scale precision) const
{
return !valid() ? quantity<no_units>() :
quantity<no_units>(scale_multiplier(multiplier_, precision_/precision), precision, int8(1));
}
template<typename U>
constexpr const quantity<U> quantity<U>::rescaled(scale precision) const
{
return !valid() ? quantity<U>() :
quantity<U>(scale_multiplier(multiplier_, precision_/precision), precision, fixed_ == 1);
}
constexpr const quantity<no_units> quantity<no_units>::rescaled(scale precision) const
{
return !valid() ? quantity<no_units>() :
quantity<no_units>(scale_multiplier(multiplier_, precision_/precision), precision, fixed_ == 1);
}
template<typename U>
constexpr const quantity<U> quantity<U>::refined() const
{
return !valid() ? quantity<U>() :
!finite() ? *this :
multiplier_ == 0 ? quantity<U>(0) :
constexpr_abs(round_multiplier(1000.0*multiplier_)) >= quantity_limit ? *this :
quantity<U>(precision_ - 1, 1000.0*multiplier_, int8(0)).autorounded().refined();
}
constexpr const quantity<no_units> quantity<no_units>::refined() const
{
return !valid() ? quantity<no_units>() :
!finite() ? *this :
multiplier_ == 0 ? quantity<no_units>(0) :
constexpr_abs(round_multiplier(1000.0*multiplier_)) >= quantity_limit ? *this :
quantity<no_units>(precision_ - 1, 1000.0*multiplier_, int8(0)).autorounded().refined();
}
template<typename U>
constexpr const quantity<U> quantity<U>::coarsened() const
{
return !valid() ? quantity<U>() :
!finite() ? *this :
multiplier_ == 0 ? quantity<U>(0) :
multiplier_ != 1000.0*round_multiplier(0.001*multiplier_) ? *this :
quantity<U>(precision_ + 1, float64(round_multiplier(0.001*multiplier_)), int8(0)).coarsened();
}
constexpr const quantity<no_units> quantity<no_units>::coarsened() const
{
return !valid() ? quantity<no_units>() :
!finite() ? *this :
multiplier_ == 0 ? quantity<no_units>(0) :
multiplier_ != 1000.0*round_multiplier(0.001*multiplier_) ? *this :
quantity<no_units>(precision_ + 1, float64(round_multiplier(0.001*multiplier_)), int8(0)).coarsened();
}
template<typename U>
constexpr const quantity<U> quantity<U>::unfixed() const
{
return quantity<U>(precision_, multiplier_, int8(0));
}
constexpr const quantity<no_units> quantity<no_units>::unfixed() const
{
return quantity<no_units>(precision_, multiplier_, int8(0));
}
template<typename U>
quantity<U>& quantity<U>::operator+=(quantity<U> rhs)
{
*this = *this + rhs;
return *this;
}
template<typename U>
quantity<U>& quantity<U>::operator-=(quantity<U> rhs)
{
*this = *this - rhs;
return *this;
}
template<typename U>
quantity<U>& quantity<U>::operator*=(float64 rhs)
{
*this = *this*rhs;
return *this;
}
template<typename U>
quantity<U>& quantity<U>::operator/=(float64 rhs)
{
*this = *this/rhs;
return *this;
}
template<typename U>
constexpr const quantity<U> quantity<U>::operator+() const
{
return quantity<U>(precision_, multiplier_, fixed_);
}
constexpr const quantity<no_units> quantity<no_units>::operator+() const
{
return quantity<no_units>(precision_, multiplier_, fixed_);
}
template<typename U>
constexpr const quantity<U> quantity<U>::operator-() const
{
return quantity<U>(precision_, -multiplier_, fixed_);
}
constexpr const quantity<no_units> quantity<no_units>::operator-() const
{
return quantity<no_units>(precision_, -multiplier_, fixed_);
}
template<typename U>
constexpr const quantity<U> quantity<U>::operator+(quantity rhs) const
{
return fixed_ && rhs.fixed_ ? precision_ == rhs.precision_ ? quantity<U>(precision_, multiplier_ + rhs.multiplier_, int8(1)).autorounded() :
quantity<U>() :
fixed_ ? quantity<U>(precision_, multiplier_ + (rhs.precision_/precision_)*rhs.multiplier_, int8(1)).autorounded() :
rhs.fixed_ ? quantity<U>(rhs.precision_, (precision_/rhs.precision_)*multiplier_ + rhs.multiplier_, int8(1)).autorounded() :
precision_ <= rhs.precision_ ? quantity<U>(precision_, multiplier_ + (rhs.precision_/precision_)*rhs.multiplier_, int8(0)).autoscaled() :
quantity<U>(rhs.precision_, (precision_/rhs.precision_)*multiplier_ + rhs.multiplier_, int8(0)).autoscaled();
}
constexpr const quantity<no_units> quantity<no_units>::operator+(quantity rhs) const
{
return fixed_ && rhs.fixed_ ? precision_ == rhs.precision_ ? quantity<no_units>(precision_, multiplier_ + rhs.multiplier_, int8(1)).autorounded() :
quantity<no_units>() :
fixed_ ? quantity<no_units>(precision_, multiplier_ + (rhs.precision_/precision_)*rhs.multiplier_, int8(1)).autorounded() :
rhs.fixed_ ? quantity<no_units>(rhs.precision_, (precision_/rhs.precision_)*multiplier_ + rhs.multiplier_, int8(1)).autorounded() :
precision_ <= rhs.precision_ ? quantity<no_units>(precision_, multiplier_ + (rhs.precision_/precision_)*rhs.multiplier_, int8(0)).autoscaled() :
quantity<no_units>(rhs.precision_, (precision_/rhs.precision_)*multiplier_ + rhs.multiplier_, int8(0)).autoscaled();
}
template<typename U>
constexpr const quantity<U> quantity<U>::operator-(quantity rhs) const
{
return fixed_ && rhs.fixed_ ? precision_ == rhs.precision_ ? quantity<U>(precision_, multiplier_ - rhs.multiplier_, int8(1)).autorounded() :
quantity<U>() :
fixed_ ? quantity<U>(precision_, multiplier_ - (rhs.precision_/precision_)*rhs.multiplier_, int8(1)).autorounded() :
rhs.fixed_ ? quantity<U>(rhs.precision_, (precision_/rhs.precision_)*multiplier_ - rhs.multiplier_, int8(1)).autorounded() :
precision_ <= rhs.precision_ ? quantity<U>(precision_, multiplier_ - (rhs.precision_/precision_)*rhs.multiplier_, int8(0)).autoscaled() :
quantity<U>(rhs.precision_, (precision_/rhs.precision_)*multiplier_ - rhs.multiplier_, int8(0)).autoscaled();
}
constexpr const quantity<no_units> quantity<no_units>::operator-(quantity rhs) const
{
return fixed_ && rhs.fixed_ ? precision_ == rhs.precision_ ? quantity<no_units>(precision_, multiplier_ - rhs.multiplier_, int8(1)).autorounded() :
quantity<no_units>() :
fixed_ ? quantity<no_units>(precision_, multiplier_ - (rhs.precision_/precision_)*rhs.multiplier_, int8(1)).autorounded() :
rhs.fixed_ ? quantity<no_units>(rhs.precision_, (precision_/rhs.precision_)*multiplier_ - rhs.multiplier_, int8(1)).autorounded() :
precision_ <= rhs.precision_ ? quantity<no_units>(precision_, multiplier_ - (rhs.precision_/precision_)*rhs.multiplier_, int8(0)).autoscaled() :
quantity<no_units>(rhs.precision_, (precision_/rhs.precision_)*multiplier_ - rhs.multiplier_, int8(0)).autoscaled();
}
template<typename U>
constexpr const quantity<U> quantity<U>::operator*(float64 rhs) const
{
return fixed_ ? quantity<U>(precision_, multiplier_*rhs, int8(1)).autorounded() :
quantity<U>(precision_, multiplier_*rhs, int8(0)).autoscaled();
}
constexpr const quantity<no_units> quantity<no_units>::operator*(float64 rhs) const
{
return fixed_ ? quantity<no_units>(precision_, multiplier_*rhs, int8(1)).autorounded() :
quantity<no_units>(precision_, multiplier_*rhs, int8(0)).autoscaled();
}
template<typename U>
constexpr const quantity<U> quantity<U>::operator/(float64 rhs) const
{
return fixed_ ? quantity<U>(precision_, multiplier_/rhs, int8(1)).autorounded() :
quantity<U>(precision_, multiplier_/rhs, int8(0)).autoscaled();
}
constexpr const quantity<no_units> quantity<no_units>::operator/(float64 rhs) const
{
return fixed_ ? quantity<no_units>(precision_, multiplier_/rhs, int8(1)).autorounded() :
quantity<no_units>(precision_, multiplier_/rhs, int8(0)).autoscaled();
}
template<typename U>
template<typename U_>
constexpr quantity<decltype(U()*U_())> quantity<U>::operator*(quantity<U_> rhs) const
{
return quantity<decltype(U()*U_())>(precision_ + rhs.precision().level(), multiplier_*rhs.multiplier(), int8(0)).autoscaled();
}
template<typename U_>
constexpr quantity<U_> quantity<no_units>::operator*(quantity<U_> rhs) const
{
return quantity<U_>(precision_ + rhs.precision().level(), multiplier_*rhs.multiplier(), int8(0)).autoscaled();
}
template<typename U>
template<typename U_>
constexpr quantity<decltype(U()/U_())> quantity<U>::operator/(quantity<U_> rhs) const
{
return !rhs.valid() ? quantity<decltype(U()/U_())>() :
quantity<decltype(U()/U_())>(precision_ - rhs.precision().level(), multiplier_/rhs.multiplier(), int8(0)).autoscaled();
}
template<typename U_>
constexpr quantity<decltype(_1/U_())> quantity<no_units>::operator/(quantity<U_> rhs) const
{
return !rhs.valid() ? quantity<decltype(_1/U_())>() :
quantity<decltype(_1/U_())>(precision_ - rhs.precision().level(), multiplier_/rhs.multiplier(), int8(0)).autoscaled();
}
template<typename U>
template<typename U_>
constexpr quantity<decltype(U()*U_())> quantity<U>::operator*(U_ rhs) const
{
return quantity<decltype(U()*U_())>(precision_, multiplier_, int8(0));
}
template<typename U_>
constexpr quantity<U_> quantity<no_units>::operator*(U_ rhs) const
{
return quantity<U_>(precision_, multiplier_, int8(0));
}
template<typename U>
template<typename U_>
constexpr quantity<decltype(U()/U_())> quantity<U>::operator/(U_ rhs) const
{
return quantity<decltype(U()/U_())>(precision_, multiplier_, int8(0));
}
template<typename U_>
constexpr quantity<decltype(_1/U_())> quantity<no_units>::operator/(U_ rhs) const
{
return quantity<decltype(_1/U_())>(precision_, multiplier_, int8(0));
}
template<typename U>
constexpr bool quantity<U>::operator==(quantity<U> rhs) const
{
return precision_ <= rhs.precision_ ? multiplier_ == (rhs.precision_/precision_)*rhs.multiplier_ :
(precision_/rhs.precision_)*multiplier_ == rhs.multiplier_;
}
template<typename U>
constexpr bool quantity<U>::operator!=(quantity<U> rhs) const
{
return precision_ <= rhs.precision_ ? multiplier_ != (rhs.precision_/precision_)*rhs.multiplier_ :
(precision_/rhs.precision_)*multiplier_ != rhs.multiplier_;
}
template<typename U>
constexpr bool quantity<U>::operator<(quantity<U> rhs) const
{
return precision_ <= rhs.precision_ ? multiplier_ < (rhs.precision_/precision_)*rhs.multiplier_ :
(precision_/rhs.precision_)*multiplier_ < rhs.multiplier_;
}
template<typename U>
constexpr bool quantity<U>::operator>(quantity<U> rhs) const
{
return precision_ <= rhs.precision_ ? multiplier_ > (rhs.precision_/precision_)*rhs.multiplier_ :
(precision_/rhs.precision_)*multiplier_ > rhs.multiplier_;
}
template<typename U>
constexpr bool quantity<U>::operator<=(quantity<U> rhs) const
{
return precision_ <= rhs.precision_ ? multiplier_ <= (rhs.precision_/precision_)*rhs.multiplier_ :
(precision_/rhs.precision_)*multiplier_ <= rhs.multiplier_;
}
template<typename U>
constexpr bool quantity<U>::operator>=(quantity<U> rhs) const
{
return precision_ <= rhs.precision_ ? multiplier_ >= (rhs.precision_/precision_)*rhs.multiplier_ :
(precision_/rhs.precision_)*multiplier_ >= rhs.multiplier_;
}
template<typename U>
constexpr quantity<U> quantity<U>::autoscaled() const
{
return !valid() ? quantity<U>() :
multiplier_ == inf_float64 ? quantity<U>::inf() :
multiplier_ == -inf_float64 ? -quantity<U>::inf() :
constexpr_abs(multiplier_) + 0.5 >= quantity_limit ? quantity<U>(precision_ + 1, 0.001*multiplier_, int8(0)).autoscaled() :
1000.0*constexpr_abs(multiplier_) + 0.5 >= quantity_limit ? autorounded() :
multiplier_ == int64(multiplier_) ? autorounded() :
quantity<U>(precision_ - 1, 1000.0*multiplier_, int8(0)).autoscaled();
}
constexpr quantity<no_units> quantity<no_units>::autoscaled() const
{
return !valid() ? quantity<no_units>() :
multiplier_ == inf_float64 ? quantity<no_units>::inf() :
multiplier_ == -inf_float64 ? -quantity<no_units>::inf() :
constexpr_abs(multiplier_) + 0.5 >= quantity_limit ? quantity<no_units>(precision_ + 1, 0.001*multiplier_, int8(0)).autoscaled() :
1000.0*constexpr_abs(multiplier_) + 0.5 >= quantity_limit ? autorounded() :
multiplier_ == int64(multiplier_) ? autorounded() :
quantity<no_units>(precision_ - 1, 1000.0*multiplier_, int8(0)).autoscaled();
}
template<typename U>
constexpr quantity<U> quantity<U>::autorounded() const
{
return !valid() ? quantity<U>() :
multiplier_ + 0.5 >= quantity_limit ? quantity<U>::inf() :
multiplier_ - 0.5 <= -quantity_limit ? -quantity<U>::inf() :
quantity(int64(offset_multiplier(multiplier_)), precision_, fixed_ == 1);
}
constexpr quantity<no_units> quantity<no_units>::autorounded() const
{
return !valid() ? quantity<no_units>() :
multiplier_ + 0.5 >= quantity_limit ? quantity<no_units>::inf() :
multiplier_ - 0.5 <= -quantity_limit ? -quantity<no_units>::inf() :
quantity(int64(offset_multiplier(multiplier_)), precision_, fixed_ == 1);
}
constexpr quantity<no_units>::operator float64() const
{
return precision_ == unit ? multiplier_ :
precision_ < unit ? 0.001*float64(quantity<no_units>(precision_ + 1, multiplier_, int8(0))) :
1000.0*float64(quantity<no_units>(precision_ - 1, multiplier_, int8(0)));
}
template<typename U>
constexpr const quantity<U> operator*(float64 lhs, quantity<U> rhs)
{
return rhs*lhs;
}
template<typename U>
constexpr const quantity<decltype(_1/U())> operator/(float64 lhs, quantity<U> rhs)
{
return (quantity<no_units>(1)*lhs)/rhs;