|
quanta 0.1.0
Type-safe units for temperature, frequency, electrical and spatial quantities, modeled after std::chrono
|
Type-safe temperature handling, modeled after std::chrono. Include <quanta/thermo>.
double) types for display and computationstd::format support** (when available)| Type | Scale | Precision |
|---|---|---|
celsius | Celsius | 1°C |
decicelsius | Celsius | 0.1°C |
millicelsius | Celsius | 0.001°C |
kelvin | Kelvin | 1 K |
decikelvin | Kelvin | 0.1 K |
millikelvin | Kelvin | 0.001 K |
fahrenheit | Fahrenheit | 1°F |
decifahrenheit | Fahrenheit | 0.1°F |
millifahrenheit | Fahrenheit | 0.001°F |
See Formatting below for how these render as strings.
| Type | Precision |
|---|---|
delta_celsius / delta_kelvin | 1° |
delta_decicelsius / delta_decikelvin | 0.1° |
delta_millicelsius / delta_millikelvin | 0.001° |
delta_fahrenheit | 1°F (= 5/9°C) |
delta_decifahrenheit | 0.1°F |
delta_millifahrenheit | 0.001°F |
The _Δ… literals are unavailable on GCC before 14, which cannot lex non-ASCII literal suffixes (QUANTA_HAS_UTF8_LITERALS is 0 there). The _delta_… forms are their ASCII alternatives and are always available.
Unary minus is available on relative scales (Celsius, Fahrenheit) so that negative literals like -123_dc work — the sign is applied to the value the literal produces, i.e. -(123_dc). It is deliberately not available on the absolute Kelvin scale, where a negated temperature would fall below absolute zero; -25_k is a compile error. Deltas are negatable on every scale.
to_string() and std::format render a temperature or delta in its own unit — the scale's degree with the precision's SI prefix, and a Δ marker for deltas. {} prints the exact stored count; any other format spec changes only how the number is rendered (as a double), never the unit:
A single leading marker letter in the spec opts into another unit; the rest of the spec is a normal floating-point format spec:
| Marker | Unit | Example |
|---|---|---|
b | Base unit: the unprefixed degree of the same scale (°C, K or °F; never converted to another scale) | {:b.1f} of millicelsius(22534) → 22.5°C |
s | Scaled: temperatures are not rescaled, so this is the type's own unit | {:s.1f} of millicelsius(22534) → 22534.0m°C |
With nothing after the marker ({:b}, {:s}), the value is rendered in the shortest fixed notation that round-trips — never scientific notation. The same forms are available without <format> through tag arguments to to_string():
Because a spec starting with b or s is always a marker, those letters cannot be used as the fill character at the start of a spec ({:b>8} is the b marker followed by >8).
Floating-point representations (e.g. temperature<celsius_scale, delta<double>>) render with no spec in the same shortest round-trip fixed notation, so to_string and {} agree: 22.5°C, 1000000°C.
Temperature precisions without an SI prefix (e.g. temperature<celsius_scale, delta<int64_t, std::centi>>) have no unit label of their own: {} is a format error, and any other spec or marker renders the value in the base unit.
Conversions follow the same philosophy as std::chrono:
celsius → millicelsius)millicelsius → celsius), or from a floating-point representation to an integral oneEvery conversion that temperature_cast can perform has exactly one of these constructors, as with the other domains' quantities.
Deltas convert like the other domains' quantities: implicitly when lossless, and otherwise explicitly (by constructor or delta_cast, both truncating). floor, ceil, trunc and round give explicit rounding control (a target whose rep is not integral, such as a floating-point one, is not rounded). abs gives a delta's magnitude.