-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtunits.hpp
1480 lines (1323 loc) · 54.3 KB
/
rtunits.hpp
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
/*
* Copyright (c) 2019, Ben Barsdell. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// RT-Units - A single-header library for working with physical quantities
// at runtime. Includes support for parsing and serialization.
// TODO: Consider support for ratios as dimensions using 1 extra bit per dim.
// E.g., sr == m**2 / m**2 => dim[m] = 2, is_ratio[m] = true.
// cos(m/m) -> m/m
// TODO: Consider a C API with functions like:
// int32_t units_power(const char* units, int8_t power,
// uint8_t result_size, char* result);
// int32_t units_multiply(const char* lhs_units, const char* rhs_units,
// uint8_t result_size, char* result);
// TODO: Try to make Quantity() constexpr.
// TODO: Add support for rational exponents.
// This is useful for some intermediate results, as well as some unusual
// units.
// Can probably use high bits of dimensions as the denominator, allowing
// values up to 7.
// Note that parsing will need to support brackets around exponents, as
// well as both "3/2" and "1.5" for exponent values.
#pragma once
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
// Note: boost::regex is 4-8x faster than std::regex for units parsing.
#if RTUNITS_USE_BOOST_REGEX
#include <boost/regex.hpp>
#else
#include <regex>
#endif
// This macro and the associated helper class come from here:
// https://gist.github.com/oliora/928424f7675d58fadf49c70fdba70d2f
// When evaluated at compile time emits a compilation error if condition is not
// true. Invokes the standard assert at run time.
#define CONSTEXPR_ASSERT(cond) \
((void)((cond) ? 0 \
: (detail::constexpr_assert_failed([]() { assert(!#cond); }), \
0)))
#ifndef RTUNITS_USE_EXCEPTIONS
#define RTUNITS_USE_EXCEPTIONS 1
#endif
#if RTUNITS_USE_EXCEPTIONS
#include <stdexcept>
#define UNITS_ASSERT(pred, exception) ((pred) ? (void)0 : (throw exception))
#define UNITS_THROW(exception) (throw exception)
#else
#define UNITS_ASSERT(pred, exception) CONSTEXPR_ASSERT(pred)
#define UNITS_THROW(exception) ((void)0)
#endif // RTUNITS_USE_EXCEPTIONS
#if __cplusplus >= 201402L
#define CONSTEXPR_IF_CPP_2014 constexpr
#else
#define CONSTEXPR_IF_CPP_2014
#endif
namespace rtunits {
namespace detail {
template <class Assert>
inline void constexpr_assert_failed(Assert&& a) noexcept {
std::forward<Assert>(a)();
}
} // end namespace detail
#if RTUNITS_USE_EXCEPTIONS
struct DimensionError : public std::runtime_error {
DimensionError(const std::string& text) : std::runtime_error(text) {}
};
struct QuantityError : public std::runtime_error {
QuantityError(const std::string& text) : std::runtime_error(text) {}
};
struct QuantityParseError : public QuantityError {
QuantityParseError(const std::string& text) : QuantityError(text) {}
};
#endif
// Forward declarations.
class Dimensions;
inline constexpr Dimensions operator*(const Dimensions& lhs,
const Dimensions& rhs);
inline constexpr Dimensions operator/(const Dimensions& lhs,
const Dimensions& rhs);
class Dimensions {
public:
enum BaseDimension : uint8_t {
kLength,
kMass,
kTime,
kCurrent,
kTemperature,
kLuminosity,
kAmount,
kBaseDimensionCount,
};
static constexpr Dimensions None() { return Dimensions(); }
static constexpr Dimensions Length() { return Dimensions(1); }
static constexpr Dimensions Mass() { return Dimensions(0, 1); }
static constexpr Dimensions Time() { return Dimensions(0, 0, 1); }
static constexpr Dimensions Current() { return Dimensions(0, 0, 0, 1); }
static constexpr Dimensions Temperature() {
return Dimensions(0, 0, 0, 0, 1);
}
static constexpr Dimensions Luminosity() {
return Dimensions(0, 0, 0, 0, 0, 1);
}
static constexpr Dimensions Amount() {
return Dimensions(0, 0, 0, 0, 0, 0, 1);
}
static constexpr Dimensions Area() { return Length().squared(); }
static constexpr Dimensions Volume() { return Length().cubed(); }
static constexpr Dimensions Density() { return Mass() / Volume(); }
static constexpr Dimensions Frequency() { return Time().reciprocal(); }
static constexpr Dimensions Speed() { return Length() / Time(); }
static constexpr Dimensions Acceleration() {
return Length() / Time().squared();
}
static constexpr Dimensions Energy() { return Mass() * Speed().squared(); }
static constexpr Dimensions Power() { return Energy() / Time(); }
static constexpr Dimensions Force() { return Mass() * Acceleration(); }
static constexpr Dimensions Momentum() { return Mass() * Speed(); }
static constexpr Dimensions Pressure() { return Force() / Area(); }
static constexpr Dimensions Voltage() { return Power() / Current(); }
static constexpr Dimensions Resistance() { return Voltage() / Current(); }
static constexpr Dimensions Charge() { return Current() * Time(); }
static constexpr Dimensions Capacitance() { return Charge() / Voltage(); }
static constexpr Dimensions Inductance() { return Resistance() * Time(); }
typedef int8_t value_type;
typedef std::array<value_type, kBaseDimensionCount> array_type;
typedef std::tuple<value_type, value_type, value_type, value_type, value_type,
value_type, value_type>
tuple_type;
constexpr Dimensions() : exponents_() {}
explicit Dimensions(BaseDimension dim) : Dimensions() { exponents_[dim] = 1; }
constexpr const array_type& exponents() const { return exponents_; }
// These constexpr methods technically require C++14, but GCC 5.3 compiles
// them under C++11 mode without issue.
constexpr bool operator==(const Dimensions& rhs) const {
#if __cplusplus > 201703L
return exponents_ == rhs.exponents_;
#endif
return as_tuple() == rhs.as_tuple();
}
constexpr bool operator!=(const Dimensions& rhs) const {
#if __cplusplus > 201703L
return exponents_ != rhs.exponents_;
#endif
return as_tuple() != rhs.as_tuple();
}
constexpr bool operator<(const Dimensions& rhs) const {
#if __cplusplus > 201703L
return exponents_ < rhs.exponents_;
#endif
return as_tuple() < rhs.as_tuple();
}
constexpr bool operator<=(const Dimensions& rhs) const {
return *this == rhs || *this < rhs;
}
constexpr bool operator>(const Dimensions& rhs) const {
return !(*this <= rhs);
}
constexpr bool operator>=(const Dimensions& rhs) const {
return !(*this < rhs);
}
// Test if this has any dimensions.
explicit constexpr operator bool() const {
return length() || mass() || time() || current() || temperature() ||
luminosity() || amount();
}
// Test if this is a base unit (has exactly 1 unit set).
constexpr bool is_base() const {
return bool(length()) + bool(mass()) + bool(time()) + bool(current()) +
bool(temperature()) + bool(luminosity()) + bool(amount()) ==
1;
}
CONSTEXPR_IF_CPP_2014 Dimensions& operator*=(const Dimensions& rhs) {
return *this = multiplied_by(rhs);
}
CONSTEXPR_IF_CPP_2014 Dimensions& operator/=(const Dimensions& rhs) {
return *this = divided_by(rhs);
}
constexpr Dimensions multiplied_by(const Dimensions& rhs) const {
return Dimensions(
static_cast<value_type>(length() + rhs.length()),
static_cast<value_type>(mass() + rhs.mass()),
static_cast<value_type>(time() + rhs.time()),
static_cast<value_type>(current() + rhs.current()),
static_cast<value_type>(temperature() + rhs.temperature()),
static_cast<value_type>(luminosity() + rhs.luminosity()),
static_cast<value_type>(amount() + rhs.amount()));
}
constexpr Dimensions divided_by(const Dimensions& rhs) const {
return Dimensions(
static_cast<value_type>(length() - rhs.length()),
static_cast<value_type>(mass() - rhs.mass()),
static_cast<value_type>(time() - rhs.time()),
static_cast<value_type>(current() - rhs.current()),
static_cast<value_type>(temperature() - rhs.temperature()),
static_cast<value_type>(luminosity() - rhs.luminosity()),
static_cast<value_type>(amount() - rhs.amount()));
}
constexpr Dimensions power(value_type n) const {
return Dimensions(static_cast<value_type>(length() * n),
static_cast<value_type>(mass() * n),
static_cast<value_type>(time() * n),
static_cast<value_type>(current() * n),
static_cast<value_type>(temperature() * n),
static_cast<value_type>(luminosity() * n),
static_cast<value_type>(amount() * n));
}
constexpr Dimensions reciprocal() const { return power(-1); }
constexpr Dimensions squared() const { return power(2); }
constexpr Dimensions cubed() const { return power(3); }
constexpr Dimensions fractional_power(value_type d) const {
return UNITS_ASSERT(exponents_divisible_by(d),
DimensionError("Cannot take root-" + std::to_string(d) +
" of Dimensions " + to_string(true))),
Dimensions(static_cast<value_type>(length() / d),
static_cast<value_type>(mass() / d),
static_cast<value_type>(time() / d),
static_cast<value_type>(current() / d),
static_cast<value_type>(temperature() / d),
static_cast<value_type>(luminosity() / d),
static_cast<value_type>(amount() / d));
}
constexpr Dimensions sqrt() const { return fractional_power(2); }
constexpr Dimensions cbrt() const { return fractional_power(3); }
constexpr const value_type& length() const { return exponents_[kLength]; }
constexpr const value_type& mass() const { return exponents_[kMass]; }
constexpr const value_type& time() const { return exponents_[kTime]; }
constexpr const value_type& current() const { return exponents_[kCurrent]; }
constexpr const value_type& temperature() const {
return exponents_[kTemperature];
}
constexpr const value_type& luminosity() const {
return exponents_[kLuminosity];
}
constexpr const value_type& amount() const { return exponents_[kAmount]; }
std::string to_string(bool write_if_none = false) const {
return to_string({"L", "M", "T", "I", "\u03F4" /*capital Theta*/, "J", "N"},
write_if_none);
}
typedef std::array<const char*, kBaseDimensionCount> SymbolArray;
std::string to_string(const SymbolArray& symbols,
bool write_if_none = false) const {
std::stringstream stream;
auto write_unit = [&stream](const std::string& symbol, int exponent,
bool with_space) {
if (exponent) {
if (with_space) {
stream << " ";
}
stream << symbol;
if (exponent != 1) {
stream << "^" << exponent;
}
return true;
}
return false;
};
bool wrote = false;
wrote |= write_unit(symbols[kLength], exponents_[kLength], wrote);
wrote |= write_unit(symbols[kMass], exponents_[kMass], wrote);
wrote |= write_unit(symbols[kTime], exponents_[kTime], wrote);
wrote |= write_unit(symbols[kCurrent], exponents_[kCurrent], wrote);
wrote |= write_unit(symbols[kTemperature], exponents_[kTemperature], wrote);
wrote |= write_unit(symbols[kLuminosity], exponents_[kLuminosity], wrote);
wrote |= write_unit(symbols[kAmount], exponents_[kAmount], wrote);
if (!wrote && write_if_none) {
stream << "<none>";
}
return stream.str();
}
private:
constexpr tuple_type as_tuple() const {
return std::make_tuple(length(), mass(), time(), current(), temperature(),
luminosity(), amount());
}
constexpr bool exponents_divisible_by(value_type n) const {
return length() % n == 0 && mass() % n == 0 && time() % n == 0 &&
current() % n == 0 && temperature() % n == 0 &&
luminosity() % n == 0 && amount() % n == 0;
}
explicit constexpr Dimensions(const value_type& length_,
const value_type& mass_ = 0,
const value_type& time_ = 0,
const value_type& current_ = 0,
const value_type& temperature_ = 0,
const value_type& luminosity_ = 0,
const value_type& amount_ = 0)
: exponents_({length_, mass_, time_, current_, temperature_, luminosity_,
amount_}) {}
void _war_unused_private_field_warning() const { (void)padding_byte_; }
array_type exponents_;
value_type padding_byte_ = 0;
};
inline constexpr Dimensions operator*(const Dimensions& lhs,
const Dimensions& rhs) {
return lhs.multiplied_by(rhs);
}
inline constexpr Dimensions operator/(const Dimensions& lhs,
const Dimensions& rhs) {
return lhs.divided_by(rhs);
}
inline constexpr Dimensions pow(const Dimensions& dims,
Dimensions::value_type exponent) {
return dims.power(exponent);
}
inline constexpr Dimensions sqrt(const Dimensions& dims) { return dims.sqrt(); }
inline constexpr Dimensions cbrt(const Dimensions& dims) { return dims.cbrt(); }
inline std::ostream& operator<<(std::ostream& stream, const Dimensions& dims) {
stream << dims.to_string();
return stream;
}
static_assert(std::is_standard_layout<Dimensions>::value,
"Internal error: Dimensions is not standard layout");
static_assert(sizeof(Dimensions) == 8,
"Internal error: Dimensions is not exactly 8 bytes");
template <typename T>
class Quantity {
public:
typedef T value_type;
static value_type pi() {
return T(
3.14159265358979323846264338327950288419716939937510582097494459230781641L);
}
static value_type tau() { return T(2) * pi(); }
// SI base units.
static Quantity number() { return Quantity(Dimensions::None()); }
static Quantity meter() { return Quantity(Dimensions::Length()); }
static Quantity kilogram() { return Quantity(Dimensions::Mass()); }
static Quantity second() { return Quantity(Dimensions::Time()); }
static Quantity ampere() { return Quantity(Dimensions::Current()); }
static Quantity kelvin() { return Quantity(Dimensions::Temperature()); }
static Quantity candela() { return Quantity(Dimensions::Luminosity()); }
static Quantity mole() { return Quantity(Dimensions::Amount()); }
// SI derived units.
static Quantity hertz() { return second().reciprocal(); }
static Quantity radian() { return meter() / meter(); }
static Quantity steradian() { return radian().squared(); }
static Quantity newton() { return kilogram() * meter() / second().squared(); }
static Quantity pascal() { return newton() / meter().squared(); }
static Quantity joule() { return newton() * meter(); }
static Quantity watt() { return joule() / second(); }
static Quantity coulomb() { return ampere() * second(); }
static Quantity volt() { return watt() / ampere(); }
static Quantity farad() { return coulomb() / volt(); }
static Quantity ohm() { return volt() / ampere(); }
static Quantity siemens() { return ohm().reciprocal(); }
static Quantity weber() { return joule() / ampere(); }
static Quantity tesla() { return weber() / meter().squared(); }
static Quantity henry() { return ohm() * second(); }
static Quantity degree_celsius() { return kelvin(); }
static Quantity lumen() { return candela() / steradian(); }
static Quantity lux() { return lumen() / meter().squared(); }
static Quantity becquerel() { return second().reciprocal(); }
static Quantity gray() { return joule() / kilogram(); }
static Quantity sievert() { return joule() / kilogram(); }
static Quantity katal() { return mole() / second(); }
// SI accepted units.
static Quantity minute() { return T(60) * second(); }
static Quantity hour() { return T(60) * minute(); }
static Quantity day() { return T(24) * hour(); }
static Quantity astronomical_unit() { return T(149597870700ll) * meter(); }
static Quantity degree() { return pi() / T(180) * radian(); }
static Quantity arc_minute() { return degree() / T(60); }
static Quantity arc_second() { return arc_minute() / T(60); }
static Quantity hour_angle() { return T(360 / 24) * degree(); }
static Quantity hectare() { return T(1e4) * meter().squared(); }
static Quantity liter() { return T(1e-3) * meter().cubed(); }
static Quantity metric_tonne() { return T(1e3) * kilogram(); }
static Quantity dalton() { return T(1.66053906660e-27) * kilogram(); }
static Quantity electronvolt() { return T(1.602176634e-19) * joule(); }
static Quantity speed_of_light() { return T(299792458) * meter() / second(); }
static Quantity reduced_planck_constant() {
return T(1.054571817e-34) * joule() * second();
}
static Quantity electron_mass() { return T(9.1093837015e-31) * kilogram(); }
static Quantity elementary_charge() { return T(1.602176634e-19) * coulomb(); }
static Quantity planck_time() { return T(5.391247e-44) * second(); }
static Quantity bohr_radius() { return T(5.29177210903e-11) * meter(); }
static Quantity hartree_energy() { return T(4.3597447222071e-18) * joule(); }
// Unofficial units.
static Quantity angstrom() { return T(1e-10) * meter(); }
static Quantity are() { return T(100) * meter().squared(); }
static Quantity barn() { return T(1e-28) * meter().squared(); }
static Quantity bar() { return T(1e5) * pascal(); }
static Quantity atmosphere() { return T(101325) * pascal(); }
static Quantity barye() { return T(0.1) * pascal(); }
static Quantity millimeter_of_mercury() {
return T(133.322387415) * pascal();
}
static Quantity conventional_mercury() {
return T(13595.1) * standard_gravity() * kilogram() / meter().cubed();
}
static Quantity torr() { return atmosphere() / T(760); }
static Quantity year() { return T(365.25) * day(); }
// CGS units.
static Quantity gal() { return T(1e-2) * meter() / second().squared(); }
static Quantity dyne() { return T(1e-5) * newton(); }
static Quantity erg() { return T(1e-7) * joule(); }
static Quantity poise() { return T(0.1) * pascal() * second(); }
static Quantity stokes() { return T(1e-4) * meter().squared() / second(); }
static Quantity gauss() { return T(1e-4) * tesla(); }
static Quantity statcoulomb() { return T(3.33564e-10) * coulomb(); }
static Quantity franklin() { return statcoulomb(); }
static Quantity statvolt() { return T(299.792458) * volt(); }
static Quantity oersted() { return T(79.57747) * ampere() / meter(); }
static Quantity phot() { return T(1e4) * lux(); }
static Quantity rad() { return T(1e-2) * gray(); }
static Quantity roentgen_equivalent_man() { return T(1e-2) * sievert(); }
static Quantity maxwell() { return T(1e-8) * weber(); }
static Quantity debye() {
return T(1e-21) * coulomb() * meter().squared() / second() /
speed_of_light();
}
// Misc units.
static Quantity week() { return T(7) * day(); }
static Quantity pounds_per_square_inch() { return T(6.894757e3) * pascal(); }
static Quantity thermochemical_calorie() { return T(4.184) * joule(); }
static Quantity nutritional_calorie() { return T(4.184e3) * joule(); }
static Quantity curie() { return T(3.7e10) * becquerel(); }
static Quantity dobson_unit() {
return T(0.4462e-3) * mole() / meter().squared();
}
static Quantity furlong() { return T(201.168) * meter(); }
static Quantity horsepower() { return T(745.7) * watt(); }
static Quantity roentgen() {
return T(2.58) * ampere() * second() / kilogram();
}
static Quantity rack_unit() { return T(44.45e-3) * meter(); }
static Quantity turn() { return T(2) * pi() * radian(); }
static Quantity british_thermal_units() {
return T(1.05505585262e3) * joule();
}
static Quantity tnt() {
return T(1e9) * thermochemical_calorie() / metric_tonne();
}
static Quantity quad() { return T(1e15) * british_thermal_units(); }
static Quantity foe() { return T(1e51) * erg(); }
static Quantity crab() { return T(2.4e-11) * watt() / meter().squared(); }
static Quantity langley() {
return T(1e4) * thermochemical_calorie() / meter().squared();
}
static Quantity milli_earth_rate_unit() {
return T(0.015) * degree() / hour();
}
static Quantity meter_water_equivalent() {
return T(1e3) * kilogram() / meter().squared();
}
static Quantity strontium_unit() { return T(1.065e-12) * gray(); }
static Quantity banana_equivalent_dose() { return T(1e-7) * sievert(); }
static Quantity furman() { return turn() / T(65536.); }
static Quantity gradian() { return turn() / T(400.); }
static Quantity shake() { return T(1e-8) * second(); }
static Quantity mars_day() { return T(88775.244/*14688*/) * second(); }
static Quantity galactic_year() { return T(250e6) * year(); }
static Quantity kermit() { return T(24 * 60 * 60 / 100.) * second(); }
static Quantity moment() { return T(90) * second(); }
static Quantity ton_of_refrigeration() {
return T(12e3) * british_thermal_units() / hour();
}
static Quantity revolutions_per_minute() { return turn() / minute(); }
static Quantity counts_per_second() { return second().reciprocal(); }
static Quantity pound() { return T(0.45359237) * kilogram(); }
static Quantity pound_of_force() { return T(4.448222) * newton(); }
static Quantity tex() { return T(1e-6) * kilogram() / meter(); }
static Quantity denier() { return tex() / T(9); }
static Quantity rutherford() { return T(1e6) * becquerel(); }
static Quantity eon() { return T(1e9) * year(); }
static Quantity inch() { return T(25.4e-3) * meter(); }
static Quantity foot() { return T(12) * inch(); }
static Quantity yard() { return T(3) * foot(); }
static Quantity mile() { return T(1760) * yard(); }
static Quantity square() { return T(100.) * foot().squared(); }
static Quantity standard_gravity() {
return T(9.806650) * meter() / second().squared();
}
static Quantity vacuum_permeability() {
return T(1.25663706212e-6) * newton() / ampere().squared();
}
static Quantity vacuum_permittivity() {
return (vacuum_permeability() * speed_of_light().squared()).reciprocal();
}
static Quantity vacuum_impedance() {
return vacuum_permeability() * speed_of_light();
}
static Quantity gravitational_constant() {
return T(6.67430e-11) * meter().cubed() / kilogram() / second().squared();
}
static Quantity avogadro_number() { return T(6.02214076e23) / mole(); }
static Quantity boltzmann_constant() {
return T(1.380649e-23) * joule() / kelvin();
}
static Quantity stefan_boltzmann_constant() {
return T(5.670374419e-8) * watt() / meter().squared() /
kelvin().squared().squared();
}
static Quantity neutron_mass() { return T(1.67492749804e-27) * kilogram(); }
static Quantity proton_mass() { return T(1.67262192369e-27) * kilogram(); }
// Astronomy units.
static Quantity solar_mass() { return T(1.98847e30) * kilogram(); }
static Quantity earth_mass() { return T(5.9722e24) * kilogram(); }
static Quantity jupiter_mass() { return T(1.898130e27) * kilogram(); }
static Quantity solar_radius() { return T(6.957e8) * meter(); }
static Quantity earth_radius() { return T(6.3781e6) * meter(); }
static Quantity jupiter_radius() { return T(7.1492e7) * meter(); }
static Quantity light_year() { return T(9460730472580800) * meter(); }
static Quantity parsec() { return T(648000) / pi() * astronomical_unit(); }
static Quantity lunar_distance() { return T(3.84402e8) * meter(); }
static Quantity solar_luminosity() { return T(3.939e26) * watt(); }
static Quantity jansky() {
return T(1e-26) * watt() / meter().squared() / hertz();
}
static Quantity rydberg() { return T(13.605693122994) * electronvolt(); }
static Quantity rayleigh() {
return T(1e10) / (T(4) * pi()) /
(meter().squared() * second() * steradian());
}
static Quantity snr() { return number(); } // Signal-to-noise ratio
static Quantity slug() { return T(14.59390) * kilogram(); }
typedef std::unordered_map<std::string, Quantity> symbol_map_type;
// TODO: Consider adding long-form names.
//{"meter", meter()},
//{"metre", meter()},
// These form a self-consistent grammar where any unit can be combined with
// any prefix without colliding with another unit name.
static const symbol_map_type& unit_symbol_map() {
static const symbol_map_type units = {
// SI base units.
{"m", meter()},
{"g", T(1e-3) * kilogram()},
{"s", second()},
{"A", ampere()},
{"K", kelvin()},
{"cd", candela()},
{"mol", mole()},
// SI derived units.
{"Hz", hertz()},
{"rad", radian()},
{"sr", steradian()},
{"N", newton()},
{"Pa", pascal()},
{"J", joule()},
{"W", watt()},
{"C", coulomb()},
{"V", volt()},
{"F", farad()},
{"ohm", ohm()},
{"S", siemens()},
{"Wb", weber()},
{"T", tesla()},
{"H", henry()},
{"degC", degree_celsius()},
{"\u00B0" // degree symbol
"C",
degree_celsius()},
{"\u2103", degree_celsius()}, // degree Celsius symbol
{"lm", lumen()},
{"lx", lux()},
{"Bq", becquerel()},
{"Gy", gray()},
{"Sv", sievert()},
{"kat", katal()},
// SI acceptable units.
{"min", minute()},
{"h", hour()},
{"day", day()}, // Can't use "d" because of candela vs. centi-days.
{"deg", degree()},
{"\u00B0", degree()}, // degree symbol
{"'", arc_minute()},
{"\u2032", arc_minute()}, // prime symbol
{"arcmin", arc_minute()},
{"\"", arc_second()},
{"\u2033", arc_second()}, // double prime symbol
{"arcsec", arc_second()},
{"mas", milli() * arc_second()},
{"\u00B5" // micro (mu) symbol
"as", micro() * arc_second()},
{"ha", hectare()},
{"L", liter()},
{"l", liter()},
{"t", metric_tonne()},
{"AU", astronomical_unit()},
{"eV", electronvolt()},
{"Da", dalton()},
{"u", dalton()},
{"c_0", speed_of_light()},
{"h_", reduced_planck_constant()},
{"m_e", electron_mass()},
{"e", elementary_charge()},
{"a_0", bohr_radius()},
{"E_h", hartree_energy()},
// Unofficial units.
{"Angstrom", angstrom()},
{"\u212B", angstrom()}, // angstrom symbol
{"\u00C5", angstrom()}, // letter A with circle over it
{"are", are()},
{"b", barn()},
{"bar", bar()},
{"atm", atmosphere()},
{"Ba", barye()},
{"Hg", conventional_mercury()},
{"mmHg", millimeter_of_mercury()},
{"Torr", torr()},
{"year", year()},
{"yr", year()},
// Astronomy units.
#if !RTUNITS_NO_ASTRONOMY_UNITS
{"Msun", solar_mass()},
{"M_S", solar_mass()},
{"M\u2609", solar_mass()}, // circle with dot in it
{"solMass", solar_mass()},
{"Mearth", earth_mass()},
{"M_E", earth_mass()},
{"M\u2295", earth_mass()}, // circle with plus in it
{"Mjupiter", jupiter_mass()},
{"M_J", jupiter_mass()},
{"Rsun", solar_radius()},
{"R_S", solar_radius()},
{"R\u2609", solar_radius()}, // circle with dot in it
{"solRad", solar_radius()},
{"Rearth", earth_radius()},
{"R_E", earth_radius()},
{"R\u2295", earth_radius()}, // circle with plus in it
{"Rjupiter", jupiter_radius()},
{"R_J", jupiter_radius()},
{"ly", light_year()},
{"lyr", light_year()},
{"pc", parsec()},
{"LD", lunar_distance()},
{"Lsun", solar_luminosity()},
{"solLum", solar_luminosity()},
{"Jy", jansky()},
{"Ry", rydberg()},
{"SNR", snr()},
{"S/N", snr()},
#endif // !RTUNITS_NO_ASTRONOMY_UNITS
#if !RTUNITS_NO_CGS_UNITS
// CGS units.
{"Gal", gal()},
{"dyn", dyne()},
{"erg", erg()},
{"P", poise()},
{"St", stokes()},
{"gauss", gauss()},
{"Fr", franklin()},
{"esu", franklin()},
{"statV", statvolt()},
{"Oe", oersted()},
{"phot", phot()},
{"rd", rad()},
{"rem", roentgen_equivalent_man()},
{"Mx", maxwell()},
{"D", debye()},
#endif // !RTUNITS_NO_CGS_UNITS
#if !RTUNITS_NO_MISC_UNITS
// Misc units.
{"wk", week()},
{"psi", pounds_per_square_inch()},
{"cal", thermochemical_calorie()},
{"Cal", nutritional_calorie()},
{"Ci", curie()},
{"DU", dobson_unit()},
{"fur", furlong()},
{"hp", horsepower()},
//{"R", roentgen()}, // Not supported due to Rayleigh using R
{"R", rayleigh()},
{"U", rack_unit()},
{"tr", turn()},
{"rev", turn()},
{"cyc", turn()},
{"BTU", british_thermal_units()},
{"Btu", british_thermal_units()},
{"TNT", tnt()},
{"Q", quad()},
{"foe", foe()},
{"Crab", crab()},
{"Ly", langley()},
{"MERU", milli_earth_rate_unit()},
{"mwe", meter_water_equivalent()},
{"SU", strontium_unit()},
{"BED", banana_equivalent_dose()},
{"furman", furman()},
{"gradian", gradian()},
{"gon", gradian()},
{"shake", shake()},
{"sol", mars_day()},
{"GY", galactic_year()},
{"Kermit", kermit()},
{"moment", moment()},
{"TOR", ton_of_refrigeration()},
{"rpm", revolutions_per_minute()},
{"cps", counts_per_second()},
{"lb", pound()},
{"lbf", pound_of_force()},
{"tex", tex()},
{"D", denier()},
{"Rd", rutherford()},
{"eon", eon()},
{"inch", inch()},
{"foot", foot()},
{"yard", yard()},
{"yd", yard()},
{"mile", mile()},
{"mi", mile()},
{"square", square()},
{"g_0", standard_gravity()},
{"mu_0", vacuum_permeability()},
{"epsilon_0", vacuum_permittivity()},
{"Z_0", vacuum_impedance()},
{"N_A", avogadro_number()},
{"k", boltzmann_constant()},
{"\u03C3", stefan_boltzmann_constant()}, // lowercase sigma
{"m_n", neutron_mass()},
{"m_p", proton_mass()},
{"G", gravitational_constant()},
{"slug", slug()},
#endif // !RTUNITS_NO_MISC_UNITS
};
return units;
}
// SI prefixes.
static value_type quetta() { return T(1e30); }
static value_type ronna() { return T(1e27); }
static value_type yotta() { return T(1e24); }
static value_type zetta() { return T(1e21); }
static value_type exa() { return T(1e18); }
static value_type peta() { return T(1e15); }
static value_type tera() { return T(1e12); }
static value_type giga() { return T(1e9); }
static value_type mega() { return T(1e6); }
static value_type kilo() { return T(1e3); }
static value_type hecto() { return T(1e2); }
static value_type deka() { return T(1e1); }
static value_type deci() { return T(1e-1); }
static value_type centi() { return T(1e-2); }
static value_type milli() { return T(1e-3); }
static value_type micro() { return T(1e-6); }
static value_type nano() { return T(1e-9); }
static value_type pico() { return T(1e-12); }
static value_type femto() { return T(1e-15); }
static value_type atto() { return T(1e-18); }
static value_type zepto() { return T(1e-21); }
static value_type yocto() { return T(1e-24); }
static value_type ronto() { return T(1e-27); }
static value_type quecto() { return T(1e-30); }
// Binary prefixes.
static value_type kibi() { return T(1024); }
static value_type mebi() { return T(1024 * kibi()); }
static value_type gibi() { return T(1024 * mebi()); }
static value_type tebi() { return T(1024 * gibi()); }
static value_type pebi() { return T(1024 * tebi()); }
static value_type exbi() { return T(1024 * pebi()); }
static value_type zebi() { return T(1024 * exbi()); }
static value_type yobi() { return T(1024 * zebi()); }
// TODO: Consider making these value_type instead of Quantity.
static const symbol_map_type& prefix_symbol_map() {
static const symbol_map_type prefixes = {
{"Q", quetta()}, {"R", ronna()}, {"Y", yotta()}, {"Z", zetta()},
{"E", exa()}, {"P", peta()}, {"T", tera()}, {"G", giga()},
{"M", mega()}, {"k", kilo()}, {"h", hecto()}, {"da", deka()},
{"d", deci()}, {"c", centi()}, {"m", milli()}, {"u", micro()},
{"\u00B5", micro()}, {"n", nano()}, {"p", pico()}, {"f", femto()},
{"a", atto()}, {"z", zepto()}, {"y", yocto()}, {"r", ronto()},
{"q", quecto()}, {"Ki", kibi()}, {"Mi", mebi()}, {"Gi", gibi()},
{"Ti", tebi()}, {"Pi", pebi()}, {"Ei", exbi()}, {"Zi", zebi()},
{"Yi", yobi()},
};
return prefixes;
}
Quantity() : value_(), dims_() {}
// Allow implicit conversion from value_type (dimensionless).
Quantity(const value_type& value) : value_(value), dims_() {}
// Allow implicit conversion from Dimensions (unit magnitude).
Quantity(const Dimensions& dims) : value_(1), dims_(dims) {}
Quantity(const value_type& si_value, const Dimensions& dims)
: value_(si_value), dims_(dims) {}
explicit Quantity(const std::string& units) {
const bool units_parsed_successfully = parse_units(units, this);
UNITS_ASSERT(units_parsed_successfully,
QuantityError("Failed to parse units: " + units));
(void)units_parsed_successfully;
}
explicit Quantity(const char* units) {
const bool units_parsed_successfully = parse_units(units, this);
UNITS_ASSERT(units_parsed_successfully,
QuantityError("Failed to parse units: " + std::string(units)));
(void)units_parsed_successfully;
}
Quantity(const value_type& value, const std::string& units)
: Quantity(value) {
Quantity q;
bool units_parsed_successfully = parse_units(units, &q);
UNITS_ASSERT(units_parsed_successfully,
QuantityError("Failed to parse units: " + units));
(void)units_parsed_successfully;
*this *= q;
}
Quantity(const value_type& value, const char* units)
: Quantity(value) {
Quantity q;
bool units_parsed_successfully = parse_units(units, &q);
UNITS_ASSERT(units_parsed_successfully,
QuantityError("Failed to parse units: " + std::string(units)));
(void)units_parsed_successfully;
*this *= q;
}
// Returns the value of the quantity in SI units.
// Fun fact: This can't be named "si_value" because POSIX defines a macro with
// the same name (in fact it reserves any use of the prefix "si_").
const value_type& value_si() const { return value_; }
// Returns the value of the quantity in the given units.
value_type value(const Quantity& desired_units) const {
UNITS_ASSERT(
desired_units.dimensions() == dimensions(),
QuantityError("Dimension mismatch: " + dimensions().to_string(true) +
" vs. " + desired_units.dimensions().to_string(true)));
return static_cast<value_type>(*this / desired_units);
}
value_type value(const std::string& desired_units) const {
return value(Quantity(desired_units));
}
value_type value(const char* desired_units) const {
return value(Quantity(desired_units));
}
#if __cplusplus >= 201402L
[[deprecated("Use .value_si() instead.")]]
#endif
const value_type&
magnitude() const {
return value();
}
#if __cplusplus >= 201402L
[[deprecated("Use .value() instead.")]]
#endif
const value_type&
magnitude(const Quantity& desired_units) const {
return value(desired_units);
}
#if __cplusplus >= 201402L
[[deprecated("Use .value() instead.")]]
#endif
value_type magnitude(const std::string& desired_units) const {
return magnitude(Quantity(desired_units));
}
const Dimensions& dimensions() const { return dims_; }
// Test if this has a non-zero value.
explicit constexpr operator bool() const { return value_ != value_type(0); }
bool operator==(const Quantity& rhs) const {
return dims_ == rhs.dims_ && value_ == rhs.value_;
}
bool operator!=(const Quantity& rhs) const {
return dims_ != rhs.dims_ || value_ != rhs.value_;
}
bool operator<(const Quantity& rhs) const {
// TODO: Factor out this assertion into a helper method.
UNITS_ASSERT(
rhs.dimensions() == dimensions(),
QuantityError("Dimension mismatch: " + dimensions().to_string(true) +
" vs. " + rhs.dimensions().to_string(true)));
return value_ < rhs.value_;
}
bool operator<=(const Quantity& rhs) const {
return *this == rhs || *this < rhs;
}
bool operator>(const Quantity& rhs) const { return !(*this <= rhs); }
bool operator>=(const Quantity& rhs) const { return !(*this < rhs); }
Quantity operator-() const { return Quantity(-value_, dims_); }
Quantity operator+() const { return Quantity(+value_, dims_); }
Quantity& operator*=(const Quantity& rhs) {
dims_ *= rhs.dims_;
value_ *= rhs.value_;
return *this;
}
Quantity& operator/=(const Quantity& rhs) {