quanta 0.1.0
Type-safe units for temperature, frequency, electrical and spatial quantities, modeled after std::chrono
Loading...
Searching...
No Matches
quanta::spatial Namespace Reference

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().
 

Detailed Description

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.

Basic Usage

The library provides standard integer-based types for each precision:

using namespace quanta::spatial;
using namespace quanta::spatial_literals;
millimeters board_width{100};
kilometers commute{12};
feet ceiling{9};
auto trace = 8_mil;
auto run = 5_km;
length< int64_t, std::milli > millimeters
Length with 1 mm precision.
Definition spatial.hpp:727
length< int64_t, std::kilo > kilometers
Length with 1 km precision.
Definition spatial.hpp:733
length< int64_t, std::ratio< 381, 1250 > > feet
Length with 1 ft (12 in = 0.3048 m) precision.
Definition spatial.hpp:740
Spatial unit types and utilities.
Definition spatial.hpp:100

Metric and Imperial

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:

static_assert(inches(1) == micrometers(25400));
static_assert(miles(1) == feet(5280));
auto total = 1_m + 1_in; // 5127 in units of 0.2 mm
auto mm = round<millimeters>(1_ft); // 305 mm
length< int64_t, std::ratio< 201168, 125 > > miles
Length with 1 mi (5280 ft = 1609.344 m) precision.
Definition spatial.hpp:744
length< int64_t, std::ratio< 127, 5000 > > inches
Length with 1 in (25.4 mm) precision.
Definition spatial.hpp:738
length< int64_t, std::micro > micrometers
Length with 1 µm precision.
Definition spatial.hpp:725

Area and Volume

Multiplying lengths yields areas and volumes, and dividing them yields lengths and areas again, with the result precision derived exactly from the operand precisions:

static_assert(1_ft * 1_ft == square_inches(144));
static_assert(acres(1) == square_feet(43560));
static_assert(us_gallons(1) == cubic_inches(231));
auto floor_area = 12_ft * 10_ft; // 120 ft²
auto tank = 2_m * 1_m * 50_cm; // 100 in units of 0.01 m³
auto depth = 1000_cm3 / 100_cm2; // 10 cm
area< int64_t, std::ratio< 145161, 1562500 > > square_feet
Area with 1 ft² (144 in² = 0.09290304 m²) precision.
Definition spatial.hpp:766
volume< int64_t, std::ratio< 2048383, 125000000000 > > cubic_inches
Volume with 1 in³ (16.387064 mL) precision.
Definition spatial.hpp:792
volume< int64_t, std::ratio< 473176473, 125000000000 > > us_gallons
Volume with 1 US gallon (231 in³ = 3.785411784 L) precision.
Definition spatial.hpp:798
area< int64_t, std::ratio< 316160658, 78125 > > acres
Area with 1 acre (43,560 ft² = 4046.8564224 m²) precision.
Definition spatial.hpp:770
area< int64_t, std::ratio< 16129, 25000000 > > square_inches
Area with 1 in² (645.16 mm²) precision.
Definition spatial.hpp:764

Formatting

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:

std::format("{}", milliliters(1500)); // "1500mL" own unit
std::format("{:p}", milliliters(1500)); // "1500cm³" power form
std::format("{:b}", milliliters(1500)); // "0.0015m³" base unit
std::format("{:s}", milliliters(1500)); // "1.5L" scaled
std::format("{:s.2f}", millimeters(1500)); // "1.50m"
to_string(milliliters(1500), scaled); // "1.5L"
volume< int64_t, std::micro > milliliters
Volume with 1 mL (1 cm³) precision.
Definition spatial.hpp:783
std::string to_string(const quantity< Unit, Rep, Precision > &q)
Converts a quantity to a string with its unit suffix.
Definition spatial.hpp:1294

Integer vs Floating-Point

  • Integer types (default): Exact arithmetic, support modulo operations, preferred for embedded and digital systems.
  • Floating-point types: Fractional precision, e.g. length<double> or length<double, std::milli>.

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.

Function Documentation

◆ abs()

template<typename Unit , typename Rep , typename Precision >
constexpr quantity< Unit, Rep, Precision > quanta::spatial::abs ( const quantity< Unit, Rep, Precision > &  q)
constexpr

Returns the absolute value of a quantity.

Template Parameters
UnitThe unit tag type.
RepRepresentation type.
PrecisionPrecision type.
Parameters
qThe quantity.
Returns
The absolute value of the quantity.

Definition at line 521 of file spatial.hpp.

◆ ceil()

template<typename ToQuantity , typename Unit , typename Rep , typename Precision >
constexpr ToQuantity quanta::spatial::ceil ( const quantity< Unit, Rep, Precision > &  q)
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.

