|
quanta 0.1.0
Type-safe units for temperature, frequency, electrical and spatial quantities, modeled after std::chrono
|
Frequency types and utilities. More...
Classes | |
| class | frequency |
| A frequency value with a representation and precision. More... | |
| struct | is_frequency |
| Trait to detect frequency specializations. More... | |
| struct | is_frequency< frequency< Rep, Precision > > |
Typedefs | |
| using | millihertz = frequency< int64_t, std::milli > |
| Frequency with 0.001 Hz (millihertz) precision. | |
| using | hertz = frequency< int64_t > |
| Frequency with 1 Hz precision. | |
| using | kilohertz = frequency< int64_t, std::kilo > |
| Frequency with 1000 Hz (kilohertz) precision. | |
| using | megahertz = frequency< int64_t, std::mega > |
| Frequency with 1,000,000 Hz (megahertz) precision. | |
| using | gigahertz = frequency< int64_t, std::giga > |
| Frequency with 1,000,000,000 Hz (gigahertz) precision. | |
| using | terahertz = frequency< int64_t, std::tera > |
| Frequency with 1,000,000,000,000 Hz (terahertz) precision. | |
Functions | |
| template<typename ToFreq , typename Rep , typename Precision > requires detail::_quantity_cast_requirements<frequency<Rep, Precision>, ToFreq> | |
| constexpr ToFreq | frequency_cast (const frequency< Rep, Precision > &f) |
| Converts a frequency to a different precision or representation. | |
| template<typename ToFreq , typename Rep , typename Precision > | |
| constexpr ToFreq | floor (const frequency< Rep, Precision > &f) |
| Converts a frequency to the target type, rounding toward negative infinity. | |
| template<typename ToFreq , typename Rep , typename Precision > | |
| constexpr ToFreq | ceil (const frequency< Rep, Precision > &f) |
| Converts a frequency to the target type, rounding toward positive infinity. | |
| template<typename ToFreq , typename Rep , typename Precision > | |
| constexpr ToFreq | trunc (const frequency< Rep, Precision > &f) |
| Converts a frequency to the target type, rounding toward zero. | |
| template<typename ToFreq , typename Rep , typename Precision > | |
| constexpr ToFreq | round (const frequency< Rep, Precision > &f) |
| Converts a frequency to the target type, rounding to nearest (ties to even). | |
| template<typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > | |
| constexpr auto | beat (const frequency< Rep1, Precision1 > &f1, const frequency< Rep2, Precision2 > &f2) -> std::common_type_t< frequency< Rep1, Precision1 >, frequency< Rep2, Precision2 > > |
| Calculates the beat frequency between two frequencies. | |
| template<typename Rep , typename Precision > | |
| constexpr frequency< Rep, Precision > | abs (const frequency< Rep, Precision > &f) |
| Returns the absolute value of a frequency. | |
| template<typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > | |
| constexpr auto | operator+ (const frequency< Rep1, Precision1 > &lhs, const frequency< Rep2, Precision2 > &rhs) -> std::common_type_t< frequency< Rep1, Precision1 >, frequency< Rep2, Precision2 > > |
| Returns the sum of two frequencies. | |
| template<typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > | |
| constexpr auto | operator- (const frequency< Rep1, Precision1 > &lhs, const frequency< Rep2, Precision2 > &rhs) -> std::common_type_t< frequency< Rep1, Precision1 >, frequency< Rep2, Precision2 > > |
| Returns the difference of two frequencies. | |
| template<typename Rep1 , typename Precision , typename Rep2 > requires not_frequency<Rep2> && std::convertible_to<const Rep2&, std::common_type_t<Rep1, Rep2>> | |
| constexpr auto | operator* (const frequency< Rep1, Precision > &f, const Rep2 &r) -> frequency< std::common_type_t< Rep1, Rep2 >, Precision > |
| Multiplies a frequency by a scalar. | |
| template<typename Rep1 , typename Rep2 , typename Precision > requires not_frequency<Rep1> && std::convertible_to<const Rep1&, std::common_type_t<Rep1, Rep2>> | |
| constexpr auto | operator* (const Rep1 &r, const frequency< Rep2, Precision > &f) -> frequency< std::common_type_t< Rep1, Rep2 >, Precision > |
| Multiplies a scalar by a frequency. | |
| template<typename Rep1 , typename Precision , typename Rep2 > requires not_frequency<Rep2> && std::convertible_to<const Rep2&, std::common_type_t<Rep1, Rep2>> | |
| constexpr auto | operator/ (const frequency< Rep1, Precision > &f, const Rep2 &s) -> frequency< std::common_type_t< Rep1, Rep2 >, Precision > |
| Divides a frequency by a scalar. | |
| template<typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > | |
| constexpr auto | operator/ (const frequency< Rep1, Precision1 > &lhs, const frequency< Rep2, Precision2 > &rhs) -> std::common_type_t< Rep1, Rep2 > |
| Divides two frequencies, returning a scalar. | |
| template<typename Rep1 , typename Precision , typename Rep2 > requires not_frequency<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 frequency< Rep1, Precision > &f, const Rep2 &s) -> frequency< std::common_type_t< Rep1, Rep2 >, Precision > |
| Returns the remainder of dividing a frequency 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 frequency< Rep1, Precision1 > &lhs, const frequency< Rep2, Precision2 > &rhs) -> std::common_type_t< frequency< Rep1, Precision1 >, frequency< Rep2, Precision2 > > |
| Returns the remainder of dividing two frequencies. | |
| template<typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 > | |
| constexpr bool | operator== (const frequency< Rep1, Precision1 > &lhs, const frequency< 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 frequency< Rep1, Precision1 > &lhs, const frequency< Rep2, Precision2 > &rhs) |
| template<typename Rep , typename Precision > | |
| std::string | to_string (const frequency< Rep, Precision > &f) |
| Converts a frequency to a string with its precision-qualified unit. | |
| template<typename Rep , typename Precision > | |
| std::string | to_string (const frequency< Rep, Precision > &f, base_unit_t) |
| Converts a frequency to a string in hertz; see base_unit. | |
| template<typename Rep , typename Precision > | |
| std::string | to_string (const frequency< Rep, Precision > &f, scaled_t) |
| Converts a frequency to a string with an SI prefix scaled to its magnitude; see scaled. | |
Variables | |
| template<typename T > | |
| constexpr bool | is_frequency_v = is_frequency<T>::value |
Frequency types and utilities.
This library provides type-safe frequency handling with support for multiple precisions (mHz, Hz, kHz, MHz, GHz, THz), following the design of std::chrono::duration.
The library provides standard integer-based frequency types:
For fractional precision (e.g., musical tuning, scientific measurements), use floating-point representations:
Conversions between integer and floating-point follow the same rules as precision conversions (implicit when lossless, explicit when lossy).
to_string() and std::format show a frequency 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 SI-prefixed unit. A single leading marker letter in the spec, or a tag argument to to_string(), opts into a different unit:
A precision with no SI prefix (e.g. std::deci) has no unit of its own: {} and to_string(f) do not compile for it, and a format spec without a marker renders the value in hertz.
|
constexpr |
Returns the absolute value of a frequency.
| Rep | Representation type. |
| Precision | Precision type. |
| f | The frequency. |
Definition at line 871 of file frequency.hpp.
Referenced by beat().
|
constexpr |
Calculates the beat frequency between two frequencies.
In acoustics and signal processing, the beat frequency is the absolute difference between two frequencies. When two sound waves of slightly different frequencies interfere, they produce a beating pattern at this frequency.
This is a convenience function equivalent to abs(f1 - f2).
| Rep1 | First frequency representation type. |
| Precision1 | First frequency precision. |
| Rep2 | Second frequency representation type. |
| Precision2 | Second frequency precision. |
| f1 | The first frequency. |
| f2 | The second frequency. |
Definition at line 849 of file frequency.hpp.
References abs().
|
constexpr |
Converts a frequency to the target type, rounding toward positive infinity.
This function performs a frequency conversion with ceiling rounding semantics. When converting to a coarser precision, values are rounded up.
As with the cast, the result is undefined if it does not fit the target rep.
| ToFreq | The target frequency type. |
| Rep | Source representation type. |
| Precision | Source precision. |
| f | The frequency to convert. |
Definition at line 758 of file frequency.hpp.
|
constexpr |
Converts a frequency to the target type, rounding toward negative infinity.
This function performs a frequency conversion with floor rounding semantics. When converting to a coarser precision, values are rounded down.
As with the cast, the result is undefined if it does not fit the target rep.
| ToFreq | The target frequency type. |
| Rep | Source representation type. |
| Precision | Source precision. |
| f | The frequency to convert. |
Definition at line 732 of file frequency.hpp.
|
constexpr |
Converts a frequency 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.
| ToFreq | The target frequency type. |
| Rep | Source representation type. |
| Precision | Source precision. |
| f | The frequency to convert. |
Definition at line 706 of file frequency.hpp.
|
constexpr |
Returns the remainder of dividing a frequency by a scalar.
Definition at line 929 of file frequency.hpp.
|
constexpr |
Returns the remainder of dividing two frequencies.
Definition at line 938 of file frequency.hpp.
|
constexpr |
Multiplies a frequency by a scalar.
Definition at line 894 of file frequency.hpp.
|
constexpr |
Multiplies a scalar by a frequency.
Definition at line 903 of file frequency.hpp.
|
constexpr |
Returns the sum of two frequencies.
Definition at line 877 of file frequency.hpp.
|
constexpr |
Returns the difference of two frequencies.
Definition at line 885 of file frequency.hpp.
|
constexpr |
Divides a frequency by a scalar.
Definition at line 911 of file frequency.hpp.
|
constexpr |
Divides two frequencies, returning a scalar.
Definition at line 919 of file frequency.hpp.
References quanta::frequency::frequency< Rep, Precision >::count().
|
constexpr |
Definition at line 952 of file frequency.hpp.
References quanta::frequency::frequency< Rep, Precision >::count().
|
constexpr |
Definition at line 945 of file frequency.hpp.
References quanta::frequency::frequency< Rep, Precision >::count().
|
constexpr |
Converts a frequency to the target type, rounding to nearest (ties to even).
This function performs a frequency conversion with round-to-nearest rounding semantics. When converting to a coarser precision, values are rounded to the nearest representable value, with ties rounded to even.
As with the cast, the result is undefined if it does not fit the target rep.
| ToFreq | The target frequency type. |
| Rep | Source representation type. |
| Precision | Source precision. |
| f | The frequency to convert. |
Definition at line 809 of file frequency.hpp.
| std::string quanta::frequency::to_string | ( | const frequency< Rep, Precision > & | f | ) |
Converts a frequency to a string with its precision-qualified unit.
Integer counts are shown exactly; floating-point counts in the shortest fixed notation that round-trips (never scientific), matching std::format("{}"). Available for precisions with an SI prefix (femto to tera); for other precisions use the base_unit or scaled form.
Definition at line 1189 of file frequency.hpp.
References quanta::frequency::frequency< Rep, Precision >::count().
| std::string quanta::frequency::to_string | ( | const frequency< Rep, Precision > & | f, |
| base_unit_t | |||
| ) |
Converts a frequency to a string in hertz; see base_unit.
Available for every precision.
Definition at line 1205 of file frequency.hpp.
| std::string quanta::frequency::to_string | ( | const frequency< Rep, Precision > & | f, |
| scaled_t | |||
| ) |
Converts a frequency to a string with an SI prefix scaled to its magnitude; see scaled.
Available for every precision.
Definition at line 1221 of file frequency.hpp.
|
constexpr |
Converts a frequency to the target type, rounding toward zero.
The same as frequency_cast<ToFreq>(f).
| ToFreq | The target frequency type. |
| Rep | Source representation type. |
| Precision | Source precision. |
| f | The frequency to convert. |
Definition at line 780 of file frequency.hpp.
|
inlineconstexpr |
Definition at line 125 of file frequency.hpp.