Project
Loading...
Searching...
No Matches
conversions.h
Go to the documentation of this file.
1
4
5#pragma once
6#include <string>
7#include <SI/literals.h>
8
9namespace SI
10{
11 inline auto to_string_format = "%.2Lf %s";
12 inline auto to_equivalent_format = "%.2Lf %s";
13 inline auto to_percentage_format = "%.0Lf %s";
14
16 inline bool from_string(const std::string& str, length& result)
17 {
18 double value;
19 char unit[1024];
20 if (std::sscanf(str.c_str(), "%lf%s", &value, unit) != 2)
21 return false; // not recognized
22
23 if (unit == "Gm")
24 result = 1_Gm * value;
25 else if (unit == "Mm")
26 result = 1_Mm * value;
27 else if (unit == "km")
28 result = 1_km * value;
29 else if (unit == "m")
30 result = 1_m * value;
31 else if (unit == "dm")
32 result = 1_dm * value;
33 else if (unit == "cm")
34 result = 1_cm * value;
35 else if (unit == "mm")
36 result = 1_mm * value;
37 else if (unit == "um")
38 result = 1_um * value;
39 else if (unit == "nm")
40 result = 1_nm * value;
41 else if (unit == "pm")
42 result = 1_pm * value;
43 else
44 return false; // unknown unit
45
46 return true;
47 }
48
49 inline bool from_string(const std::string& str, time& result)
50 {
51 double value;
52 char unit[1024];
53 if (std::sscanf(str.c_str(), "%lf%s", &value, unit) != 2)
54 return false; // not recognized
55
56 if (unit == "day" || unit == "days")
57 result = 1_day * value;
58 else if (unit == "h" || unit == "hrs")
59 result = 1_h * value;
60 else if (unit == "m" || unit == "min")
61 result = 1_min * value;
62 else if (unit == "s" || unit == "sec" || unit == "seconds")
63 result = 1_s * value;
64 else if (unit == "ms")
65 result = 1_ms * value;
66 else if (unit == "us")
67 result = 1_us * value;
68 else if (unit == "ns")
69 result = 1_ns * value;
70 else if (unit == "ps")
71 result = 1_ps * value;
72 else
73 return false; // unknown unit
74
75 return true;
76 }
77
78 inline bool from_string(const std::string& str, mass& result)
79 {
80 double value;
81 char unit[1024];
82 if (std::sscanf(str.c_str(), "%lf%s", &value, unit) != 2)
83 return false; // not recognized
84
85 if (unit == "Gt")
86 result = 1_Gt * value;
87 else if (unit == "Mt")
88 result = 1_Mt * value;
89 else if (unit == "kt")
90 result = 1_kt * value;
91 else if (unit == "t")
92 result = 1_t * value;
93 else if (unit == "kg")
94 result = 1_kg * value;
95 else if (unit == "g")
96 result = 1_g * value;
97 else if (unit == "mg")
98 result = 1_mg * value;
99 else if (unit == "ug")
100 result = 1_ug * value;
101 else if (unit == "ng")
102 result = 1_ng * value;
103 else
104 return false; // unknown unit
105
106 return true;
107 }
108
109 // internal function to join and format both quantity and unit into a string.
110 inline std::string _join(long double quantity, const char* unit, const char* formatString)
111 {
112 char buf[256];
113 std::snprintf(buf, sizeof(buf), formatString, quantity, unit);
114 return std::string(buf);
115 }
116
117 // convert the 7 SI base units:
118
120 inline std::string to_string(const length d)
121 {
122 if (d <= -1_Gpc || d >= 1_Gpc)
123 return _join(d / 1_Gpc, "Gpc", to_string_format); // gigaparsec
124 if (d <= -1_Mpc || d >= 1_Mpc)
125 return _join(d / 1_Mpc, "Mpc", to_string_format); // megaparsec
126 if (d <= -1_kpc || d >= 1_kpc)
127 return _join(d / 1_kpc, "kpc", to_string_format); // kiloparsec
128 if (d <= -1_pc || d >= 1_pc)
129 return _join(d / 1_pc, "pc", to_string_format); // parsec
130 if (d <= -1_ly || d >= 1_ly)
131 return _join(d / 1_ly, "ly", to_string_format); // light-years
132 if (d <= -0.1_au || d >= 0.1_au)
133 return _join(d / 1_au, "au", to_string_format); // astronomical unit
134 if (d <= -1_km || d >= 1_km)
135 return _join(d / 1_km, "km", to_string_format);
136 if (d <= -1_m || d >= 1_m || d == 0.0_m)
137 return _join(d / 1_m, "m", to_string_format);
138 if (d <= -1_cm || d >= 1_cm)
139 return _join(d / 1_cm, "cm", to_string_format);
140 if (d <= -1_mm || d >= 1_mm)
141 return _join(d / 1_mm, "mm", to_string_format);
142 if (d <= -1_um || d >= 1_um)
143 return _join(d / 1_um, "μm", to_string_format);
144 if (d <= -1_nm || d >= 1_nm)
145 return _join(d / 1_nm, "nm", to_string_format);
146 return _join(d / 1_pm, "pm", to_string_format);
147 }
148
150 inline std::string to_equivalent(const length d)
151 {
152 if (d <= -1_ly || d >= 1_ly)
153 return _join(d / 1_ly, "light-years", to_equivalent_format);
154 if (d <= -1_ls || d >= 1_ls)
155 return _join(d / 1_ls, "light-seconds", to_equivalent_format);
156 if (d <= -1_mi || d >= 1_mi)
157 return _join(d / 1_mi, "miles", to_equivalent_format);
158 if (d <= -1_yd || d >= 1_yd)
159 return _join(d / 1_yd, "yards", to_equivalent_format);
160 if (d <= -1_ft || d >= 1_ft)
161 return _join(d / 1_ft, "feet", to_equivalent_format);
162 return _join(d / 1_in, "inch", to_equivalent_format);
163 }
164
165 inline std::ostream& operator<<(std::ostream& os, const length d)
166 {
167 os << to_string(d);
168 return os;
169 }
170
172 inline std::string to_string(const time t)
173 {
174 if (abs(t) > 365.25_days)
175 return _join(t / 365.25_days, "y", to_string_format);
176 if (abs(t) > 10_days)
177 return _join(t / 7_days, "w", to_string_format);
178 if (abs(t) > 2_day)
179 return _join(t / 1_day, "d", to_string_format);
180 if (t <= -1_h || t >= 1_h)
181 return _join(t / 1_h, "h", to_string_format);
182 if (t <= -1_min || t >= 1_min)
183 return _join(t / 1_min, "min", to_string_format);
184 if (t <= -1_s || t >= 1_s || t == 0.0_s)
185 return _join(t / 1_s, "s", to_string_format);
186 if (t <= -1_ms || t >= 1_ms)
187 return _join(t / 1_ms, "ms", to_string_format);
188 if (t <= -1_us || t >= 1_us)
189 return _join(t / 1_us, "μs", to_string_format);
190 if (t <= -1_ns || t >= 1_ns)
191 return _join(t / 1_ns, "ns", to_string_format);
192 return _join(t / 1_ps, "ps", to_string_format);
193 }
194
195 inline std::ostream& operator<<(std::ostream& os, const time t)
196 {
197 os << to_string(t);
198 return os;
199 }
200
202 inline std::string to_string(const mass m)
203 {
204 if (m <= -1_Pt || m >= 1_Pt)
205 return _join(m / 1_Pt, "Pt", to_string_format);
206 if (m <= -1_Tt || m >= 1_Tt)
207 return _join(m / 1_Tt, "Tt", to_string_format);
208 if (m <= -1_Gt || m >= 1_Gt)
209 return _join(m / 1_Gt, "Gt", to_string_format);
210 if (m <= -1_Mt || m >= 1_Mt)
211 return _join(m / 1_Mt, "Mt", to_string_format);
212 if (m <= -1_kt || m >= 1_kt)
213 return _join(m / 1_kt, "kt", to_string_format);
214 if (m <= -1_t || m >= 1_t)
215 return _join(m / 1_t, "t", to_string_format);
216 if (m <= -1_kg || m >= 1_kg || m == 0.0_kg)
217 return _join(m / 1_kg, "kg", to_string_format);
218 if (m <= -1_g || m >= 1_g)
219 return _join(m / 1_g, "g", to_string_format);
220 if (m <= -1_mg || m >= 1_mg)
221 return _join(m / 1_mg, "mg", to_string_format);
222 if (m <= -1_ug || m >= 1_ug)
223 return _join(m / 1_ug, "µg", to_string_format);
224 return _join(m / 1_ng, "ng", to_string_format);
225 }
226
228 inline std::string to_equivalent(const mass m)
229 {
230 if (m <= -1_Msun || m >= 1_Msun)
231 return _join(m / 1_Msun, "solar masses", to_equivalent_format);
232 if (m <= -1_Mjup || m >= 1_Mjup)
233 return _join(m / 1_Mjup, "Jupiter masses", to_equivalent_format);
234 if (m <= -1_Mearth || m >= 1_Mearth)
235 return _join(m / 1_Mearth, "Earth masses", to_equivalent_format);
236 if (m <= -1_Mmoon || m >= 1_Mmoon)
237 return _join(m / 1_Mmoon, "Moon masses", to_equivalent_format);
238 if (m <= -1_lb || m >= 1_lb)
239 return _join(m / 1_lb, "lb", to_equivalent_format);
240 return _join(m / 1_oz, "oz", to_equivalent_format);
241 }
242
243 inline std::ostream& operator<<(std::ostream& os, const mass m)
244 {
245 os << to_string(m);
246 return os;
247 }
248
250 inline std::string to_string(const temperature T)
251 {
252 if (T >= 1_GK)
253 return _join(T / 1_GK, "GK", to_string_format);
254 if (T >= 1_MK)
255 return _join(T / 1_MK, "MK", to_string_format);
256 if (T >= -150_degC && T <= 150_degC) // human temperature range
257 return _join(celsius(T), "°C", to_string_format);
258 if (T <= -1_K || T >= 1_K || T == 0.0_K)
259 return _join(T / 1_K, "K", to_string_format);
260 if (T <= -1_mK || T >= 1_mK)
261 return _join(T / 1_mK, "mK", to_string_format);
262 if (T <= -1_uK || T >= 1_uK)
263 return _join(T / 1_uK, "μK", to_string_format);
264 return _join(T / 1_nK, "nK", to_string_format);
265 }
266
268 inline std::string to_equivalent(const temperature T)
269 {
270 return _join(fahrenheit(T), "°F", to_equivalent_format);
271 }
272
273 inline std::ostream& operator<<(std::ostream& os, const temperature T)
274 {
275 os << to_string(T);
276 return os;
277 }
278
280 inline std::string to_string(const electric_current I)
281 {
282 if (I <= -1_GA || I >= 1_GA)
283 return _join(I / 1_GA, "GA", to_string_format);
284 if (I <= -1_MA || I >= 1_MA)
285 return _join(I / 1_MA, "MA", to_string_format);
286 if (I <= -1_kA || I >= 1_kA)
287 return _join(I / 1_kA, "kA", to_string_format);
288 if (I <= -1_A || I >= 1_A || I == 0.0_A)
289 return _join(I / 1_A, "A", to_string_format);
290 if (I <= -1_mA || I >= 1_mA)
291 return _join(I / 1_mA, "mA", to_string_format);
292 if (I <= -1_uA || I >= 1_uA)
293 return _join(I / 1_uA, "μA", to_string_format);
294 if (I <= -1_nA || I >= 1_nA)
295 return _join(I / 1_nA, "nA", to_string_format);
296 return _join(I / 1_pA, "pA", to_string_format);
297 }
298
299 // Convert the 22 derived SI units:
300
302 inline std::string to_string(const area a)
303 {
304 if (a <= -1_km² || a >= 1_km²)
305 return _join(a / 1_km², "km²", to_string_format);
306 if (a <= -1_hm² || a >= 1_hm²)
307 return _join(a / 1_hm², "hm²", to_string_format);
308 if (a <= -1_m² || a >= 1_m² || a == 0.0_m²)
309 return _join(a / 1_m², "m²", to_string_format);
310 if (a <= -1_cm² || a >= 1_cm²)
311 return _join(a / 1_cm², "cm²", to_string_format);
312 if (a <= -1_mm² || a >= 1_mm²)
313 return _join(a / 1_mm², "mm²", to_string_format);
314 return _join(a / 1_um², "μm²", to_string_format);
315 }
316
317 inline std::ostream& operator<<(std::ostream& os, const area a)
318 {
319 os << to_string(a);
320 return os;
321 }
322
324 inline std::string to_string(const per_area a)
325 {
326 if (a <= -1_per_km² || a >= 1_per_km²)
327 return _join(a / 1_per_km², "/km²", to_string_format);
328 if (a <= -1_per_hm² || a >= 1_per_hm²)
329 return _join(a / 1_per_hm², "/hm²", to_string_format);
330 if (a <= -1_per_m² || a >= 1_per_m² || a == 0.0_per_m²)
331 return _join(a / 1_per_m², "/m²", to_string_format);
332 if (a <= -1_per_cm² || a >= 1_per_cm²)
333 return _join(a / 1_per_cm², "/cm²", to_string_format);
334 if (a <= -1_per_mm² || a >= 1_per_mm²)
335 return _join(a / 1_per_mm², "/mm²", to_string_format);
336 return _join(a / 1_per_μm², "/μm²", to_string_format);
337 }
338
339 inline std::ostream& operator<<(std::ostream& os, const per_area a)
340 {
341 os << to_string(a);
342 return os;
343 }
344
346 inline std::string to_string(const volume v)
347 {
348 if (v <= -1_km³ || v >= 1_km³)
349 return _join(v / 1_km³, "km³", to_string_format);
350 if (v <= -1_m³ || v >= 1_m³)
351 return _join(v / 1_m³, "m³", to_string_format);
352 if (v <= -1_l || v >= 1_l || v == 0.0_l)
353 return _join(v / 1_l, "l", to_string_format);
354 if (v <= -1_ml || v >= 1_ml)
355 return _join(v / 1_ml, "ml", to_string_format);
356 if (v <= -1_ul || v >= 1_ul)
357 return _join(v / 1_ul, "μl", to_string_format);
358 if (v <= -1_nl || v >= 1_nl)
359 return _join(v / 1_nl, "nl", to_string_format);
360 return _join(v / 1_pl, "pl", to_string_format);
361 }
362
363 inline std::ostream& operator<<(std::ostream& os, const volume v)
364 {
365 os << to_string(v);
366 return os;
367 }
368
370 inline std::string to_string(const velocity v)
371 {
372 if (v <= -1_km_per_s || v >= 1_km_per_s)
373 return _join(v / 1_km_per_s, "km/s", to_string_format);
374 if (v <= -1_km_per_h || v >= 1_km_per_h)
375 return _join(v / 1_km_per_h, "km/h", to_string_format);
376 if (v <= -1_m_per_s || v >= 1_m_per_s || v == 0.0_m_per_s)
377 return _join(v / 1_m_per_s, "m/s", to_string_format);
378 return _join(v / 1_mm_per_h, "mm/h", to_string_format);
379 }
380
382 inline std::string to_equivalent(const velocity V)
383 {
384 if (V <= -1_Mach || V >= 1_Mach)
385 return _join(V / 1_Mach, "Mach", to_equivalent_format);
386 return _join(V / 1_mph, "mph", to_equivalent_format);
387 }
388
389 inline std::ostream& operator<<(std::ostream& os, const velocity v)
390 {
391 os << to_string(v);
392 return os;
393 }
394
396 inline std::string to_string(const acceleration a)
397 {
398 if (a <= -1_km_per_s² || a >= 1_km_per_s²)
399 return _join(a / 1_km_per_s², "km/s", to_string_format);
400 return _join(a / 1_m_per_s², "m/s²", to_string_format);
401 }
402
403 inline std::ostream& operator<<(std::ostream& os, const acceleration a)
404 {
405 os << to_string(a);
406 return os;
407 }
408
410 inline std::string to_string(const frequency f)
411 {
412 if (f <= -1_THz || f >= 1_THz)
413 return _join(f / 1_THz, "THz", to_string_format);
414 if (f <= -1_GHz || f >= 1_GHz)
415 return _join(f / 1_GHz, "GHz", to_string_format);
416 if (f <= -1_MHz || f >= 1_MHz)
417 return _join(f / 1_MHz, "MHz", to_string_format);
418 if (f <= -1_kHz || f >= 1_kHz)
419 return _join(f / 1_kHz, "kHz", to_string_format);
420 if (f <= -1_Hz || f >= 1_Hz || f == 0.0_Hz)
421 return _join(f / 1_Hz, "Hz", to_string_format);
422 return _join(f / 1_mHz, "mHz", to_string_format);
423 }
424
425 inline std::ostream& operator<<(std::ostream& os, const frequency f)
426 {
427 os << to_string(f);
428 return os;
429 }
430
432 inline std::string to_string(const force F)
433 {
434 if (F <= -1_ZN || F >= 1_ZN)
435 return _join(F / 1_ZN, "ZN", to_string_format);
436 if (F <= -1_EN || F >= 1_EN)
437 return _join(F / 1_EN, "EN", to_string_format);
438 if (F <= -1_PN || F >= 1_PN)
439 return _join(F / 1_PN, "PN", to_string_format);
440 if (F <= -1_TN || F >= 1_TN)
441 return _join(F / 1_TN, "TN", to_string_format);
442 if (F <= -1_GN || F >= 1_GN)
443 return _join(F / 1_GN, "GN", to_string_format);
444 if (F <= -1_MN || F >= 1_MN)
445 return _join(F / 1_MN, "MN", to_string_format);
446 if (F <= -1_kN || F >= 1_kN)
447 return _join(F / 1_kN, "kN", to_string_format);
448 if (F <= -1_N || F >= 1_N || F == 0.0_N)
449 return _join(F / 1_N, "N", to_string_format);
450 if (F <= -1_mN || F >= 1_mN)
451 return _join(F / 1_mN, "mN", to_string_format);
452 if (F <= -1_uN || F >= 1_uN)
453 return _join(F / 1_uN, "µN", to_string_format);
454 return _join(F / 1_pN, "pN", to_string_format);
455 }
456
457 inline std::ostream& operator<<(std::ostream& os, const force F)
458 {
459 os << to_string(F);
460 return os;
461 }
462
464 inline std::string to_string(const energy E)
465 {
466 if (E <= -1_PJ || E >= 1_PJ)
467 return _join(E / 1_PJ, "PJ", to_string_format);
468 if (E <= -1_TJ || E >= 1_TJ)
469 return _join(E / 1_TJ, "TJ", to_string_format);
470 if (E <= -1_GJ || E >= 1_GJ)
471 return _join(E / 1_GJ, "GJ", to_string_format);
472 if (E <= -1_MJ || E >= 1_MJ)
473 return _join(E / 1_MJ, "MJ", to_string_format);
474 if (E <= -1_kJ || E >= 1_kJ)
475 return _join(E / 1_kJ, "kJ", to_string_format);
476 if (E <= -1_J || E >= 1_J || E == 0.0_J)
477 return _join(E / 1_J, "J", to_string_format);
478 return _join(E / 1_mJ, "mJ", to_string_format);
479 }
480
481 inline std::ostream& operator<<(std::ostream& os, const energy E)
482 {
483 os << to_string(E);
484 return os;
485 }
486
488 inline std::string to_string(const power P)
489 {
490 if (P <= -1_TW || P >= 1_TW)
491 return _join(P / 1_TW, "TW", to_string_format);
492 if (P <= -1_GW || P >= 1_GW)
493 return _join(P / 1_GW, "GW", to_string_format);
494 if (P <= -1_MW || P >= 1_MW)
495 return _join(P / 1_MW, "MW", to_string_format);
496 if (P <= -1_kW || P >= 1_kW)
497 return _join(P / 1_kW, "kW", to_string_format);
498 return _join(P / 1_W, "W", to_string_format);
499 }
500
501 inline std::ostream& operator<<(std::ostream& os, const power P)
502 {
503 os << to_string(P);
504 return os;
505 }
506
508 inline std::string to_string(const power_intensity I)
509 {
510 if (I <= -1_MW_per_m² || I >= 1_MW_per_m²)
511 return _join(I / 1_MW_per_m², "MW/m²", to_string_format);
512 if (I <= -1_kW_per_m² || I >= 1_kW_per_m²)
513 return _join(I / 1_kW_per_m², "kW/m²", to_string_format);
514 if (I <= -1_W_per_m² || I >= 1_W_per_m²)
515 return _join(I / 1_W_per_m², "W/m²", to_string_format);
516 return _join(I / 1_mW_per_m², "mW/m²", to_string_format);
517 }
518
519 inline std::ostream& operator<<(std::ostream& os, const power_intensity I)
520 {
521 os << to_string(I);
522 return os;
523 }
524
526 inline std::string to_string(const pressure p)
527 {
528 if (p <= -1_MPa || p >= 1_MPa)
529 return _join(p / 1_MPa, "MPa", to_string_format);
530 if (p <= -1_kPa || p >= 1_kPa)
531 return _join(p / 1_kPa, "kPa", to_string_format);
532 if (p <= -1_hPa || p >= 1_hPa)
533 return _join(p / 1_hPa, "hPa", to_string_format);
534 if (p <= -1_Pa || p >= 1_Pa || p == 0.0_Pa)
535 return _join(p / 1_Pa, "Pa", to_string_format);
536 if (p <= -1_mPa || p >= 1_mPa)
537 return _join(p / 1_mPa, "mPa", to_string_format);
538 return _join(p / 1_uPa, "µPa", to_string_format);
539 }
540
541 inline std::ostream& operator<<(std::ostream& os, const pressure p)
542 {
543 os << to_string(p);
544 return os;
545 }
546
548 inline std::string to_string(const electric_potential U)
549 {
550 if (U <= -1_GV || U >= 1_GV)
551 return _join(U / 1_GV, "GV", to_string_format);
552 if (U <= -1_MV || U >= 1_MV)
553 return _join(U / 1_MV, "MV", to_string_format);
554 if (U <= -1_kV || U >= 1_kV)
555 return _join(U / 1_kV, "kV", to_string_format);
556 if (U <= -1_V || U >= 1_V || U == 0.0_V)
557 return _join(U / 1_V, "V", to_string_format);
558 if (U <= -1_mV || U >= 1_mV)
559 return _join(U / 1_mV, "mV", to_string_format);
560 if (U <= -1_uV || U >= 1_uV)
561 return _join(U / 1_uV, "μV", to_string_format);
562 if (U <= -1_nV || U >= 1_nV)
563 return _join(U / 1_nV, "nV", to_string_format);
564 return _join(U / 1_pV, "pV", to_string_format);
565 }
566
567 inline std::ostream& operator<<(std::ostream& os, const electric_potential U)
568 {
569 os << to_string(U);
570 return os;
571 }
572
574 inline std::string to_string(const electric_charge Q)
575 {
576 if (Q <= -1_GAh || Q >= 1_GAh)
577 return _join(Q / 1_MAh, "MAh", to_string_format);
578 if (Q <= -1_MAh || Q >= 1_MAh)
579 return _join(Q / 1_MAh, "MAh", to_string_format);
580 if (Q <= -1_kAh || Q >= 1_kAh)
581 return _join(Q / 1_kAh, "kAh", to_string_format);
582 if (Q <= -1_Ah || Q >= 1_Ah || Q == 0.0_Ah)
583 return _join(Q / 1_Ah, "Ah", to_string_format);
584 if (Q <= -1_mAh || Q >= 1_mAh)
585 return _join(Q / 1_mAh, "mAh", to_string_format);
586 return _join(Q / 1_uAh, "µAh", to_string_format);
587 }
588
589 inline std::string to_string(const mass_per_area m)
590 {
591 if (m <= -1_t_per_m² || m >= 1_t_per_m²)
592 return _join(m / 1_t_per_m², "t/m²", to_string_format);
593 return _join(m / 1_kg_per_m², "kg/m²", to_string_format);
594 }
595
597 inline std::string to_string(const mass_per_power m)
598 {
599 if (m <= -1_kg_per_kW || m >= 1_kg_per_kW)
600 return _join(m / 1_kg_per_kW, "kg/kW", to_string_format);
601 return _join(m / 1_kg_per_W, "kg/W", to_string_format);
602 }
603
604 inline std::ostream& operator<<(std::ostream& os, const mass_per_power m)
605 {
606 os << to_string(m);
607 return os;
608 }
609
611 inline std::string to_string(const density d)
612 {
613 if (d <= -1_t_per_m³ || d >= 1_t_per_m³)
614 return _join(d / 1_t_per_m³, "t/m³", to_string_format);
615 return _join(d / 1_kg_per_m³, "kg/m³", to_string_format);
616 }
617
619 inline std::string to_string(const angle a)
620 {
621 return _join(a / 1_deg, "°", to_string_format);
622 }
623
624 inline std::string to_string(const dimensionless value)
625 {
626 return _join(value, "", to_string_format);
627 }
628
629 inline std::string to_string(const unsigned char byte)
630 {
631 char buf[256];
632 std::snprintf(buf, sizeof(buf), "%u", byte);
633 return std::string(buf);
634 }
635
636 inline std::string to_string(const char glyph)
637 {
638 char buf[256];
639 std::snprintf(buf, sizeof(buf), "%c", glyph);
640 return std::string(buf);
641 }
642
643 inline std::string to_string(const std::string& text)
644 {
645 return text;
646 }
647
649 inline std::string to_equivalent(const power_intensity I)
650 {
651 return _join(10.0 * std::log10((I / 1_W_per_m²) / 1e-12), "dB", to_equivalent_format);
652 }
653
655 inline std::string to_equivalent(const energy E)
656 {
657 const auto Hiroshima_bomb = 62_TJ; // (explosion energy of the Hiroshima bomb)
658 if (E >= Hiroshima_bomb)
659 return _join(E / Hiroshima_bomb, "Hiroshima bombs", to_equivalent_format);
660
661 const auto one_kg_TNT = 4.184_MJ; // (explosion energy of 1kg Trinitrotoluol)
662 mass kgTNT = kilograms(E / one_kg_TNT);
663 return to_string(kgTNT) + " TNT";
664 }
665
667 inline std::string to_percentage(length part, length total)
668 {
669 return _join((total / part) * 100.0, "%", to_percentage_format);
670 }
671
672} // end of namespace SI
673
674// References
675// ----------
676// 1. https://en.wikipedia.org/wiki/International_System_of_Units
Defines 297 literals, e.g. 24_h.
Definition constants.h:12
std::string to_string(const length d)
Returns the given length as string using SI units.
Definition conversions.h:120
SI_INLINE_CONSTEXPR quantity< Dimension, T > abs(const quantity< Dimension, T > &x)
Definition internal.h:563
auto to_percentage_format
configurable
Definition conversions.h:13
detail::unit< detail::dimension_of_t< Dimension >, detail::ratio< numerator, denumerator > > unit
Definition internal.h:829
std::ostream & operator<<(std::ostream &os, const length d)
Definition conversions.h:165
auto to_string_format
configurable, SI standard formatting by default
Definition conversions.h:11
double dimensionless
to hold a quantity without any unit, e.g. 42
Definition datatypes.h:18
auto to_equivalent_format
configurable, SI standard formatting by default
Definition conversions.h:12
std::string to_equivalent(const length d)
Returns a length equivalent as string, e.g. in Imperial units.
Definition conversions.h:150
bool from_string(const std::string &str, length &result)
The from_string() function.
Definition conversions.h:16
std::string _join(long double quantity, const char *unit, const char *formatString)
Definition conversions.h:110
long double angle
basic datatype to hold a dimensionless angle, e.g. 3.1415
Definition internal.h:14
std::string to_percentage(length part, length total)
Returns the percentage as string, e.g. 50.0%.
Definition conversions.h:667