Template Parameters
ToQuantityThe target quantity type.
UnitThe unit tag type.
RepSource representation type.
PrecisionSource precision.
Parameters
qThe quantity to convert.
Returns
The converted quantity, rounded toward positive infinity.
using namespace quanta::spatial;
auto m = ceil<meters>(millimeters(1500)); // 2 m (rounded up from 1.5)
auto ft = ceil<feet>(meters(1)); // 4 ft (rounded up from 3.28)
length< int64_t > meters
Length with 1 m precision.
Definition spatial.hpp:731

Definition at line 459 of file spatial.hpp.

◆ floor()

template<typename ToQuantity , typename Unit , typename Rep , typename Precision >
constexpr ToQuantity quanta::spatial::floor ( const quantity< Unit, Rep, Precision > &  q)
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.

Template Parameters
ToQuantityThe target quantity type.
UnitThe unit tag type.
RepSource representation type.
PrecisionSource precision.
Parameters
qThe quantity to convert.
Returns
The converted quantity, rounded toward negative infinity.
using namespace quanta::spatial;
auto m = floor<meters>(millimeters(1500)); // 1 m (rounded down from 1.5)
auto in = floor<inches>(centimeters(10)); // 3 in (rounded down from 3.94)
length< int64_t, std::centi > centimeters
Length with 1 cm precision.
Definition spatial.hpp:729

Definition at line 435 of file spatial.hpp.

◆ operator%() [1/2]

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 quanta::spatial::operator% ( const quantity< Unit, Rep1, Precision > &  q,
const Rep2 &  s 
) -> quantity<Unit, std::common_type_t<Rep1, Rep2>, Precision>
constexpr

Returns the remainder of dividing a quantity by a scalar.

Definition at line 583 of file spatial.hpp.

◆ operator%() [2/2]

template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
requires (!treat_as_inexact_v<Rep1> && !treat_as_inexact_v<Rep2>)
constexpr auto quanta::spatial::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>>
constexpr

Returns the remainder of dividing two quantities of the same unit.

Definition at line 592 of file spatial.hpp.

◆ operator*() [1/2]

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 quanta::spatial::operator* ( const quantity< Unit, Rep1, Precision > &  q,
const Rep2 &  r 
) -> quantity<Unit, std::common_type_t<Rep1, Rep2>, Precision>
constexpr

Multiplies a quantity by a scalar.

Definition at line 548 of file spatial.hpp.

◆ operator*() [2/2]

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 quanta::spatial::operator* ( const Rep1 &  r,
const quantity< Unit, Rep2, Precision > &  q 
) -> quantity<Unit, std::common_type_t<Rep1, Rep2>, Precision>
constexpr

Multiplies a scalar by a quantity.

Definition at line 557 of file spatial.hpp.

◆ operator+()

template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr auto quanta::spatial::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>>
constexpr

Returns the sum of two quantities of the same unit.

Definition at line 531 of file spatial.hpp.

◆ operator-()

template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr auto quanta::spatial::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>>
constexpr

Returns the difference of two quantities of the same unit.

Definition at line 539 of file spatial.hpp.

◆ operator/() [1/2]

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 quanta::spatial::operator/ ( const quantity< Unit, Rep1, Precision > &  q,
const Rep2 &  s 
) -> quantity<Unit, std::common_type_t<Rep1, Rep2>, Precision>
constexpr

Divides a quantity by a scalar.

Definition at line 565 of file spatial.hpp.

◆ operator/() [2/2]

template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr auto quanta::spatial::operator/ ( const quantity< Unit, Rep1, Precision1 > &  lhs,
const quantity< Unit, Rep2, Precision2 > &  rhs 
) -> std::common_type_t<Rep1, Rep2>
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().

◆ operator<=>()

template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
requires std::three_way_comparable<std::common_type_t<Rep1, Rep2>>
constexpr auto quanta::spatial::operator<=> ( const quantity< Unit, Rep1, Precision1 > &  lhs,
const quantity< Unit, Rep2, Precision2 > &  rhs 
)
constexpr

◆ operator==()

template<typename Unit , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr bool quanta::spatial::operator== ( const quantity< Unit, Rep1, Precision1 > &  lhs,
const quantity< Unit, Rep2, Precision2 > &  rhs 
)
constexpr

◆ quantity_cast()

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 quanta::spatial::quantity_cast ( const quantity< Unit, Rep, Precision > &  q)
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.

Template Parameters
ToQuantityThe target quantity type (must have the same unit).
UnitThe unit tag type.
RepSource representation type.
PrecisionSource precision.
Parameters
qThe quantity to convert.
Returns
The converted quantity.
using namespace quanta::spatial;
auto in = quantity_cast<inches>(millimeters(100)); // 3 in (3.937 truncated)

