MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
length.hpp
Go to the documentation of this file.
1/*
2
3Copyright 2025 Matthew Tolman
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17*/
18
19#ifndef MTCORE_UNITS_LENGTH_HPP
20#define MTCORE_UNITS_LENGTH_HPP
21
22#include "mtcore/units/base.hpp"
23
24#include <concepts>
25#include <string_view>
26
27namespace mtcore::units {
28 namespace length {
29 // SI Units
31 constexpr auto attometer = attometre;
33 constexpr auto femtometer = femtometre;
35 constexpr auto picometer = picometre;
37 constexpr auto nanometer = nanometre;
39 constexpr auto micrometer = micrometre;
41 constexpr auto millimeter = millimetre;
43 constexpr auto centimeter = centimetre;
45 constexpr auto decimeter = decimetre;
47 constexpr auto meter = metre;
49 constexpr auto decameter = decametre;
51 constexpr auto hectometer = hectometre;
53 constexpr auto kilometer = kilometre;
55 constexpr auto megameter = megametre;
57 constexpr auto gigameter = gigametre;
59 constexpr auto terameter = terametre;
61 constexpr auto petameter = petametre;
63 constexpr auto exameter = exametre;
64
76
77 static_assert(3 * mile == 1 * league);
78 static_assert(8 * furlong == 1 * mile);
79 static_assert(10 * chain == 1 * furlong);
80 static_assert(22 * yard == 1 * chain);
81 static_assert(3 * foot == 1 * yard);
82 static_assert(12 * inch == 1 * foot);
83 static_assert(3 * barleycorn == 1 * inch);
84 static_assert((333.333333 * thou).to(barleycorn).val >= 0.99999 &&
85 (333.333333 * thou).to(barleycorn).val <= 1.000001);
86 static_assert((1.44 * twip).to(thou).val >= 0.99999 && (1.44 * twip).to(thou).val <= 1.0001);
87
88 static_assert((25.4 * millimeter).to(inch).val >= 0.99999 && (25.4 * millimeter).to(inch).val <= 1.000001);
89
90 // Maritime Units
94
95 static_assert((1. * fathom).to(meter).val == 1.852);
96 static_assert(1. * fathom == 1.852 * meter);
97 static_assert(100 * fathom == 1 * cable);
98 static_assert(10 * cable == 1 * nautical_mile);
99
100 // Gunter's survey units
103
104 static_assert(7.92 * inch == 1. * link);
105 static_assert(25 * link == 1 * rod);
106 } // namespace length
107
108 namespace impl {
109 static_assert(AllLeftInRight<decltype(length::meter), decltype(length::meter)>::value);
110 static_assert(Convert<decltype(length::meter), decltype(length::centimeter)>::convert<false>(1) == 100);
111 static_assert(Convert<decltype(length::meter), decltype(length::decimeter)>::convert<false>(1) == 10);
112
113 static_assert(std::same_as<ConvertMorePrecise<std::remove_cv_t<decltype(length::meter)>,
114 std::remove_cv_t<decltype(length::decimeter)>>::T,
115 std::remove_cv_t<decltype(length::decimeter)>>);
116
117 static_assert(std::same_as<ConvertMorePrecise<std::remove_cv_t<decltype(length::decimeter)>,
118 std::remove_cv_t<decltype(length::meter)>>::T,
119 std::remove_cv_t<decltype(length::decimeter)>>);
120 } // namespace impl
121
122 MTCORE_DEF_UNIT_NAME_SI(length, metre, meter, m)
123 MTCORE_DEF_UNIT_NAME_DIFF_US(length, attometre, attometer, am);
124 MTCORE_DEF_UNIT_NAME_DIFF_US(length, exametre, exameter, Em);
125
137
140 MTCORE_DEF_UNIT_NAME(length, nautical_mile, "nmi");
141
144
145 namespace area {
146 constexpr auto perch = length::rod * length::rod;
147 constexpr auto rood = length::furlong * length::rod;
150
151 static_assert(1. * perch == 272.25 * length::foot * length::foot);
152 static_assert(1. * rood == 40. * perch);
153 static_assert(1. * acre == 4. * rood);
154 static_assert(1. * square_mile == 640. * acre);
155 } // namespace area
156
160 MTCORE_DEF_UNIT_NAME(area, square_mile, sq mi);
161
162 namespace volume {
163 // SI volumes
165 constexpr auto liter = litre;
168 constexpr auto milliliter = millilitre;
169 constexpr auto microliter = microlitre;
171 constexpr auto centiliter = centilitre;
173 constexpr auto deciliter = decilitre;
175 constexpr auto nanoliter = nanolitre;
177 constexpr auto picoliter = picolitre;
179 constexpr auto femtoliter = femtolitre;
180
182 constexpr auto decaliter = decalitre;
184 constexpr auto hectoliter = hectolitre;
185
187 constexpr auto kiloliter = kilolitre;
188
190 constexpr auto megaliter = megalitre;
191
193 constexpr auto gigaliter = gigalitre;
194
196 constexpr auto teraliter = teralitre;
197
199 constexpr auto petaliter = petalitre;
200
201 static_assert(10 * decilitre == 1 * liter);
202 static_assert(100 * centilitre == 1 * liter);
203 static_assert(1000 * millilitre == 1 * liter);
204 static_assert(1e6 * microlitre == 1 * liter);
205 static_assert(1e9 * nanolitre == 1 * liter);
206 static_assert(1e12 * picoliter == 1. * liter);
207 static_assert(1e15 * femtoliter == 1. * liter);
208 static_assert(1e-1 * decalitre == 1. * liter);
209 static_assert(1e-2 * hectoliter == 1. * liter);
210 static_assert(1e-3 * kiloliter == 1. * liter);
211 static_assert(1e-6 * megaliter == 1. * liter);
212 static_assert(1e-9 * gigaliter == 1. * liter);
213 static_assert(1e-12 * teraliter == 1. * liter);
214 static_assert(1e-15 * petaliter == 1. * liter);
215
216 // US Customary cubics
222
223 static_assert(1. * cubic_inch >= 16.387064 * millilitre - 0.00001 * millilitre);
224 static_assert(1. * cubic_inch <= 16.387064 * millilitre + 0.00001 * millilitre);
225
226 static_assert(1. * cubic_foot >= 28.316846592 * liter - 0.00001 * liter);
227 static_assert(1. * cubic_foot <= 28.316846592 * liter + 0.00001 * liter);
228
229 static_assert(1. * cubic_yard >= 764.554857984 * liter - 0.00001 * liter);
230 static_assert(1. * cubic_yard <= 764.554857984 * liter + 0.00001 * liter);
231
232 static_assert(1. * acre_foot >= 43560 * cubic_foot - 0.00001 * cubic_foot);
233 static_assert(1. * acre_foot <= 43560 * cubic_foot + 0.00001 * cubic_foot);
234
235 // US Fluid volume
236 namespace fluid {
247
264
265 static_assert(1. * us_gal >= 3.785411784 * liter - 0.0000001 * liter);
266 static_assert(1. * us_gal <= 3.785411784 * liter + 0.0000001 * liter);
267 static_assert(1. * us_gal <= 231. * cubic_inch + 0.0000001 * cubic_inch);
268 static_assert(2. * us_pot == 1. * us_gal);
269 static_assert(4. * us_quart == 1. * us_gal);
270 static_assert(8. * us_pint == 1. * us_gal);
271 static_assert(16. * us_cup == 1. * us_gal);
272 static_assert(2. * us_gill >= 1. * us_cup - 0.000001 * us_cup);
273 static_assert(2. * us_gill <= 1. * us_cup + 0.000001 * us_cup);
274 static_assert(8. * us_fluid_ounce == 1.0 * us_cup);
275 static_assert(4. * us_fluid_ounce == 1.0 * us_gill);
276 static_assert(1.5 * us_fluid_ounce == 1.0 * us_jig);
277 static_assert(2. * us_tablespoon == 1.0 * us_fluid_ounce);
278 static_assert((3. * us_teaspoon).to(us_tablespoon).val == 1.0);
279 static_assert(3. * us_teaspoon == 1. * us_tablespoon);
280 static_assert(1. * us_barrel >= 31.5 * us_gal - 0.00001 * us_gal);
281 static_assert(1. * us_barrel <= 31.5 * us_gal + 0.00001 * us_gal);
282 static_assert(1. * us_oil_barrel >= 42 * us_gal - 0.00001 * us_gal);
283 static_assert(1. * us_oil_barrel <= 42 * us_gal + 0.00001 * us_gal);
284 static_assert(1. * us_hogshead >= 63 * us_gal - 0.00001 * us_gal);
285 static_assert(1. * us_hogshead <= 63 * us_gal + 0.00001 * us_gal);
286 } // namespace fluid
287
288 // US dry volume
311 } // namespace volume
312
314
315 MTCORE_DEF_UNIT_NAME_STRS(volume, fluid::us_gal, "US gallon", "gallon", "US gal", "US gal", "gal", "");
316 MTCORE_DEF_UNIT_NAME_STRS(volume, fluid::us_pot, "US pot", "pot", "US pot", "US pot", "pot", "");
317 MTCORE_DEF_UNIT_NAME_STRS(volume, fluid::us_quart, "US quart", "quart", "US qt", "US qt", "qt", "");
318 MTCORE_DEF_UNIT_NAME_STRS(volume, fluid::us_pint, "US pint", "pint", "US pt", "US pt", "pt", "");
319 MTCORE_DEF_UNIT_NAME_STRS(volume, fluid::us_cup, "US cup", "cup", "US c", "US c", "c", "");
320 MTCORE_DEF_UNIT_NAME_STRS(volume, fluid::us_gill, "US gill", "gill", "US gi", "US gi", "gi", "");
321 MTCORE_DEF_UNIT_NAME_STRS(volume, fluid::us_jig, "US jig", "jig", "US jig", "US jig", "jig", "");
322 MTCORE_DEF_UNIT_NAME_STRS(volume, fluid::us_fluid_ounce, "US fluid ounce", "fluid ounce", "US fl oz", "US fl oz",
323 "fl oz", "");
324 MTCORE_DEF_UNIT_NAME_STRS(volume, fluid::us_tablespoon, "US tablespoon", "tablespoon", "US tbsp", "US tbsp", "tbsp",
325 "");
326 MTCORE_DEF_UNIT_NAME_STRS(volume, fluid::us_teaspoon, "US teaspoon", "teaspoon", "US tsp", "US tsp", "tsp", "");
327 MTCORE_DEF_UNIT_NAME_STRS(volume, fluid::us_barrel, "US barrel (liquid)", "barrel (liquid)", "US bbl", "US bbl",
328 "bbl", "");
329 MTCORE_DEF_UNIT_NAME_STRS(volume, fluid::us_oil_barrel, "US oil barrel", "oil barrel", "US bbl", "US bbl", "bbl", "");
330 MTCORE_DEF_UNIT_NAME_STRS(volume, fluid::us_hogshead, "US hogshead", "hogshead", "US hogshead", "US hogshead",
331 "hogshead", "");
332
333 MTCORE_DEF_UNIT_NAME_STRS(volume, cubic_inch, "cubic inch", "cubic inch", "cu in", "cu in", "cu in", "");
334 MTCORE_DEF_UNIT_NAME_STRS(volume, cubic_foot, "cubic foot", "cubic foot", "cu ft", "cu ft", "cu ft", "");
335 MTCORE_DEF_UNIT_NAME_STRS(volume, cubic_yard, "cubic yard", "cubic yard", "cu yd", "cu yd", "cu yd", "");
336 MTCORE_DEF_UNIT_NAME_STRS(volume, acre_foot, "acre-foot", "acre-foot", "acre-ft", "acre-ft", "acre-ft", "");
337
338 static_assert(UnitName<std::remove_const_t<decltype(length::meter)>>::name == "metre");
339 static_assert(UnitName<std::remove_const_t<decltype(length::decimeter)>>::name == "decimetre");
340 static_assert(UnitName<std::remove_const_t<decltype(area::acre)>>::name == "acre");
341 static_assert(UnitName<std::remove_const_t<decltype(volume::litre)>>::name == "litre");
342 static_assert(UnitName<std::remove_const_t<decltype(volume::decilitre)>>::name == "decilitre");
343 static_assert(UnitName<std::remove_const_t<decltype(volume::decilitre)>>::name == "decilitre");
344
345 static_assert(std::same_as<std::remove_cvref_t<decltype(length::meter * length::meter)>, Units<Unit<0>, Unit<0>>>);
346 static_assert(impl::GetUnit<0, std::remove_cvref_t<decltype(length::meter * length::meter)>>::exponent == 2);
347 static_assert(
348 std::same_as<std::remove_cvref_t<decltype(1 * length::meter * length::meter)::UnitsType>, Units<Unit<0>, Unit<0>>>);
349 static_assert(impl::GetUnit<0, decltype(1 * length::meter * length::meter)::UnitsType>::exponent == 2);
350 static_assert(impl::GetUnit<0, decltype(length::meter * length::meter)>::exponent == 2);
351 static_assert(impl::GetUnit<0, decltype(1.5 * length::decimeter * length::decimeter)::UnitsType>::exponent == 2);
353 static_assert((1.5 * length::decimeter * length::decimeter).val == 1.5);
354 static_assert((1.5 * length::centimeter * length::decimeter).val == 1.5);
355 static_assert((1.5 * length::decimeter * length::centimeter).to(length::centimeter * length::centimeter).val == 15);
356 static_assert((1.5 * length::decimeter * length::decimeter + 1.5 * length::decimeter * length::decimeter).val == 3.);
357 static_assert((1 * length::meter) + (2 * length::meter) == (3 * length::meter));
358 static_assert((1. * length::meter) + (2. * length::meter) == (3. * length::meter));
359 static_assert((1 / length::meter) + (2 / length::meter) == (3 / length::meter));
360 static_assert((1. / length::meter) + (2. / length::meter) == (3. / length::meter));
361 static_assert((1 / length::meter) * length::meter == 1);
362 static_assert(((1 * length::meter) + (10 * length::decimeter)).val == 20);
363 static_assert((1 * length::meter) + (10 * length::decimeter) == 20 * length::decimeter);
364 static_assert((1 * length::meter) + (10 * length::decimeter) == 2 * length::meter);
365 static_assert((1. * length::meter) + (15. * length::decimeter) == 2.5 * length::meter);
366 static_assert(((1. * length::meter) + (15. * length::decimeter)).convert<decltype(length::meter)>() ==
367 2.5 * length::meter);
368 static_assert(((1. * length::meter) + (15. * length::decimeter)).to(length::meter) == 2.5 * length::meter);
369 static_assert((1. * length::meter * length::meter) + (1.5 * length::meter * length::meter) ==
371 static_assert(((1. * length::meter * length::meter) + (1.5 * length::decimeter * length::decimeter)).val == 1.015);
372 static_assert(((1. * length::meter * length::meter) + (1.5 * length::decimeter * length::decimeter)) ==
373 1.015 * length::meter * length::meter);
374 static_assert(((1. * length::meter * length::meter) + (1.5 * length::centimeter * length::decimeter)).val == 1.0015);
375 static_assert(((1. * length::meter * length::meter) + (1.5 * length::centimeter * length::decimeter)) ==
376 1.0015 * length::meter * length::meter);
377} // namespace mtcore::units
378
379#endif // MTCORE_UNITS_LENGTH_HPP
constexpr auto acre
Definition length.hpp:148
constexpr auto rood
Definition length.hpp:147
constexpr auto square_mile
Definition length.hpp:149
constexpr auto perch
Definition length.hpp:146
constexpr auto exametre
Definition length.hpp:62
constexpr auto decimeter
Definition length.hpp:45
constexpr auto attometer
Definition length.hpp:31
constexpr auto gigameter
Definition length.hpp:57
constexpr auto picometer
Definition length.hpp:35
constexpr auto decimetre
Definition length.hpp:44
constexpr auto twip
Definition length.hpp:65
constexpr auto centimetre
Definition length.hpp:42
constexpr auto hectometre
Definition length.hpp:50
constexpr auto terameter
Definition length.hpp:59
constexpr auto attometre
Definition length.hpp:30
constexpr auto nanometer
Definition length.hpp:37
constexpr auto thou
Definition length.hpp:66
constexpr auto millimeter
Definition length.hpp:41
constexpr auto fathom
Definition length.hpp:91
constexpr auto decameter
Definition length.hpp:49
constexpr auto link
Definition length.hpp:101
constexpr auto inch
Definition length.hpp:68
constexpr auto decametre
Definition length.hpp:48
constexpr auto micrometre
Definition length.hpp:38
constexpr auto mile
Definition length.hpp:74
constexpr auto petameter
Definition length.hpp:61
constexpr auto kilometre
Definition length.hpp:52
constexpr auto centimeter
Definition length.hpp:43
constexpr auto nautical_mile
Definition length.hpp:93
constexpr auto metre
Definition length.hpp:46
constexpr auto kilometer
Definition length.hpp:53
constexpr auto petametre
Definition length.hpp:60
constexpr auto picometre
Definition length.hpp:34
constexpr auto micrometer
Definition length.hpp:39
constexpr auto exameter
Definition length.hpp:63
constexpr auto femtometre
Definition length.hpp:32
constexpr auto meter
Definition length.hpp:47
constexpr auto chain
Definition length.hpp:72
constexpr auto foot
Definition length.hpp:70
constexpr auto gigametre
Definition length.hpp:56
constexpr auto nanometre
Definition length.hpp:36
constexpr auto terametre
Definition length.hpp:58
constexpr auto femtometer
Definition length.hpp:33
constexpr auto cable
Definition length.hpp:92
constexpr auto furlong
Definition length.hpp:73
constexpr auto megametre
Definition length.hpp:54
constexpr auto yard
Definition length.hpp:71
constexpr auto millimetre
Definition length.hpp:40
constexpr auto hectometer
Definition length.hpp:51
constexpr auto league
Definition length.hpp:75
constexpr auto rod
Definition length.hpp:102
constexpr auto barleycorn
Definition length.hpp:67
constexpr auto hand
Definition length.hpp:69
constexpr auto megameter
Definition length.hpp:55
constexpr auto us_peck
Definition length.hpp:296
constexpr auto us_pint
Definition length.hpp:294
constexpr auto us_bushel
Definition length.hpp:298
constexpr auto us_barrel
Definition length.hpp:300
constexpr auto us_quart
Definition length.hpp:292
constexpr auto us_gal
Definition length.hpp:290
constexpr auto us_tablespoon
Definition length.hpp:254
constexpr auto us_oil_barrel
Definition length.hpp:260
constexpr auto us_teaspoon
Definition length.hpp:256
constexpr auto us_fluid_ounce
Definition length.hpp:248
constexpr auto us_hogshead
Definition length.hpp:262
constexpr auto decaliter
Definition length.hpp:182
constexpr auto hectolitre
Definition length.hpp:183
constexpr auto cubic_yard
Definition length.hpp:219
constexpr auto megalitre
Definition length.hpp:189
constexpr auto kiloliter
Definition length.hpp:187
constexpr auto milliliter
Definition length.hpp:168
constexpr auto picoliter
Definition length.hpp:177
constexpr auto megaliter
Definition length.hpp:190
constexpr auto centiliter
Definition length.hpp:171
constexpr auto acre_foot
Definition length.hpp:220
constexpr auto gigalitre
Definition length.hpp:192
constexpr auto gigaliter
Definition length.hpp:193
constexpr auto microlitre
Definition length.hpp:166
constexpr auto femtoliter
Definition length.hpp:179
constexpr auto kilolitre
Definition length.hpp:186
constexpr auto nanoliter
Definition length.hpp:175
constexpr auto picolitre
Definition length.hpp:176
constexpr auto cubic_foot
Definition length.hpp:218
constexpr auto millilitre
Definition length.hpp:167
constexpr auto liter
Definition length.hpp:165
constexpr auto microliter
Definition length.hpp:169
constexpr auto teraliter
Definition length.hpp:196
constexpr auto litre
Definition length.hpp:164
constexpr auto hectoliter
Definition length.hpp:184
constexpr auto deciliter
Definition length.hpp:173
constexpr auto femtolitre
Definition length.hpp:178
constexpr auto teralitre
Definition length.hpp:195
constexpr auto nanolitre
Definition length.hpp:174
constexpr auto centilitre
Definition length.hpp:170
constexpr auto decilitre
Definition length.hpp:172
constexpr auto cubic_inch
Definition length.hpp:217
constexpr auto petaliter
Definition length.hpp:199
constexpr auto decalitre
Definition length.hpp:181
constexpr auto petalitre
Definition length.hpp:198
Base namespace for units.
Definition angle.hpp:24
MTCORE_DEF_UNIT_NAME_NO_ABBREV(length, twip)
MTCORE_DEF_UNIT_NAME_DIFF_US(length, attometre, attometer, am)
MTCORE_DEF_UNIT_NAME_SYM(length, inch, in, "″")
MTCORE_DEF_UNIT_NAME(length, thou, th)
Template for storing unit names.
Represents a single unit of measurement.
List of units.
#define MTCORE_DEF_UNIT_NAME_NO_ABBREV(NS, UNIT)
#define MTCORE_DEF_UNIT_NAME_SI(NS, UNIT, US_NAME, ABBREV)
#define MTCORE_DEF_UNIT_NAME_STRS(NS, UNIT, NAME, US_NAME, ABBREV, ABBREV_ASCII, ABBREV_US, SYMBOL)
#define MTCORE_DEF_UNIT_NAME(NS, UNIT, ABBREV)