|
quanta 0.1.0
Type-safe units for temperature, frequency, electrical and spatial quantities, modeled after std::chrono
|
Spatial unit types and utilities. More...
Classes | |
| struct | cubic_meter_unit |
| Volume, measured in cubic meters. More... | |
| struct | is_quantity |
| Trait to detect quantity specializations. More... | |
| struct | is_quantity< quantity< Unit, Rep, Precision > > |
| struct | meter_unit |
| Length, measured in meters. More... | |
| struct | power_form_t |
| Tag type selecting the power-form suffix, see power_form. More... | |
| class | quantity |
| A quantity of a spatial unit with a representation and precision. More... | |
| struct | quantity_suffix |
| Trait giving the display suffix for a (unit, precision) pair. More... | |
| struct | quantity_values |
| Provides special values for quantity representations. More... | |
| struct | si_prefix |
| Trait giving the display symbol for an SI precision prefix. More... | |
| struct | square_meter_unit |
| Area, measured in square meters. More... | |
Typedefs | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | length = quantity< meter_unit, Rep, Precision > |
| Length quantity with configurable representation and precision. | |
| using | nanometers = length< int64_t, std::nano > |
| Length with 1 nm precision. | |
| using | micrometers = length< int64_t, std::micro > |
| Length with 1 µm precision. | |
| using | millimeters = length< int64_t, std::milli > |
| Length with 1 mm precision. | |
| using | centimeters = length< int64_t, std::centi > |
| Length with 1 cm precision. | |
| using | meters = length< int64_t > |
| Length with 1 m precision. | |
| using | kilometers = length< int64_t, std::kilo > |
| Length with 1 km precision. | |
| using | mils = length< int64_t, std::ratio< 127, 5000000 > > |
| Length with 1 mil (thou, 1/1000 in = 25.4 µm) precision. | |
| using | inches = length< int64_t, std::ratio< 127, 5000 > > |
| Length with 1 in (25.4 mm) precision. | |
| using | feet = length< int64_t, std::ratio< 381, 1250 > > |
| Length with 1 ft (12 in = 0.3048 m) precision. | |
| using | yards = length< int64_t, std::ratio< 1143, 1250 > > |
| Length with 1 yd (3 ft = 0.9144 m) precision. | |
| using | miles = length< int64_t, std::ratio< 201168, 125 > > |
| Length with 1 mi (5280 ft = 1609.344 m) precision. | |
| using | nautical_miles = length< int64_t, std::ratio< 1852 > > |
| Length with 1 nmi (1852 m) precision. | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | area = quantity< square_meter_unit, Rep, Precision > |
| Area quantity with configurable representation and precision (a ratio of one m²). | |
| using | square_millimeters = area< int64_t, std::micro > |
| Area with 1 mm² precision. | |
| using | square_centimeters = area< int64_t, std::ratio< 1, 10000 > > |
| Area with 1 cm² precision. | |
| using | square_meters = area< int64_t > |
| Area with 1 m² precision. | |
| using | hectares = area< int64_t, std::ratio< 10000 > > |
| Area with 1 ha (1 hm² = 10,000 m²) precision. | |
| using | square_kilometers = area< int64_t, std::mega > |
| Area with 1 km² precision. | |
| using | square_inches = area< int64_t, std::ratio< 16129, 25000000 > > |
| Area with 1 in² (645.16 mm²) precision. | |
| using | square_feet = area< int64_t, std::ratio< 145161, 1562500 > > |
| Area with 1 ft² (144 in² = 0.09290304 m²) precision. | |
| using | square_yards = area< int64_t, std::ratio< 1306449, 1562500 > > |
| Area with 1 yd² (9 ft² = 0.83612736 m²) precision. | |
| using | acres = area< int64_t, std::ratio< 316160658, 78125 > > |
| Area with 1 acre (43,560 ft² = 4046.8564224 m²) precision. | |
| using | square_miles = area< int64_t, std::ratio< 40468564224, 15625 > > |
| Area with 1 mi² (640 acres = 2,589,988.110336 m²) precision. | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | volume = quantity< cubic_meter_unit, Rep, Precision > |
| Volume quantity with configurable representation and precision (a ratio of one m³). | |
| using | microliters = volume< int64_t, std::nano > |
| Volume with 1 µL (1 mm³) precision. | |
| using | cubic_millimeters = microliters |
| Volume with 1 mm³ precision (the same type as microliters). | |
| using | milliliters = volume< int64_t, std::micro > |
| Volume with 1 mL (1 cm³) precision. | |
| using | cubic_centimeters = milliliters |
| Volume with 1 cm³ precision (the same type as milliliters). | |
| using | liters = volume< int64_t, std::milli > |
| Volume with 1 L (1 dm³) precision. | |
| using | cubic_meters = volume< int64_t > |
| Volume with 1 m³ precision. | |
| using | cubic_inches = volume< int64_t, std::ratio< 2048383, 125000000000 > > |
| Volume with 1 in³ (16.387064 mL) precision. | |
| using | cubic_feet = volume< int64_t, std::ratio< 55306341, 1953125000 > > |
| Volume with 1 ft³ (1728 in³ = 28.316846592 L) precision. | |
| using | cubic_yards = volume< int64_t, std::ratio< 1493271207, 1953125000 > > |
| Volume with 1 yd³ (27 ft³ = 0.764554857984 m³) precision. | |
| using | us_gallons = volume< int64_t, std::ratio< 473176473, 125000000000 > > |
| Volume with 1 US gallon (231 in³ = 3.785411784 L) precision. | |
Functions | |
| template<typename ToQuantity , typename Unit , typename Rep , typename Precision > requires (!std::is_same_v<typename ToQuantity::unit, Unit>) || detail::_quantity_cast_requirements<quantity<Unit, Rep, Precision>, ToQuantity> | |
| constexpr ToQuantity | quantity_cast (const quantity< Unit, Rep, Precision > &q) |
| Converts a quantity to a different precision or representation. | |
| template<typename ToQuantity , typename Unit , typename Rep , typename Precision > | |
| constexpr ToQuantity | floor (const quantity< Unit, Rep, Precision > &q) |
| Converts a quantity to the target type, rounding toward negative infinity. | |
| template<typename ToQuantity , typename Unit , typename Rep , typename Precision > | |
| constexpr ToQuantity | ceil (const quantity< Unit, Rep, Precision > &q) |
| Converts a quantity to the target type, rounding toward positive infinity. | |
| template<typename ToQuantity , typename Unit , typename Rep , typename Precision > | |
| constexpr ToQuantity | trunc (const quantity< Unit, Rep, Precision > &q) |
| Converts a quantity to the target type, rounding toward zero. | |
| template<typename ToQuantity , typename Unit , typename Rep , typename Precision > | |
| constexpr ToQuantity | round (const quantity< Unit, Rep, Precision > &q) |
| Converts a quantity to the target type, rounding to nearest (ties to even). | |
| template<typename Unit , typename Rep , typename Precision > | |
| constexpr quantity< Unit, Rep, Precision > | abs (const quantity< Unit, Rep, Precision > &q) |
| Returns the absolute value of a quantity. | |
| template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > | |
| constexpr auto | operator+ (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs) -> std::common_type_t< quantity< Unit, Rep1, Precision1 >, quantity< Unit, Rep2, Precision2 > > |
| Returns the sum of two quantities of the same unit. | |
| template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > | |
| constexpr auto | operator- (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs) -> std::common_type_t< quantity< Unit, Rep1, Precision1 >, quantity< Unit, Rep2, Precision2 > > |
| Returns the difference of two quantities of the same unit. | |
| template<typename Unit , typename Rep1 , typename Precision , typename Rep2 > requires not_quantity<Rep2> && std::convertible_to<const Rep2&, std::common_type_t<Rep1, Rep2>> | |
| constexpr auto | operator* (const quantity< Unit, Rep1, Precision > &q, const Rep2 &r) -> quantity< Unit, std::common_type_t< Rep1, Rep2 >, Precision > |
| Multiplies a quantity by a scalar. | |
| template<typename Unit , typename Rep1 , typename Rep2 , typename Precision > requires not_quantity<Rep1> && std::convertible_to<const Rep1&, std::common_type_t<Rep1, Rep2>> | |
| constexpr auto | operator* (const Rep1 &r, const quantity< Unit, Rep2, Precision > &q) -> quantity< Unit, std::common_type_t< Rep1, Rep2 >, Precision > |
| Multiplies a scalar by a quantity. | |
| template<typename Unit , typename Rep1 , typename Precision , typename Rep2 > requires not_quantity<Rep2> && std::convertible_to<const Rep2&, std::common_type_t<Rep1, Rep2>> | |
| constexpr auto | operator/ (const quantity< Unit, Rep1, Precision > &q, const Rep2 &s) -> quantity< Unit, std::common_type_t< Rep1, Rep2 >, Precision > |
| Divides a quantity by a scalar. | |
| template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > | |
| constexpr auto | operator/ (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs) -> std::common_type_t< Rep1, Rep2 > |
| Divides two quantities of the same unit, returning a scalar. | |
| template<typename Unit , typename Rep1 , typename Precision , typename Rep2 > requires not_quantity<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 quantity< Unit, Rep1, Precision > &q, const Rep2 &s) -> quantity< Unit, std::common_type_t< Rep1, Rep2 >, Precision > |
| Returns the remainder of dividing a quantity by a scalar. | |
| template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > requires (!treat_as_inexact_v<Rep1> && !treat_as_inexact_v<Rep2>) | |
| constexpr auto | operator% (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs) -> std::common_type_t< quantity< Unit, Rep1, Precision1 >, quantity< Unit, Rep2, Precision2 > > |
| Returns the remainder of dividing two quantities of the same unit. | |
| template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > | |
| constexpr bool | operator== (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs) |
| template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > requires std::three_way_comparable<std::common_type_t<Rep1, Rep2>> | |
| constexpr auto | operator<=> (const quantity< Unit, Rep1, Precision1 > &lhs, const quantity< Unit, Rep2, Precision2 > &rhs) |
| template<typename R1 , typename P1 , typename R2 , typename P2 > | |
| constexpr auto | operator* (const quantity< meter_unit, R1, P1 > &lhs, const quantity< meter_unit, R2, P2 > &rhs) |
| length x length = area. | |
| template<typename R1 , typename P1 , typename R2 , typename P2 > | |
| constexpr auto | operator* (const quantity< meter_unit, R1, P1 > &lhs, const quantity< square_meter_unit, R2, P2 > &rhs) |
| length x area = volume. | |
| template<typename R1 , typename P1 , typename R2 , typename P2 > | |
| constexpr auto | operator* (const quantity< square_meter_unit, R1, P1 > &lhs, const quantity< meter_unit, R2, P2 > &rhs) |
| area x length = volume. | |
| template<typename R1 , typename P1 , typename R2 , typename P2 > | |
| constexpr auto | operator/ (const quantity< square_meter_unit, R1, P1 > &lhs, const quantity< meter_unit, R2, P2 > &rhs) |
| area / length = length. | |
| template<typename R1 , typename P1 , typename R2 , typename P2 > | |
| constexpr auto | operator/ (const quantity< cubic_meter_unit, R1, P1 > &lhs, const quantity< meter_unit, R2, P2 > &rhs) |
| volume / length = area. | |
| template<typename R1 , typename P1 , typename R2 , typename P2 > | |
| constexpr auto | operator/ (const quantity< cubic_meter_unit, R1, P1 > &lhs, const quantity< square_meter_unit, R2, P2 > &rhs) |
| volume / area = length. | |
| template<typename Unit , typename Rep , typename Precision > requires _has_suffix<Unit, typename Precision::type> | |
| std::string | to_string (const quantity< Unit, Rep, Precision > &q) |
| Converts a quantity to a string with its unit suffix. | |
| template<typename Unit , typename Rep , typename Precision > requires _has_suffix<Unit, typename Precision::type> | |
| std::string | to_string (const quantity< Unit, Rep, Precision > &q, power_form_t) |
| Converts a quantity to a string with its power-form unit suffix. | |
| template<typename Unit , typename Rep , typename Precision > requires _has_suffix<Unit, typename Precision::type> | |
| std::string | to_string (const quantity< Unit, Rep, Precision > &q, base_unit_t) |
| Converts a quantity to a string in its base unit (m, m² or m³). | |
| template<typename Unit , typename Rep , typename Precision > requires _has_suffix<Unit, typename Precision::type> | |
| std::string | to_string (const quantity< Unit, Rep, Precision > &q, scaled_t) |
| Converts a quantity to a string in a unit scaled to its magnitude. | |
Variables | |
| template<typename T > | |
| constexpr bool | is_quantity_v = is_quantity<T>::value |
| constexpr power_form_t | power_form {} |
| Selects the power-form suffix in to_string(). | |
Spatial unit types and utilities.
This library provides type-safe handling of spatial quantities - length, area and volume - with support for multiple precisions, both metric (nanometers through kilometers, square millimeters through hectares, milliliters through cubic meters) and imperial/US customary (mils through nautical miles, square inches through acres, cubic inches through US gallons), following the design of std::chrono::duration.
The library provides standard integer-based types for each precision:
Imperial and nautical units are defined by their exact metric values (the international inch is exactly 25.4 mm, the nautical mile exactly 1852 m), so every precision is an exact std::ratio of one meter and quantities of any precision mix freely:
Multiplying lengths yields areas and volumes, and dividing them yields lengths and areas again, with the result precision derived exactly from the operand precisions:
to_string() and std::format show a quantity 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's suffix. A single leading marker letter in the spec, or a tag argument to to_string(), opts into a different unit:
Conversions between precisions are implicit when lossless and require an explicit cast when lossy, exactly as with std::chrono::duration. For example, inches convert implicitly to micrometers (1 in = 25400 µm), but millimeters to inches requires a cast.
|
constexpr |
Returns the absolute value of a quantity.
| Unit | The unit tag type. |
| Rep | Representation type. |
| Precision | Precision type. |
| q | The quantity. |
Definition at line 521 of file spatial.hpp.
|
constexpr |
Converts a quantity to the target type, rounding toward positive infinity.
As with the cast, the result is undefined if it does not fit the target rep.
| ToQuantity | The target quantity type. |
| Unit | The unit tag type. |
| Rep | Source representation type. |
| Precision | Source precision. |
| q | The quantity to convert. |
Definition at line 459 of file spatial.hpp.
|
constexpr |
Converts a quantity to the target type, rounding toward negative infinity.
As with the cast, the result is undefined if it does not fit the target rep.
| ToQuantity | The target quantity type. |
| Unit | The unit tag type. |
| Rep | Source representation type. |
| Precision | Source precision. |
| q | The quantity to convert. |
Definition at line 435 of file spatial.hpp.
|
constexpr |
Returns the remainder of dividing a quantity by a scalar.
Definition at line 583 of file spatial.hpp.
|
constexpr |
Returns the remainder of dividing two quantities of the same unit.
Definition at line 592 of file spatial.hpp.
|
constexpr |
Multiplies a quantity by a scalar.
Definition at line 548 of file spatial.hpp.
|
constexpr |
Multiplies a scalar by a quantity.
Definition at line 557 of file spatial.hpp.
|
constexpr |
Returns the sum of two quantities of the same unit.
Definition at line 531 of file spatial.hpp.
|
constexpr |
Returns the difference of two quantities of the same unit.
Definition at line 539 of file spatial.hpp.
|
constexpr |
Divides a quantity by a scalar.
Definition at line 565 of file spatial.hpp.
|
constexpr |
Divides two quantities of the same unit, returning a scalar.
Definition at line 573 of file spatial.hpp.
References quanta::spatial::quantity< Unit, Rep, Precision >::count().
|
constexpr |
Definition at line 606 of file spatial.hpp.
References quanta::spatial::quantity< Unit, Rep, Precision >::count().
|
constexpr |
Definition at line 599 of file spatial.hpp.
References quanta::spatial::quantity< Unit, Rep, Precision >::count().
|
constexpr |
Converts a quantity 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. Integer results truncate toward zero; use floor, ceil or round for other rounding modes.
| ToQuantity | The target quantity type (must have the same unit). |
| Unit | The unit tag type. |
| Rep | Source representation type. |
| Precision | Source precision. |
| q | The quantity to convert. |
Definition at line 407 of file spatial.hpp.
|
constexpr |
Converts a quantity to the target type, rounding to nearest (ties to even).
As with the cast, the result is undefined if it does not fit the target rep.
| ToQuantity | The target quantity type. |
| Unit | The unit tag type. |
| Rep | Source representation type. |
| Precision | Source precision. |
| q | The quantity to convert. |
Definition at line 506 of file spatial.hpp.
|
inline |
Converts a quantity to a string with its unit suffix.
Available for quantities whose precision has a conventional suffix (see quantity_suffix): standard SI prefixes plus mil, in, ft, yd, mi and nmi for lengths, and the listed area and volume precisions. Integer counts are shown exactly; floating-point counts in the shortest fixed notation that round-trips (never scientific), matching std::format("{}").
Definition at line 1294 of file spatial.hpp.
References quanta::spatial::quantity< Unit, Rep, Precision >::count(), and quanta::spatial::quantity_suffix< Unit, Precision >::value().
|
inline |
Converts a quantity to a string in its base unit (m, m² or m³).
Available for the same quantities as to_string(q); see base_unit.
Definition at line 1327 of file spatial.hpp.
|
inline |
Converts a quantity to a string with its power-form unit suffix.
Available for the same quantities as to_string(q); see power_form.
Definition at line 1311 of file spatial.hpp.
References quanta::spatial::quantity< Unit, Rep, Precision >::count().
|
inline |
Converts a quantity to a string in a unit scaled to its magnitude.
Available for the same quantities as to_string(q); see scaled.
Definition at line 1344 of file spatial.hpp.
|
constexpr |
Converts a quantity to the target type, rounding toward zero.
The same as quantity_cast<ToQuantity>(q).
| ToQuantity | The target quantity type. |
| Unit | The unit tag type. |
| Rep | Source representation type. |
| Precision | Source precision. |
| q | The quantity to convert. |
Definition at line 482 of file spatial.hpp.
|
inlineconstexpr |
Definition at line 120 of file spatial.hpp.
|
inlineconstexpr |
Selects the power-form suffix in to_string().
The power form writes the precision as a power of a length unit, so milliliters display as "cm³" rather than "mL", liters as "dm³" and hectares as "hm²". Suffixes that are already powers (and all length suffixes) are unchanged, as are acres ("ac") and US gallons ("gal"), which have no power form.
Definition at line 1232 of file spatial.hpp.