|
quanta 0.1.0
Type-safe units for temperature, frequency, electrical and spatial quantities, modeled after std::chrono
|
Temperature types and utilities. More...
Classes | |
| class | delta |
| A temperature difference with a representation and precision. More... | |
| struct | is_delta |
| Trait to detect delta specializations. More... | |
| struct | is_delta< delta< Rep, Precision > > |
| class | temperature |
| An absolute temperature on a given scale. More... | |
Typedefs | |
| using | delta_celsius = delta< int64_t > |
| Delta with 1 degree precision (Celsius/Kelvin). | |
| using | delta_decicelsius = delta< int64_t, std::deci > |
| Delta with 0.1 degree precision (Celsius/Kelvin). | |
| using | delta_millicelsius = delta< int64_t, std::milli > |
| Delta with 0.001 degree precision (Celsius/Kelvin). | |
| using | delta_kelvin = delta< int64_t > |
| Delta with 1 degree precision (Celsius/Kelvin). | |
| using | delta_decikelvin = delta< int64_t, std::deci > |
| Delta with 0.1 degree precision (Celsius/Kelvin). | |
| using | delta_millikelvin = delta< int64_t, std::milli > |
| Delta with 0.001 degree precision (Celsius/Kelvin). | |
| using | delta_fahrenheit = delta< int64_t, std::ratio< 5, 9 > > |
| Delta with 1°F precision. | |
| using | delta_decifahrenheit = delta< int64_t, std::ratio< 5, 90 > > |
| Delta with 0.1°F precision. | |
| using | delta_millifahrenheit = delta< int64_t, std::ratio< 5, 9000 > > |
| Delta with 0.001°F precision. | |
| using | celsius = temperature< celsius_scale > |
| Celsius with 1 degree precision. | |
| using | decicelsius = temperature< celsius_scale, delta< int64_t, std::deci > > |
| Celsius with 0.1 degree precision. | |
| using | millicelsius = temperature< celsius_scale, delta< int64_t, std::milli > > |
| Celsius with 0.001 degree precision. | |
| using | kelvin = temperature< kelvin_scale > |
| Kelvin with 1 degree precision. | |
| using | decikelvin = temperature< kelvin_scale, delta< int64_t, std::deci > > |
| Kelvin with 0.1 degree precision. | |
| using | millikelvin = temperature< kelvin_scale, delta< int64_t, std::milli > > |
| Kelvin with 0.001 degree precision. | |
| using | fahrenheit = temperature< fahrenheit_scale, delta< int64_t, std::ratio< 5, 9 > > > |
| Fahrenheit with 1 degree precision. | |
| using | decifahrenheit = temperature< fahrenheit_scale, delta< int64_t, std::ratio< 5, 90 > > > |
| Fahrenheit with 0.1 degree precision. | |
| using | millifahrenheit = temperature< fahrenheit_scale, delta< int64_t, std::ratio< 5, 9000 > > > |
| Fahrenheit with 0.001 degree precision. | |
Functions | |
| template<typename ToDelta , typename Rep , typename Precision > requires detail::_quantity_cast_requirements<delta<Rep, Precision>, ToDelta> | |
| constexpr ToDelta | delta_cast (const delta< Rep, Precision > &d) |
| Converts a delta to a different precision or representation. | |
| template<typename ToDelta , typename Rep , typename Precision > | |
| constexpr ToDelta | ceil (const delta< Rep, Precision > &d) |
| Rounds a delta up to the nearest representable value in the target precision. | |
| template<typename ToDelta , typename Rep , typename Precision > | |
| constexpr ToDelta | floor (const delta< Rep, Precision > &d) |
| Rounds a delta down to the nearest representable value in the target precision. | |
| template<typename ToDelta , typename Rep , typename Precision > | |
| constexpr ToDelta | trunc (const delta< Rep, Precision > &d) |
| Rounds a delta toward zero to the nearest representable value in the target precision. | |
| template<typename ToDelta , typename Rep , typename Precision > | |
| constexpr ToDelta | round (const delta< Rep, Precision > &d) |
| Rounds a delta to the nearest representable value in the target precision. | |
| template<typename Rep , typename Precision > | |
| constexpr delta< Rep, Precision > | abs (const delta< Rep, Precision > &d) |
| Returns the absolute value of a delta. | |
| template<typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > | |
| constexpr auto | operator+ (const delta< Rep1, Precision1 > &lhs, const delta< Rep2, Precision2 > &rhs) -> std::common_type_t< delta< Rep1, Precision1 >, delta< Rep2, Precision2 > > |
| Returns the sum of two deltas. | |
| template<typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > | |
| constexpr auto | operator- (const delta< Rep1, Precision1 > &lhs, const delta< Rep2, Precision2 > &rhs) -> std::common_type_t< delta< Rep1, Precision1 >, delta< Rep2, Precision2 > > |
| Returns the difference of two deltas. | |
| template<typename Rep1 , typename Precision , typename Rep2 > requires not_delta<Rep2> && std::convertible_to<const Rep2&, std::common_type_t<Rep1, Rep2>> | |
| constexpr auto | operator* (const delta< Rep1, Precision > &d, const Rep2 &r) -> delta< std::common_type_t< Rep1, Rep2 >, Precision > |
| Multiplies a delta by a scalar. | |
| template<typename Rep1 , typename Rep2 , typename Precision > requires not_delta<Rep1> && std::convertible_to<const Rep1&, std::common_type_t<Rep1, Rep2>> | |
| constexpr auto | operator* (const Rep1 &r, const delta< Rep2, Precision > &d) -> delta< std::common_type_t< Rep1, Rep2 >, Precision > |
| Multiplies a scalar by a delta. | |
| template<typename Rep1 , typename Precision , typename Rep2 > requires not_delta<Rep2> && std::convertible_to<const Rep2&, std::common_type_t<Rep1, Rep2>> | |
| constexpr auto | operator/ (const delta< Rep1, Precision > &d, const Rep2 &s) -> delta< std::common_type_t< Rep1, Rep2 >, Precision > |
| Divides a delta by a scalar. | |
| template<typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > | |
| constexpr auto | operator/ (const delta< Rep1, Precision1 > &lhs, const delta< Rep2, Precision2 > &rhs) -> std::common_type_t< Rep1, Rep2 > |
| Divides two deltas, returning a scalar. | |
| template<typename Rep1 , typename Precision , typename Rep2 > requires not_delta<Rep2> && std::convertible_to<const Rep2&, std::common_type_t<Rep1, Rep2>> && (!treat_as_inexact_v<Rep1> && !treat_as_inexact_v<Rep2>) | |
| constexpr auto | operator% (const delta< Rep1, Precision > &d, const Rep2 &s) -> delta< std::common_type_t< Rep1, Rep2 >, Precision > |
| Returns the remainder of dividing a delta by a scalar. | |
| template<typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > requires (!treat_as_inexact_v<Rep1> && !treat_as_inexact_v<Rep2>) | |
| constexpr auto | operator% (const delta< Rep1, Precision1 > &lhs, const delta< Rep2, Precision2 > &rhs) -> std::common_type_t< delta< Rep1, Precision1 >, delta< Rep2, Precision2 > > |
| Returns the remainder of dividing two deltas. | |
| template<typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > | |
| constexpr bool | operator== (const delta< Rep1, Precision1 > &lhs, const delta< Rep2, Precision2 > &rhs) |
| template<typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > requires std::three_way_comparable<std::common_type_t<Rep1, Rep2>> | |
| constexpr auto | operator<=> (const delta< Rep1, Precision1 > &lhs, const delta< Rep2, Precision2 > &rhs) |
| template<typename Rep , typename Precision > | |
| std::string | to_string (const delta< Rep, Precision > &d) |
| Renders a delta as its exact count with a precision-qualified unit, e.g. | |
| template<typename Rep , typename Precision > | |
| std::string | to_string (const delta< Rep, Precision > &d, base_unit_t) |
| Renders a delta in its base unit (Δ°C or Δ°F), see base_unit, e.g. | |
| template<typename Rep , typename Precision > | |
| std::string | to_string (const delta< Rep, Precision > &d, scaled_t) |
| Renders a delta in its scaled form (its own unit), see scaled, e.g. | |
| template<typename ToTemp , typename Scale , typename Delta > requires _temperature_cast_requirements<Scale, Delta, typename ToTemp::scale, typename ToTemp::delta_type> | |
| constexpr ToTemp | temperature_cast (const temperature< Scale, Delta > &t) |
| Converts a temperature to a different scale or precision. | |
| template<typename Scale , typename Delta1 , typename Delta2 > | |
| constexpr auto | difference (const temperature< Scale, Delta1 > &lhs, const temperature< Scale, Delta2 > &rhs) -> std::common_type_t< Delta1, Delta2 > |
| Returns the difference between two temperatures as a delta. | |
| template<typename Scale , typename Delta1 , typename Delta2 > | |
| constexpr auto | operator- (const temperature< Scale, Delta1 > &lhs, const temperature< Scale, Delta2 > &rhs) -> std::common_type_t< Delta1, Delta2 > |
| Subtracts two temperatures on the same scale, giving the delta between them. | |
| template<typename Scale , typename Delta1 , typename Delta2 > | |
| void | operator+ (const temperature< Scale, Delta1 > &lhs, const temperature< Scale, Delta2 > &rhs)=delete |
| Adding two absolute temperatures is meaningless, and ill-formed. | |
| template<typename Scale , typename Delta1 , typename Rep2 , typename Precision2 > | |
| constexpr auto | operator+ (const temperature< Scale, Delta1 > &t, const delta< Rep2, Precision2 > &d) -> temperature< Scale, std::common_type_t< Delta1, delta< Rep2, Precision2 > > > |
| Adds a delta to a temperature. | |
| template<typename Rep1 , typename Precision1 , typename Scale , typename Delta2 > | |
| constexpr auto | operator+ (const delta< Rep1, Precision1 > &d, const temperature< Scale, Delta2 > &t) -> temperature< Scale, std::common_type_t< delta< Rep1, Precision1 >, Delta2 > > |
| Adds a temperature to a delta. | |
| template<typename Scale , typename Delta1 , typename Rep2 , typename Precision2 > | |
| constexpr auto | operator- (const temperature< Scale, Delta1 > &t, const delta< Rep2, Precision2 > &d) -> temperature< Scale, std::common_type_t< Delta1, delta< Rep2, Precision2 > > > |
| Subtracts a delta from a temperature. | |
| template<typename Scale , typename Delta1 , typename Delta2 > | |
| constexpr bool | operator== (const temperature< Scale, Delta1 > &lhs, const temperature< Scale, Delta2 > &rhs) |
| template<typename Scale , typename Delta1 , typename Delta2 > requires std::three_way_comparable<std::common_type_t<typename Delta1::rep, typename Delta2::rep>> | |
| constexpr auto | operator<=> (const temperature< Scale, Delta1 > &lhs, const temperature< Scale, Delta2 > &rhs) |
| template<typename Scale , typename Rep , typename Precision > | |
| std::string | to_string (const temperature< Scale, delta< Rep, Precision > > &t) |
| Renders a temperature as its exact count with a precision-qualified unit, e.g. | |
| template<typename Scale , typename Rep , typename Precision > | |
| std::string | to_string (const temperature< Scale, delta< Rep, Precision > > &t, base_unit_t) |
| Renders a temperature in the unprefixed degree of its own scale (°C, K or °F), see base_unit, e.g. | |
| template<typename Scale , typename Rep , typename Precision > | |
| std::string | to_string (const temperature< Scale, delta< Rep, Precision > > &t, scaled_t) |
| Renders a temperature in its scaled form (its own unit), see scaled, e.g. | |
Temperature types and utilities.
This library provides type-safe temperature handling with support for multiple scales (Celsius, Kelvin, Fahrenheit) and precisions, following the design of std::chrono. It distinguishes between absolute temperatures and temperature differences (deltas).
to_string() and std::format show a temperature or delta in its own unit by default, whatever the format spec: {} prints the exact count and {:.1f} the count as a double, each followed by the precision-qualified unit. A single leading marker letter in the spec, or a tag argument to to_string(), opts into a different unit:
The base unit is the unprefixed degree of the same scale (m°C -> °C, mK -> K, m°F -> °F); temperatures are never converted to another scale. The scaled form (s, see scaled) keeps the type's own unit: temperatures are not rescaled to suit their magnitude.
| using quanta::thermo::celsius = typedef temperature<celsius_scale> |
Celsius with 1 degree precision.
Definition at line 1153 of file thermo.hpp.
| using quanta::thermo::decicelsius = typedef temperature<celsius_scale, delta<int64_t, std::deci> > |
Celsius with 0.1 degree precision.
Definition at line 1155 of file thermo.hpp.
| using quanta::thermo::decifahrenheit = typedef temperature<fahrenheit_scale, delta<int64_t, std::ratio<5, 90> >> |
Fahrenheit with 0.1 degree precision.
Definition at line 1167 of file thermo.hpp.
| using quanta::thermo::decikelvin = typedef temperature<kelvin_scale, delta<int64_t, std::deci> > |
Kelvin with 0.1 degree precision.
Definition at line 1161 of file thermo.hpp.
| using quanta::thermo::delta_celsius = typedef delta<int64_t> |
Delta with 1 degree precision (Celsius/Kelvin).
Definition at line 446 of file thermo.hpp.
| using quanta::thermo::delta_decicelsius = typedef delta<int64_t, std::deci> |
Delta with 0.1 degree precision (Celsius/Kelvin).
Definition at line 448 of file thermo.hpp.
| using quanta::thermo::delta_decifahrenheit = typedef delta<int64_t, std::ratio<5, 90> > |
Delta with 0.1°F precision.
Definition at line 460 of file thermo.hpp.
| using quanta::thermo::delta_decikelvin = typedef delta<int64_t, std::deci> |
Delta with 0.1 degree precision (Celsius/Kelvin).
Definition at line 454 of file thermo.hpp.
| using quanta::thermo::delta_fahrenheit = typedef delta<int64_t, std::ratio<5, 9> > |
Delta with 1°F precision.
Definition at line 458 of file thermo.hpp.
| using quanta::thermo::delta_kelvin = typedef delta<int64_t> |
Delta with 1 degree precision (Celsius/Kelvin).
Definition at line 452 of file thermo.hpp.
| using quanta::thermo::delta_millicelsius = typedef delta<int64_t, std::milli> |
Delta with 0.001 degree precision (Celsius/Kelvin).
Definition at line 450 of file thermo.hpp.
| using quanta::thermo::delta_millifahrenheit = typedef delta<int64_t, std::ratio<5, 9000> > |
Delta with 0.001°F precision.
Definition at line 462 of file thermo.hpp.
| using quanta::thermo::delta_millikelvin = typedef delta<int64_t, std::milli> |
Delta with 0.001 degree precision (Celsius/Kelvin).
Definition at line 456 of file thermo.hpp.
| using quanta::thermo::fahrenheit = typedef temperature<fahrenheit_scale, delta<int64_t, std::ratio<5, 9> >> |
Fahrenheit with 1 degree precision.
Definition at line 1165 of file thermo.hpp.
| using quanta::thermo::kelvin = typedef temperature<kelvin_scale> |
Kelvin with 1 degree precision.
Definition at line 1159 of file thermo.hpp.
| using quanta::thermo::millicelsius = typedef temperature<celsius_scale, delta<int64_t, std::milli> > |
Celsius with 0.001 degree precision.
Definition at line 1157 of file thermo.hpp.
| using quanta::thermo::millifahrenheit = typedef temperature<fahrenheit_scale, delta<int64_t, std::ratio<5, 9000> >> |
Fahrenheit with 0.001 degree precision.
Definition at line 1169 of file thermo.hpp.
| using quanta::thermo::millikelvin = typedef temperature<kelvin_scale, delta<int64_t, std::milli> > |
Kelvin with 0.001 degree precision.
Definition at line 1163 of file thermo.hpp.
|
constexpr |
Returns the absolute value of a delta.
| Rep | Representation type. |
| Precision | Precision type. |
| d | The delta. |
Definition at line 359 of file thermo.hpp.
|
constexpr |
Rounds a delta up to the nearest representable value in the target precision.
Returns the smallest value of type ToDelta that is greater than or equal to d. When converting to the same or finer precision, returns the exact conversion. A target whose rep is not integral (such as a floating-point one) is not rounded: the result is delta_cast<ToDelta>(d).
As with the cast, the result is undefined if it does not fit the target rep.
| ToDelta | Target delta type. |
| Rep | Source representation type. |
| Precision | Source precision. |
| d | The delta to round. |
Definition at line 282 of file thermo.hpp.
|
constexpr |
Converts a delta to a different precision or representation.
For integer-to-integer conversions, this function uses wider intermediate types (128-bit when available) to minimize overflow risk during ratio arithmetic.
| ToDelta | The target delta type. |
| Rep | Source representation type. |
| Precision | Source precision. |
| d | The delta to convert. |
Definition at line 261 of file thermo.hpp.
|
constexpr |
Returns the difference between two temperatures as a delta.
The same as lhs - rhs.
| lhs | First temperature. |
| rhs | Second temperature. |
Definition at line 1091 of file thermo.hpp.
References quanta::thermo::delta< Rep, Precision >::count().
Referenced by operator-().
|
constexpr |
Rounds a delta down to the nearest representable value in the target precision.
Returns the largest value of type ToDelta that is less than or equal to d. When converting to the same or finer precision, returns the exact conversion. A target whose rep is not integral (such as a floating-point one) is not rounded: the result is delta_cast<ToDelta>(d).
As with the cast, the result is undefined if it does not fit the target rep.
| ToDelta | Target delta type. |
| Rep | Source representation type. |
| Precision | Source precision. |
| d | The delta to round. |
Definition at line 303 of file thermo.hpp.
|
constexpr |
Returns the remainder of dividing a delta by a scalar.
Definition at line 417 of file thermo.hpp.
|
constexpr |
Returns the remainder of dividing two deltas.
Definition at line 426 of file thermo.hpp.
|
constexpr |
Multiplies a delta by a scalar.
Definition at line 382 of file thermo.hpp.
|
constexpr |
Multiplies a scalar by a delta.
Definition at line 391 of file thermo.hpp.
|
constexpr |
Adds a temperature to a delta.
Definition at line 1128 of file thermo.hpp.
|
constexpr |
Returns the sum of two deltas.
Definition at line 365 of file thermo.hpp.
|
delete |
Adding two absolute temperatures is meaningless, and ill-formed.
Add a delta to a temperature instead (celsius(20) + delta_celsius(5)).
|
constexpr |
Adds a delta to a temperature.
Definition at line 1120 of file thermo.hpp.
References quanta::thermo::temperature< Scale, Delta >::count().
|
constexpr |
Returns the difference of two deltas.
Definition at line 373 of file thermo.hpp.
|
constexpr |
Subtracts two temperatures on the same scale, giving the delta between them.
As with std::chrono::time_point, the difference of two absolute points is a delta, not a temperature: celsius(25) - celsius(20) == delta_celsius(5). Equivalent to difference(lhs, rhs).
Definition at line 1105 of file thermo.hpp.
References difference().
|
constexpr |
Subtracts a delta from a temperature.
Definition at line 1135 of file thermo.hpp.
References quanta::thermo::temperature< Scale, Delta >::count().
|
constexpr |
Divides a delta by a scalar.
Definition at line 399 of file thermo.hpp.
|
constexpr |
Divides two deltas, returning a scalar.
Definition at line 407 of file thermo.hpp.
References quanta::thermo::delta< Rep, Precision >::count().
|
constexpr |
Definition at line 440 of file thermo.hpp.
References quanta::thermo::delta< Rep, Precision >::count().
|
constexpr |
Definition at line 1148 of file thermo.hpp.
References quanta::thermo::temperature< Scale, Delta >::count().
|
constexpr |
Definition at line 433 of file thermo.hpp.
References quanta::thermo::delta< Rep, Precision >::count().
|
constexpr |
Definition at line 1142 of file thermo.hpp.
References quanta::thermo::temperature< Scale, Delta >::count().
|
constexpr |
Rounds a delta to the nearest representable value in the target precision.
Returns the value of type ToDelta that is nearest to d. When exactly halfway between two values, rounds to the even one, as std::chrono::round does. When converting to the same or finer precision, returns the exact conversion.
As with the cast, the result is undefined if it does not fit the target rep.
| ToDelta | Target delta type. |
| Rep | Source representation type. |
| Precision | Source precision. |
| d | The delta to round. |
Definition at line 340 of file thermo.hpp.
|
constexpr |
Converts a temperature to a different scale or precision.
For integer-to-integer conversions, this function uses wider intermediate types (128-bit when available) to minimize overflow risk during ratio arithmetic. Across scales the intermediate is signed even for unsigned reps, so a negative offset between the scales does not wrap; as with any cast, a result outside the target rep's range is the caller's concern. Without 128-bit integer support (e.g. MSVC, 32-bit targets such as the ESP32), cross-scale integral conversions of counts near the 64-bit limits can overflow the intermediate.
| ToTemp | The target temperature type. |
| Scale | Source scale. |
| Delta | Source delta type. |
| t | The temperature to convert. |
Definition at line 934 of file thermo.hpp.
| std::string quanta::thermo::to_string | ( | const delta< Rep, Precision > & | d | ) |
Renders a delta as its exact count with a precision-qualified unit, e.g.
to_string(delta_millicelsius(1500)) == "1500Δm°C".
Floating-point counts are rendered in the shortest fixed notation that round-trips, as by {} in std::format.
Definition at line 664 of file thermo.hpp.
References quanta::thermo::delta< Rep, Precision >::count().
| std::string quanta::thermo::to_string | ( | const delta< Rep, Precision > & | d, |
| base_unit_t | |||
| ) |
Renders a delta in its base unit (Δ°C or Δ°F), see base_unit, e.g.
to_string(delta_millicelsius(1500), base_unit) == "1.5Δ°C".
Definition at line 673 of file thermo.hpp.
References quanta::thermo::delta< Rep, Precision >::count().
| std::string quanta::thermo::to_string | ( | const delta< Rep, Precision > & | d, |
| scaled_t | |||
| ) |
Renders a delta in its scaled form (its own unit), see scaled, e.g.
to_string(delta_millicelsius(1500), scaled) == "1500Δm°C".
Definition at line 683 of file thermo.hpp.
References quanta::thermo::delta< Rep, Precision >::count().
| std::string quanta::thermo::to_string | ( | const temperature< Scale, delta< Rep, Precision > > & | t | ) |
Renders a temperature as its exact count with a precision-qualified unit, e.g.
to_string(millicelsius(22500)) == "22500m°C".
Floating-point counts are rendered in the shortest fixed notation that round-trips, as by {} in std::format.
Definition at line 1189 of file thermo.hpp.
References quanta::thermo::temperature< Scale, Delta >::count().
| std::string quanta::thermo::to_string | ( | const temperature< Scale, delta< Rep, Precision > > & | t, |
| base_unit_t | |||
| ) |
Renders a temperature in the unprefixed degree of its own scale (°C, K or °F), see base_unit, e.g.
to_string(millicelsius(22534), base_unit) == "22.534°C".
Definition at line 1199 of file thermo.hpp.
References quanta::thermo::temperature< Scale, Delta >::count().
| std::string quanta::thermo::to_string | ( | const temperature< Scale, delta< Rep, Precision > > & | t, |
| scaled_t | |||
| ) |
Renders a temperature in its scaled form (its own unit), see scaled, e.g.
to_string(millicelsius(22534), scaled) == "22534m°C".
Definition at line 1209 of file thermo.hpp.
References quanta::thermo::temperature< Scale, Delta >::count().
|
constexpr |
Rounds a delta toward zero to the nearest representable value in the target precision.
Returns the value of type ToDelta that is closest to zero. When converting to the same or finer precision, returns the exact conversion.
| ToDelta | Target delta type. |
| Rep | Source representation type. |
| Precision | Source precision. |
| d | The delta to round. |
Definition at line 320 of file thermo.hpp.