Definition at line 407 of file spatial.hpp.

◆ round()

template<typename ToQuantity , typename Unit , typename Rep , typename Precision >
constexpr ToQuantity quanta::spatial::round ( const quantity< Unit, Rep, Precision > &  q)
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.

Template Parameters
ToQuantityThe target quantity type.
UnitThe unit tag type.
RepSource representation type.
PrecisionSource precision.
Parameters
qThe quantity to convert.
Returns
The converted quantity, rounded to nearest.
using namespace quanta::spatial;
auto m = round<meters>(millimeters(1500)); // 2 m (rounded to even)
auto mm = round<millimeters>(feet(1)); // 305 mm (from 304.8)

Definition at line 506 of file spatial.hpp.

◆ to_string() [1/4]

template<typename Unit , typename Rep , typename Precision >
requires _has_suffix<Unit, typename Precision::type>
std::string quanta::spatial::to_string ( const quantity< Unit, Rep, Precision > &  q)
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("{}").

to_string(millimeters(250)); // "250mm"
to_string(micrometers(5)); // "5µm"
to_string(feet(6)); // "6ft"
to_string(square_feet(12)); // "12ft²"
to_string(milliliters(24)); // "24mL"
to_string(length<double>(2.5)); // "2.5m"
quantity< meter_unit, Rep, Precision > length
Length quantity with configurable representation and precision.
Definition spatial.hpp:720

Definition at line 1294 of file spatial.hpp.

References quanta::spatial::quantity< Unit, Rep, Precision >::count(), and quanta::spatial::quantity_suffix< Unit, Precision >::value().

◆ to_string() [2/4]

template<typename Unit , typename Rep , typename Precision >
requires _has_suffix<Unit, typename Precision::type>
std::string quanta::spatial::to_string ( const quantity< Unit, Rep, Precision > &  q,
base_unit_t   
)
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.

to_string(millimeters(1500), base_unit); // "1.5m"
to_string(hectares(3), base_unit); // "30000m²"
area< int64_t, std::ratio< 10000 > > hectares
Area with 1 ha (1 hm² = 10,000 m²) precision.
Definition spatial.hpp:759

Definition at line 1327 of file spatial.hpp.

◆ to_string() [3/4]

template<typename Unit , typename Rep , typename Precision >
requires _has_suffix<Unit, typename Precision::type>
std::string quanta::spatial::to_string ( const quantity< Unit, Rep, Precision > &  q,
power_form_t   
)
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.

to_string(milliliters(24), power_form); // "24cm³"
to_string(liters(2), power_form); // "2dm³"
to_string(millimeters(250), power_form); // "250mm"
volume< int64_t, std::milli > liters
Volume with 1 L (1 dm³) precision.
Definition spatial.hpp:787
constexpr power_form_t power_form
Selects the power-form suffix in to_string().
Definition spatial.hpp:1232

Definition at line 1311 of file spatial.hpp.

References quanta::spatial::quantity< Unit, Rep, Precision >::count().

◆ to_string() [4/4]

template<typename Unit , typename Rep , typename Precision >
requires _has_suffix<Unit, typename Precision::type>
std::string quanta::spatial::to_string ( const quantity< Unit, Rep, Precision > &  q,
scaled_t   
)
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.

to_string(millimeters(1500), scaled); // "1.5m"
to_string(liters(2000), scaled); // "2m³"

Definition at line 1344 of file spatial.hpp.

◆ trunc()

template<typename ToQuantity , typename Unit , typename Rep , typename Precision >
constexpr ToQuantity quanta::spatial::trunc ( const quantity< Unit, Rep, Precision > &  q)
constexpr

Converts a quantity to the target type, rounding toward zero.

The same as quantity_cast<ToQuantity>(q).

Template Parameters
ToQuantityThe target quantity type.
UnitThe unit tag type.
RepSource representation type.
PrecisionSource precision.
Parameters
qThe quantity to convert.
Returns
The converted quantity, rounded toward zero.
using namespace quanta::spatial;
auto m = trunc<meters>(millimeters(-1500)); // -1 m (rounded toward zero from -1.5)

Definition at line 482 of file spatial.hpp.

Variable Documentation

◆ is_quantity_v

template<typename T >
constexpr bool quanta::spatial::is_quantity_v = is_quantity<T>::value
inlineconstexpr

Definition at line 120 of file spatial.hpp.

◆ power_form

constexpr power_form_t quanta::spatial::power_form {}
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.

to_string(milliliters(24), power_form); // "24cm³"
to_string(hectares(3), power_form); // "3hm²"
to_string(square_feet(12), power_form); // "12ft²"

Definition at line 1232 of file spatial.hpp.