-
Notifications
You must be signed in to change notification settings - Fork 1
/
units.h
1182 lines (991 loc) · 40 KB
/
units.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
/**
* MIT License
*
* Copyright (c) 2016-2017 David Brown
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <numeric>
#include <ratio>
#include <stdexcept>
#include <type_traits>
#include <cmath>
#include <cstdint>
#ifndef UNIT_DISABLE_IOSTREAM
#include <iostream>
#endif
namespace units
{
template <typename Rep, typename Ratio, typename UnitType>
struct unit;
template <typename T>
struct is_unit : std::false_type
{
};
template <typename Rep, typename Ratio, typename UnitType>
struct is_unit<unit<Rep, Ratio, UnitType>> : std::true_type
{
};
namespace detail
{
template <intmax_t Numerator>
struct integer_sign : std::integral_constant<intmax_t, (Numerator < 0) ? -1 : 1>
{
};
template <intmax_t Numerator>
struct integer_abs : std::integral_constant<intmax_t, Numerator * integer_sign<Numerator>::value>
{
};
template <intmax_t Numerator, intmax_t Quotient>
struct greatest_common_divisor;
template <intmax_t Numerator, intmax_t Quotient>
struct greatest_common_divisor : greatest_common_divisor<Quotient, (Numerator % Quotient)>
{
};
template <intmax_t Numerator>
struct greatest_common_divisor<Numerator, 0> : std::integral_constant<intmax_t, integer_abs<Numerator>::value>
{
};
template <intmax_t Quotient>
struct greatest_common_divisor<0, Quotient> : std::integral_constant<intmax_t, integer_abs<Quotient>::value>
{
};
template <typename T>
auto fmod(T x, T y) -> typename std::enable_if<std::is_floating_point<T>::value, long long int>::type
{
return y != 0 ? static_cast<long long int>(x - std::trunc(x / y) * y)
: throw std::domain_error{"Dividing by zero!"};
}
template <class CommonRep,
class Ratio,
class UnitType,
class Rep2,
bool = std::is_convertible<Rep2, CommonRep>::value>
struct unit_div_mod_base
{ // return type for unit / rep and unit % rep
using type = units::unit<CommonRep, Ratio, UnitType>;
};
template <class CommonRep, class Ratio, class UnitType, class Rep2>
struct unit_div_mod_base<CommonRep, Ratio, UnitType, Rep2, false>
{ // no return type
};
template <class Rep1, class Ratio1, class UnitType1, class Rep2, bool = is_unit<Rep2>::value>
struct unit_div_mod
{ // no return type
};
template <class Rep1, class Ratio1, class UnitType1, class Rep2>
struct unit_div_mod<Rep1, Ratio1, UnitType1, Rep2, false>
: unit_div_mod_base<typename std::common_type<Rep1, Rep2>::type, Ratio1, UnitType1, Rep2>
{ // return type for unit / rep and unit % rep
};
}
}
namespace std
{
template <typename CommonRep, typename Ratio1, typename Ratio2, typename UnitType>
struct unit_common_type
{
private:
using gcd_num = units::detail::greatest_common_divisor<Ratio1::num, Ratio2::num>;
using gcd_den = units::detail::greatest_common_divisor<Ratio1::den, Ratio2::den>;
using common_rep = typename CommonRep::type;
using ratio = std::ratio<gcd_num::value, (Ratio1::den / gcd_den::value) * Ratio2::den>;
using unit_type = UnitType;
public:
using type = units::unit<common_rep, ratio, unit_type>;
};
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
struct common_type<units::unit<Rep1, Ratio1, UnitType1>, units::unit<Rep2, Ratio2, UnitType2>>
{
static_assert(std::is_same<UnitType1, UnitType2>::value, "Incompatible unit types");
using type = typename unit_common_type<std::common_type<Rep1, Rep2>, Ratio1, Ratio2, UnitType1>::type;
};
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2>
struct common_type<units::unit<Rep1, Ratio1, UnitType1>, Rep2>;
template <typename Rep1, typename Rep2, typename Ratio, typename UnitType>
struct common_type<Rep1, units::unit<Rep2, Ratio, UnitType>>;
}
namespace units
{
namespace unit_type
{
// clang-format off
struct distance {};
struct mass {};
struct area {};
// clang-format on
}
template <typename ToUnit, typename Rep, typename Ratio, typename UnitType>
constexpr auto unit_cast(unit<Rep, Ratio, UnitType> from) ->
typename std::enable_if<is_unit<ToUnit>::value, ToUnit>::type;
template <typename Type, typename Unit>
constexpr auto unit_cast(Unit from) ->
typename std::enable_if<is_unit<Unit>::value
&& (std::is_integral<Type>::value || std::is_floating_point<Type>::value),
Type>::type;
template <typename Rep, typename Ratio, typename UnitType>
struct unit
{
using rep = Rep;
using ratio = Ratio;
using unit_type = UnitType;
constexpr explicit unit(rep value)
: value{value}
{
}
template <typename Rep2, typename Ratio2, typename UnitType2>
constexpr unit(unit<Rep2, Ratio2, UnitType2> dist)
: value{unit_cast<unit<rep, ratio, unit_type>>(dist).count()}
{
static_assert(std::is_same<unit_type, UnitType2>::value, "Unit types are not compatible");
}
constexpr rep count() const;
constexpr std::common_type_t<unit> operator+() const;
constexpr std::common_type_t<unit> operator-() const;
unit& operator++();
unit operator++(int);
unit& operator--();
unit operator--(int);
unit& operator+=(unit const other);
unit& operator-=(unit const other);
unit& operator*=(rep const scalar);
unit& operator/=(rep const scalar);
unit& operator%=(rep const scalar);
unit& operator%=(unit const other);
private:
rep value;
};
template<typename Unit>
struct squared
{
using ratio = std::ratio_multiply<typename Unit::ratio, typename Unit::ratio>;
using unit_ratio = typename Unit::ratio;
};
template <typename Rep, typename Ratio = std::ratio<1>>
using distance = unit<Rep, Ratio, unit_type::distance>;
// Useful aliases
using nanometres = distance<double, std::nano>;
using micrometres = distance<double, std::micro>;
using millimetres = distance<double, std::milli>;
using centimetres = distance<double, std::centi>;
using decimetres = distance<double, std::deci>;
using metres = distance<double>;
using kilometres = distance<double, std::kilo>;
// American spellings
using nanometers = nanometres;
using micrometers = micrometres;
using millimeters = millimetres;
using centimeters = centimetres;
using decimeters = decimetres;
using meters = metres;
using kilometers = kilometres;
// Imperial
using feet = distance<double, std::ratio_multiply<std::ratio<381, 1250>, metres::ratio>>;
using thous = distance<double, std::ratio_divide<feet::ratio, std::ratio<12000>>>;
using inches = distance<double, std::ratio_divide<feet::ratio, std::ratio<12>>>;
using links = distance<double, std::ratio_multiply<std::ratio<33, 50>, feet::ratio>>;
using yards = distance<double, std::ratio_multiply<std::ratio<3>, feet::ratio>>;
using rods = distance<double, std::ratio_multiply<std::ratio<25>, links::ratio>>;
using chains = distance<double, std::ratio_multiply<std::ratio<4>, rods::ratio>>;
using furlongs = distance<double, std::ratio_multiply<std::ratio<10>, chains::ratio>>;
using miles = distance<double, std::ratio_multiply<std::ratio<5280>, feet::ratio>>;
using leagues = distance<double, std::ratio_multiply<std::ratio<15840>, feet::ratio>>;
// Maritime
using fathoms = distance<double, std::ratio_multiply<std::ratio<608, 100>, feet::ratio>>;
using cables = distance<double, std::ratio_multiply<std::ratio<608>, feet::ratio>>;
using nautical_miles = distance<double, std::ratio_multiply<std::ratio<6080>, feet::ratio>>;
// Astronimical Units
//
// Unfortunately, std::ratio uses std::intmax_t which does not have enough precision for defining a hubble
// ratio
// which is 14.4 billion lightyears.
using earth_radii = distance<long double, std::ratio_multiply<std::ratio<6371>, kilometres::ratio>>;
using lunar_distances = distance<long double, std::ratio_multiply<std::ratio<384402>, kilometres::ratio>>;
using astronimical_units = distance<long double, std::ratio<149597870700>>;
using light_years = distance<long double, std::ratio_multiply<std::ratio<94607304725808, 10>, kilometres::ratio>>;
using parsecs = distance<long double, std::ratio_multiply<std::ratio<308567758146719, 10>, kilometres::ratio>>;
template <typename Rep, typename Ratio = std::ratio<1>>
using mass = unit<Rep, Ratio, unit_type::mass>;
// Metric
using picograms = mass<double, std::pico>;
using nanograms = mass<double, std::nano>;
using micrograms = mass<double, std::micro>;
using milligrams = mass<double, std::milli>;
using grams = mass<double>;
using kilograms = mass<double, std::kilo>;
using tons = mass<double, std::ratio_multiply<std::ratio<1000>, kilograms::ratio>>;
// Imperial
using pounds = mass<double, std::ratio_multiply<std::ratio<45359237, 100000000>, kilograms::ratio>>;
using grains = mass<double, std::ratio_divide<pounds::ratio, std::ratio<7000>>>;
using drams = mass<double, std::ratio_multiply<std::ratio<2734375, 100000>, grains::ratio>>;
using ounces = mass<double, std::ratio_multiply<std::ratio<16>, drams::ratio>>;
using us_hundredweight = mass<double, std::ratio_multiply<std::ratio<100>, pounds::ratio>>;
using long_hundredweight = mass<double, std::ratio_multiply<std::ratio<112>, pounds::ratio>>;
using short_ton = mass<double, std::ratio_multiply<std::ratio<2000>, pounds::ratio>>;
using long_ton = mass<double, std::ratio_multiply<std::ratio<2240>, pounds::ratio>>;
template <typename Rep, typename Ratio>
using area = unit<Rep, typename squared<unit<Rep, Ratio, unit_type::distance>>::ratio, unit_type::area>;
using square_metres = area<double, metres::ratio>;
using square_centimetres = area<double, centimetres::ratio>;
using square_feet = area<double, feet::ratio>;
// Arithmetic operations
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr auto operator+(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs) ->
typename std::common_type<unit<Rep1, Ratio1, UnitType1>, unit<Rep2, Ratio2, UnitType2>>::type;
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr auto operator-(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs) ->
typename std::common_type<unit<Rep1, Ratio1, UnitType1>, unit<Rep2, Ratio2, UnitType2>>::type;
template <typename Rep1, typename Ratio, typename UnitType, typename Rep2>
constexpr auto operator*(unit<Rep1, Ratio, UnitType> lhs, Rep2 const scalar) ->
typename std::enable_if<std::is_floating_point<Rep2>::value || std::is_integral<Rep2>::value,
unit<typename std::common_type<Rep1, Rep2>::type, Ratio, UnitType>>::type;
template <typename Rep1, typename Rep2, typename Ratio, typename UnitType>
constexpr auto operator*(Rep1 const scalar, unit<Rep2, Ratio, UnitType> rhs) ->
typename std::enable_if<std::is_floating_point<Rep1>::value || std::is_integral<Rep1>::value,
unit<typename std::common_type<Rep1, Rep2>::type, Ratio, UnitType>>::type;
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr auto operator*(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs) ->
typename std::enable_if<std::is_same<UnitType1, UnitType2>::value,
unit<Rep1, typename squared<decltype(lhs)>::ratio, unit_type::area >> ::type;
template <typename Rep1, typename Ratio, typename UnitType, typename Rep2>
constexpr auto operator/(unit<Rep1, Ratio, UnitType> lhs, Rep2 const scalar)
-> unit<typename std::common_type<Rep1, Rep2>::type, Ratio, UnitType>;
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr auto operator%(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs) ->
typename std::common_type<unit<Rep1, Ratio1, UnitType1>, unit<Rep2, Ratio2, UnitType2>>::type;
template <typename Rep1, typename Ratio, typename UnitType, typename Rep2>
constexpr auto operator%(unit<Rep1, Ratio, UnitType> lhs, Rep2 const scalar) ->
typename detail::unit_div_mod<Rep1, Ratio, UnitType, Rep2>::type;
// Relational operations
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr bool operator==(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs);
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr bool operator!=(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs);
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr bool operator<(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs);
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr bool operator<=(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs);
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr bool operator>(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs);
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr bool operator>=(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs);
#ifndef UNITS_DISABLE_IOSTREAM
template <typename CharT, typename Traits, typename Rep, typename Ratio, typename UnitType>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os,
unit<Rep, Ratio, UnitType> const& u);
#endif
}
inline namespace literals
{
namespace distance_literals
{
// Metric
constexpr units::nanometres operator"" _nm(unsigned long long int dist);
constexpr units::micrometres operator"" _um(unsigned long long int dist);
constexpr units::millimetres operator"" _mm(unsigned long long int dist);
constexpr units::centimetres operator"" _cm(unsigned long long int dist);
constexpr units::decimetres operator"" _dm(unsigned long long int dist);
constexpr units::metres operator"" _m(unsigned long long int dist);
constexpr units::kilometres operator"" _km(unsigned long long int dist);
// Imperial
constexpr units::thous operator"" _th(unsigned long long int dist);
constexpr units::inches operator"" _in(unsigned long long int dist);
constexpr units::links operator"" _li(unsigned long long int dist);
constexpr units::feet operator"" _ft(unsigned long long int dist);
constexpr units::yards operator"" _yd(unsigned long long int dist);
constexpr units::rods operator"" _rd(unsigned long long int dist);
constexpr units::chains operator"" _ch(unsigned long long int dist);
constexpr units::furlongs operator"" _fur(unsigned long long int dist);
constexpr units::miles operator"" _mi(unsigned long long int dist);
constexpr units::leagues operator"" _lea(unsigned long long int dist);
// Maritime
constexpr units::fathoms operator"" _ftm(unsigned long long int dist);
constexpr units::cables operator"" _cb(unsigned long long int dist);
constexpr units::nautical_miles operator"" _NM(unsigned long long int dist);
constexpr units::nautical_miles operator"" _nmi(unsigned long long int dist);
// Astronomical Units
constexpr units::earth_radii operator"" _R(unsigned long long int dist);
constexpr units::lunar_distances operator"" _LD(unsigned long long int dist);
constexpr units::astronimical_units operator"" _AU(unsigned long long int dist);
constexpr units::light_years operator"" _ly(unsigned long long int dist);
constexpr units::parsecs operator"" _pc(unsigned long long int dist);
}
namespace mass_literals
{
// Metric
constexpr units::picograms operator"" _pg(unsigned long long int mass);
constexpr units::nanograms operator"" _ng(unsigned long long int mass);
constexpr units::micrograms operator"" _ug(unsigned long long int mass);
constexpr units::milligrams operator"" _mg(unsigned long long int mass);
constexpr units::grams operator"" _g(unsigned long long int mass);
constexpr units::kilograms operator"" _kg(unsigned long long int mass);
// Imperial
constexpr units::grains operator"" _gr(unsigned long long int mass);
constexpr units::drams operator"" _dr(unsigned long long int mass);
constexpr units::ounces operator"" _oz(unsigned long long int mass);
constexpr units::pounds operator"" _lb(unsigned long long int mass);
constexpr units::us_hundredweight operator"" _cwr(unsigned long long int mass);
}
}
namespace units
{
// Implementation
template <typename Rep, typename Ratio, typename UnitType>
constexpr typename unit<Rep, Ratio, UnitType>::rep unit<Rep, Ratio, UnitType>::count() const
{
return value;
}
template <typename Rep, typename Ratio, typename UnitType>
constexpr std::common_type_t<unit<Rep, Ratio, UnitType>> unit<Rep, Ratio, UnitType>::operator+() const
{
return (*this);
}
template <typename Rep, typename Ratio, typename UnitType>
constexpr std::common_type_t<unit<Rep, Ratio, UnitType>> unit<Rep, Ratio, UnitType>::operator-() const
{
return unit<Rep, Ratio, UnitType>{0 - value};
}
template <typename Rep, typename Ratio, typename UnitType>
unit<Rep, Ratio, UnitType>& unit<Rep, Ratio, UnitType>::operator++()
{
++value;
return *this;
}
template <typename Rep, typename Ratio, typename UnitType>
unit<Rep, Ratio, UnitType> unit<Rep, Ratio, UnitType>::operator++(int)
{
auto const temp = value;
++value;
return unit<Rep, Ratio, UnitType>{temp};
}
template <typename Rep, typename Ratio, typename UnitType>
unit<Rep, Ratio, UnitType>& unit<Rep, Ratio, UnitType>::operator--()
{
--value;
return *this;
}
template <typename Rep, typename Ratio, typename UnitType>
unit<Rep, Ratio, UnitType> unit<Rep, Ratio, UnitType>::operator--(int)
{
auto const temp = value;
--value;
return unit<Rep, Ratio, UnitType>{temp};
}
template <typename Rep, typename Ratio, typename UnitType>
unit<Rep, Ratio, UnitType>& unit<Rep, Ratio, UnitType>::operator+=(unit const other)
{
value += other.count();
return *this;
}
template <typename Rep, typename Ratio, typename UnitType>
unit<Rep, Ratio, UnitType>& unit<Rep, Ratio, UnitType>::operator-=(unit const other)
{
value -= other.count();
return *this;
}
template <typename Rep, typename Ratio, typename UnitType>
unit<Rep, Ratio, UnitType>& unit<Rep, Ratio, UnitType>::operator*=(rep const scalar)
{
value *= scalar;
return *this;
}
template <typename Rep, typename Ratio, typename UnitType>
unit<Rep, Ratio, UnitType>& unit<Rep, Ratio, UnitType>::operator/=(rep const scalar)
{
value /= scalar;
return *this;
}
template <typename Rep, typename Ratio, typename UnitType>
unit<Rep, Ratio, UnitType>& unit<Rep, Ratio, UnitType>::operator%=(rep const scalar)
{
value = static_cast<rep>(detail::fmod(value, scalar));
return *this;
}
template <typename Rep, typename Ratio, typename UnitType>
unit<Rep, Ratio, UnitType>& unit<Rep, Ratio, UnitType>::operator%=(unit const other)
{
value = static_cast<rep>(detail::fmod(value, other.count()));
return *this;
}
// Arithmetic operations
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr auto operator+(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs) ->
typename std::common_type<unit<Rep1, Ratio1, UnitType1>, unit<Rep2, Ratio2, UnitType2>>::type
{
using unit1 = unit<Rep1, Ratio1, UnitType1>;
using unit2 = unit<Rep2, Ratio2, UnitType2>;
using common_type = typename std::common_type<unit1, unit2>::type;
return static_cast<common_type>(static_cast<common_type>(lhs).count() + static_cast<common_type>(rhs).count());
}
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr auto operator-(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs) ->
typename std::common_type<unit<Rep1, Ratio1, UnitType1>, unit<Rep2, Ratio2, UnitType2>>::type
{
using unit1 = unit<Rep1, Ratio1, UnitType1>;
using unit2 = unit<Rep2, Ratio2, UnitType2>;
using common_type = typename std::common_type<unit1, unit2>::type;
return static_cast<common_type>(static_cast<common_type>(lhs).count() - static_cast<common_type>(rhs).count());
}
template <typename Rep1, typename Ratio, typename UnitType, typename Rep2>
constexpr auto operator*(unit<Rep1, Ratio, UnitType> lhs, Rep2 const scalar) ->
typename std::enable_if<std::is_floating_point<Rep2>::value || std::is_integral<Rep2>::value,
unit<typename std::common_type<Rep1, Rep2>::type, Ratio, UnitType>>::type
{
using result_type = unit<typename std::common_type<Rep1, Rep2>::type, Ratio, UnitType>;
return static_cast<result_type>(static_cast<result_type>(lhs).count() * scalar);
}
template <typename Rep1, typename Rep2, typename Ratio, typename UnitType>
constexpr auto operator*(Rep1 const scalar, unit<Rep2, Ratio, UnitType> rhs) ->
typename std::enable_if<std::is_floating_point<Rep1>::value || std::is_integral<Rep1>::value,
unit<typename std::common_type<Rep1, Rep2>::type, Ratio, UnitType>>::type
{
return rhs * scalar;
}
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr auto operator*(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs) ->
typename std::enable_if<std::is_same<UnitType1, UnitType2>::value,
unit<Rep1, typename squared<decltype(lhs)>::ratio, unit_type::area>>::type
{
using result_type = unit<Rep1, typename squared<decltype(lhs)>::ratio, unit_type::area>;
return result_type{lhs.count() * unit_cast<decltype(lhs)>(rhs).count()};
}
template <typename Rep1, typename Ratio, typename UnitType, typename Rep2>
constexpr auto operator/(unit<Rep1, Ratio, UnitType> lhs, Rep2 const scalar)
-> unit<typename std::common_type<Rep1, Rep2>::type, Ratio, UnitType>
{
using result_type = unit<typename std::common_type<Rep1, Rep2>::type, Ratio, UnitType>;
return static_cast<result_type>(static_cast<result_type>(lhs).count() / scalar);
}
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr auto operator%(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs) ->
typename std::common_type<unit<Rep1, Ratio1, UnitType1>, unit<Rep2, Ratio2, UnitType2>>::type
{
using unit1 = unit<Rep1, Ratio1, UnitType1>;
using unit2 = unit<Rep2, Ratio2, UnitType2>;
using common_type = typename std::common_type<unit1, unit2>::type;
return common_type{common_type{lhs}.count() % common_type{rhs}.count()};
}
template <typename Rep1, typename Ratio, typename UnitType, typename Rep2>
constexpr auto operator%(unit<Rep1, Ratio, UnitType> lhs, Rep2 const scalar) ->
typename detail::unit_div_mod<Rep1, Ratio, UnitType, Rep2>::type
{
using result_type = unit<typename std::common_type<Rep1, Rep2>::type, Ratio, UnitType>;
return result_type{result_type{lhs}.count()
% result_type{static_cast<typename result_type::rep>(scalar)}.count()};
}
namespace detail
{
template <typename T>
constexpr auto abs(const T& value)
{
return (T{} > value) ? -value : value;
}
constexpr bool unit_compare(double lhs,
double rhs,
double max_diff = 0.000000001,
double max_relative_diff = std::numeric_limits<double>::epsilon())
{
return (abs(lhs - rhs) <= (((abs(rhs) > abs(lhs)) ? abs(rhs) : abs(lhs)) * max_relative_diff)) || (abs(lhs - rhs) <= max_diff);
}
}
// Relational operations
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr bool operator==(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs)
{
using unit1 = unit<Rep1, Ratio1, UnitType1>;
using unit2 = unit<Rep2, Ratio2, UnitType2>;
using common_type = typename std::common_type<unit1, unit2>::type;
return detail::unit_compare(unit_cast<common_type>(lhs).count(), unit_cast<common_type>(rhs).count());
}
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr bool operator!=(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs)
{
return !(lhs == rhs);
}
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr bool operator<(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs)
{
using unit1 = unit<Rep1, Ratio1, UnitType1>;
using unit2 = unit<Rep2, Ratio2, UnitType2>;
using common_type = typename std::common_type<unit1, unit2>::type;
return std::isless(unit_cast<common_type>(lhs).count(), unit_cast<common_type>(rhs).count());
}
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr bool operator<=(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs)
{
using unit1 = unit<Rep1, Ratio1, UnitType1>;
using unit2 = unit<Rep2, Ratio2, UnitType2>;
using common_type = typename std::common_type<unit1, unit2>::type;
return std::islessequal(unit_cast<common_type>(lhs).count(), unit_cast<common_type>(rhs).count());
}
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr bool operator>(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs)
{
using unit1 = unit<Rep1, Ratio1, UnitType1>;
using unit2 = unit<Rep2, Ratio2, UnitType2>;
using common_type = typename std::common_type<unit1, unit2>::type;
return std::isgreater(unit_cast<common_type>(lhs).count(), unit_cast<common_type>(rhs).count());
}
template <typename Rep1, typename Ratio1, typename UnitType1, typename Rep2, typename Ratio2, typename UnitType2>
constexpr bool operator>=(unit<Rep1, Ratio1, UnitType1> lhs, unit<Rep2, Ratio2, UnitType2> rhs)
{
using unit1 = unit<Rep1, Ratio1, UnitType1>;
using unit2 = unit<Rep2, Ratio2, UnitType2>;
using common_type = typename std::common_type<unit1, unit2>::type;
return std::isgreaterequal(unit_cast<common_type>(lhs).count(), unit_cast<common_type>(rhs).count());
}
namespace detail
{
template <typename ToUnit,
typename Ratio,
typename CommonType,
bool RatioNumIsOne = false,
bool RatioDenIsOne = false>
struct unit_cast
{
template <typename Rep, typename Length, typename UnitType>
static constexpr ToUnit cast(unit<Rep, Length, UnitType> from)
{
using ToRep = typename ToUnit::rep;
return ToUnit{static_cast<ToRep>(static_cast<CommonType>(from.count())
/ static_cast<CommonType>(Ratio::num)
* static_cast<CommonType>(Ratio::den))};
}
};
template <typename ToUnit, typename Ratio, typename CommonType>
struct unit_cast<ToUnit, Ratio, CommonType, true, true>
{
template <typename Rep, typename Length, typename UnitType>
static constexpr ToUnit cast(unit<Rep, Length, UnitType> from)
{
using ToRep = typename ToUnit::rep;
return ToUnit{static_cast<ToRep>(static_cast<CommonType>(from.count()))};
}
};
template <typename ToUnit, typename Ratio, typename CommonType>
struct unit_cast<ToUnit, Ratio, CommonType, true, false>
{
template <typename Rep, typename Length, typename UnitType>
static constexpr ToUnit cast(unit<Rep, Length, UnitType> from)
{
using ToRep = typename ToUnit::rep;
return ToUnit{
static_cast<ToRep>(static_cast<CommonType>(from.count()) * static_cast<CommonType>(Ratio::den))};
}
};
template <typename ToUnit, typename Ratio, typename CommonType>
struct unit_cast<ToUnit, Ratio, CommonType, false, true>
{
template <typename Rep, typename Length, typename UnitType>
static constexpr ToUnit cast(unit<Rep, Length, UnitType> from)
{
using ToRep = typename ToUnit::rep;
return ToUnit{
static_cast<ToRep>(static_cast<CommonType>(from.count()) / static_cast<CommonType>(Ratio::num))};
}
};
}
template <typename ToUnit, typename Rep, typename Ratio, typename UnitType>
constexpr auto unit_cast(unit<Rep, Ratio, UnitType> from) ->
typename std::enable_if<is_unit<ToUnit>::value, ToUnit>::type
{
static_assert(std::is_same<typename ToUnit::unit_type, UnitType>::value, "Incompatible types");
using ToRatio = typename ToUnit::ratio;
using ToRep = typename ToUnit::rep;
using CommonType = typename std::common_type<ToRep, Rep, intmax_t>::type;
using CommonRatio = std::ratio_divide<ToRatio, Ratio>;
return detail::unit_cast<ToUnit, CommonRatio, CommonType, CommonRatio::num == 1, CommonRatio::den == 1>::cast(
from);
}
template <typename Type, typename Unit>
constexpr auto unit_cast(Unit from)
->typename std::enable_if<is_unit<Unit>::value
&& (std::is_integral<Type>::value || std::is_floating_point<Type>::value),
Type>::type
{
return static_cast<Type>(from.count());
}
}
inline namespace literals
{
namespace distance_literals
{
// Metric
constexpr units::nanometres operator"" _nm(unsigned long long int dist)
{
return units::nanometres{static_cast<units::nanometres::rep>(dist)};
}
constexpr units::micrometres operator"" _um(unsigned long long int dist)
{
return units::micrometres{static_cast<units::micrometres::rep>(dist)};
}
constexpr units::millimetres operator"" _mm(unsigned long long int dist)
{
return units::millimetres{static_cast<units::millimetres::rep>(dist)};
}
constexpr units::centimetres operator"" _cm(unsigned long long int dist)
{
return units::centimetres{static_cast<units::centimetres::rep>(dist)};
}
constexpr units::decimetres operator"" _dm(unsigned long long int dist)
{
return units::decimetres{static_cast<units::decimetres::rep>(dist)};
}
constexpr units::metres operator"" _m(unsigned long long int dist)
{
return units::metres{static_cast<units::metres::rep>(dist)};
}
constexpr units::kilometres operator"" _km(unsigned long long int dist)
{
return units::kilometres{static_cast<units::kilometres::rep>(dist)};
}
// Imperial
constexpr units::thous operator"" _th(unsigned long long int dist)
{
return units::thous{static_cast<units::thous::rep>(dist)};
}
constexpr units::inches operator"" _in(unsigned long long int dist)
{
return units::inches{static_cast<units::inches::rep>(dist)};
}
constexpr units::links operator"" _li(unsigned long long int dist)
{
return units::links{static_cast<units::links::rep>(dist)};
}
constexpr units::feet operator"" _ft(unsigned long long int dist)
{
return units::feet{static_cast<units::feet::rep>(dist)};
}
constexpr units::yards operator"" _yd(unsigned long long int dist)
{
return units::yards{static_cast<units::yards::rep>(dist)};
}
constexpr units::rods operator"" _rd(unsigned long long int dist)
{
return units::rods{static_cast<units::rods::rep>(dist)};
}
constexpr units::chains operator"" _ch(unsigned long long int dist)
{
return units::chains{static_cast<units::chains::rep>(dist)};
}
constexpr units::furlongs operator"" _fur(unsigned long long int dist)
{
return units::furlongs{static_cast<units::furlongs::rep>(dist)};
}
constexpr units::miles operator"" _mi(unsigned long long int dist)
{
return units::miles{static_cast<units::miles::rep>(dist)};
}
constexpr units::leagues operator"" _lea(unsigned long long int dist)
{
return units::leagues{static_cast<units::leagues::rep>(dist)};
}
// Maritime
constexpr units::fathoms operator"" _ftm(unsigned long long int dist)
{
return units::fathoms{static_cast<units::fathoms::rep>(dist)};
}
constexpr units::cables operator"" _cb(unsigned long long int dist)
{
return units::cables{static_cast<units::cables::rep>(dist)};
}
constexpr units::nautical_miles operator"" _NM(unsigned long long int dist)
{
return units::nautical_miles{static_cast<units::nautical_miles::rep>(dist)};
}
constexpr units::nautical_miles operator"" _nmi(unsigned long long int dist)
{
return units::nautical_miles{static_cast<units::nautical_miles::rep>(dist)};
}
// Astronomical Units
constexpr units::earth_radii operator"" _R(unsigned long long int dist)
{
return units::earth_radii{static_cast<units::earth_radii::rep>(dist)};
}
constexpr units::lunar_distances operator"" _LD(unsigned long long int dist)
{
return units::lunar_distances{static_cast<units::lunar_distances::rep>(dist)};
}
constexpr units::astronimical_units operator"" _AU(unsigned long long int dist)
{
return units::astronimical_units{static_cast<units::astronimical_units::rep>(dist)};
}
constexpr units::light_years operator"" _ly(unsigned long long int dist)
{
return units::light_years{static_cast<units::light_years::rep>(dist)};
}
constexpr units::parsecs operator"" _pc(unsigned long long int dist)
{
return units::parsecs{static_cast<units::parsecs::rep>(dist)};
}
}
namespace mass_literals
{
// Metric
constexpr units::picograms operator"" _pg(unsigned long long int mass)
{
return units::picograms{static_cast<units::picograms::rep>(mass)};
}
constexpr units::nanograms operator"" _ng(unsigned long long int mass)
{
return units::nanograms{static_cast<units::nanograms::rep>(mass)};
}
constexpr units::micrograms operator"" _ug(unsigned long long int mass)
{
return units::micrograms{static_cast<units::micrograms::rep>(mass)};
}
constexpr units::milligrams operator"" _mg(unsigned long long int mass)
{
return units::milligrams{static_cast<units::milligrams::rep>(mass)};
}
constexpr units::grams operator"" _g(unsigned long long int mass)
{
return units::grams{static_cast<units::grams::rep>(mass)};
}
constexpr units::kilograms operator"" _kg(unsigned long long int mass)
{
return units::kilograms{static_cast<units::kilograms::rep>(mass)};
}
// Imperial
constexpr units::grains operator"" _gr(unsigned long long int mass)
{
return units::grains{static_cast<units::grains::rep>(mass)};
}
constexpr units::drams operator"" _dr(unsigned long long int mass)
{
return units::drams{static_cast<units::drams::rep>(mass)};
}
constexpr units::ounces operator"" _oz(unsigned long long int mass)
{
return units::ounces{static_cast<units::ounces::rep>(mass)};
}
constexpr units::pounds operator"" _lb(unsigned long long int mass)
{
return units::pounds{static_cast<units::pounds::rep>(mass)};
}
constexpr units::us_hundredweight operator"" _cwt(unsigned long long int mass)
{
return units::us_hundredweight{static_cast<units::us_hundredweight::rep>(mass)};
}
}
}
namespace units
{
#ifndef UNITS_DISABLE_IOSTREAM
namespace detail
{
template <typename Ratio>
inline std::string get_prefix();
template <>
inline std::string get_prefix<std::ratio<1>>()
{
return "";
}
template <>
inline std::string get_prefix<std::pico>()
{
return "p";
}
template <>
inline std::string get_prefix<std::nano>()
{
return "n";
}
template <>
inline std::string get_prefix<std::micro>()
{
return "u";
}
template <>
inline std::string get_prefix<std::milli>()
{
return "m";
}
template <>
inline std::string get_prefix<std::centi>()
{
return "c";
}
template <>
inline std::string get_prefix<std::deci>()
{
return "d";
}
template <>
inline std::string get_prefix<std::kilo>()
{
return "k";
}
template <typename UnitType>
inline std::string get_unit();
template<>