|
electro 0.3.0
Type-safe electrical units library modeled after std::chrono
|
Electrical unit types and utilities. More...
Classes | |
| struct | _is_ratio |
| struct | _is_ratio< std::ratio< Num, Denom > > |
| struct | ampere_unit |
| Electric current, measured in amperes. More... | |
| struct | coulomb_unit |
| Electric charge, measured in coulombs. More... | |
| struct | decibel_unit |
| A dimensionless logarithmic ratio, measured in decibels. More... | |
| struct | farad_unit |
| Capacitance, measured in farads. More... | |
| struct | henry_unit |
| Inductance, measured in henries. More... | |
| struct | is_level |
| Trait to detect level specializations. More... | |
| struct | is_level< level< Reference, Rep, Precision > > |
| struct | is_quantity |
| Trait to detect quantity specializations. More... | |
| struct | is_quantity< quantity< Unit, Rep, Precision > > |
| struct | joule_unit |
| Energy, measured in joules. More... | |
| class | level |
| An absolute point on a logarithmic scale, relative to a reference. More... | |
| struct | microvolt_reference |
| Reference of 1 µV, for dBµV. More... | |
| struct | millivolt_reference |
| Reference of 1 mV, for dBmV. More... | |
| struct | milliwatt_reference |
| Reference of 1 mW, for dBm. More... | |
| struct | ohm_unit |
| Electrical resistance, measured in ohms. More... | |
| class | quantity |
| A quantity of an electrical unit with a representation and precision. More... | |
| struct | quantity_suffix |
| Trait giving the display suffix for a (unit, precision) pair. More... | |
| struct | quantity_suffix< decibel_unit, Precision > |
| Decibels opt out of the generic SI-prefix suffix machinery. 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 | treat_as_inexact |
| Trait to indicate floating-point-like behavior. More... | |
| struct | volt_reference |
| Reference of 1 V, for dBV. More... | |
| struct | volt_unit |
| Voltage, measured in volts. More... | |
| struct | watt_reference |
| Reference of 1 W, for dBW. More... | |
| struct | watt_unit |
| Power, measured in watts. More... | |
Concepts | |
| concept | decibel_reference |
| Concept for decibel reference tags. | |
Typedefs | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | gain = quantity< decibel_unit, Rep, Precision > |
| A logarithmic ratio with configurable representation and precision. | |
| using | decibels = gain< int64_t > |
| Gain with 1 dB precision. | |
| using | centidecibels = gain< int64_t, std::centi > |
| Gain with 0.01 dB precision. | |
| using | millidecibels = gain< int64_t, std::milli > |
| Gain with 0.001 dB precision. | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | dbm_level = level< milliwatt_reference, Rep, Precision > |
| A level referenced to 1 mW. | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | dbw_level = level< watt_reference, Rep, Precision > |
| A level referenced to 1 W. | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | dbv_level = level< volt_reference, Rep, Precision > |
| A level referenced to 1 V. | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | dbmv_level = level< millivolt_reference, Rep, Precision > |
| A level referenced to 1 mV. | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | dbuv_level = level< microvolt_reference, Rep, Precision > |
| A level referenced to 1 µV. | |
| using | dbm = dbm_level< int64_t > |
| Power level in dBm, with 1 dB precision. | |
| using | centi_dbm = dbm_level< int64_t, std::centi > |
| Power level in dBm, with 0.01 dB precision. | |
| using | dbw = dbw_level< int64_t > |
| Power level in dBW, with 1 dB precision. | |
| using | dbv = dbv_level< int64_t > |
| Voltage level in dBV, with 1 dB precision. | |
| using | dbmv = dbmv_level< int64_t > |
| Voltage level in dBmV, with 1 dB precision. | |
| using | dbuv = dbuv_level< int64_t > |
| Voltage level in dBµV, with 1 dB precision. | |
| using | centi_dbuv = dbuv_level< int64_t, std::centi > |
| Voltage level in dBµV, with 0.01 dB precision. | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | voltage = quantity< volt_unit, Rep, Precision > |
| Voltage quantity with configurable representation and precision. | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | current = quantity< ampere_unit, Rep, Precision > |
| Current quantity with configurable representation and precision. | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | resistance = quantity< ohm_unit, Rep, Precision > |
| Resistance quantity with configurable representation and precision. | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | power = quantity< watt_unit, Rep, Precision > |
| Power quantity with configurable representation and precision. | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | charge = quantity< coulomb_unit, Rep, Precision > |
| Charge quantity with configurable representation and precision. | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | energy = quantity< joule_unit, Rep, Precision > |
| Energy quantity with configurable representation and precision. | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | capacitance = quantity< farad_unit, Rep, Precision > |
| Capacitance quantity with configurable representation and precision. | |
| template<typename Rep , typename Precision = std::ratio<1>> | |
| using | inductance = quantity< henry_unit, Rep, Precision > |
| Inductance quantity with configurable representation and precision. | |
| using | microvolts = voltage< int64_t, std::micro > |
| Voltage with 1 µV precision. | |
| using | millivolts = voltage< int64_t, std::milli > |
| Voltage with 1 mV precision. | |
| using | volts = voltage< int64_t > |
| Voltage with 1 V precision. | |
| using | kilovolts = voltage< int64_t, std::kilo > |
| Voltage with 1 kV precision. | |
| using | microamperes = current< int64_t, std::micro > |
| Current with 1 µA precision. | |
| using | milliamperes = current< int64_t, std::milli > |
| Current with 1 mA precision. | |
| using | amperes = current< int64_t > |
| Current with 1 A precision. | |
| using | microamps = microamperes |
| Shorthand for microamperes. | |
| using | milliamps = milliamperes |
| Shorthand for milliamperes. | |
| using | amps = amperes |
| Shorthand for amperes. | |
| using | milliohms = resistance< int64_t, std::milli > |
| Resistance with 1 mΩ precision. | |
| using | ohms = resistance< int64_t > |
| Resistance with 1 Ω precision. | |
| using | kiloohms = resistance< int64_t, std::kilo > |
| Resistance with 1 kΩ precision. | |
| using | megaohms = resistance< int64_t, std::mega > |
| Resistance with 1 MΩ precision. | |
| using | gigaohms = resistance< int64_t, std::giga > |
| Resistance with 1 GΩ precision. | |
| using | microwatts = power< int64_t, std::micro > |
| Power with 1 µW precision. | |
| using | milliwatts = power< int64_t, std::milli > |
| Power with 1 mW precision. | |
| using | watts = power< int64_t > |
| Power with 1 W precision. | |
| using | kilowatts = power< int64_t, std::kilo > |
| Power with 1 kW precision. | |
| using | megawatts = power< int64_t, std::mega > |
| Power with 1 MW precision. | |
| using | gigawatts = power< int64_t, std::giga > |
| Power with 1 GW precision. | |
| using | microcoulombs = charge< int64_t, std::micro > |
| Charge with 1 µC precision. | |
| using | millicoulombs = charge< int64_t, std::milli > |
| Charge with 1 mC precision. | |
| using | coulombs = charge< int64_t > |
| Charge with 1 C precision. | |
| using | milliampere_hours = charge< int64_t, std::ratio< 18, 5 > > |
| Charge with 1 mAh (3.6 C) precision. | |
| using | ampere_hours = charge< int64_t, std::ratio< 3600 > > |
| Charge with 1 Ah (3600 C) precision. | |
| using | millijoules = energy< int64_t, std::milli > |
| Energy with 1 mJ precision. | |
| using | joules = energy< int64_t > |
| Energy with 1 J precision. | |
| using | kilojoules = energy< int64_t, std::kilo > |
| Energy with 1 kJ precision. | |
| using | megajoules = energy< int64_t, std::mega > |
| Energy with 1 MJ precision. | |
| using | watt_hours = energy< int64_t, std::ratio< 3600 > > |
| Energy with 1 Wh (3600 J) precision. | |
| using | kilowatt_hours = energy< int64_t, std::ratio< 3600000 > > |
| Energy with 1 kWh (3,600,000 J) precision. | |
| using | picofarads = capacitance< int64_t, std::pico > |
| Capacitance with 1 pF precision. | |
| using | nanofarads = capacitance< int64_t, std::nano > |
| Capacitance with 1 nF precision. | |
| using | microfarads = capacitance< int64_t, std::micro > |
| Capacitance with 1 µF precision. | |
| using | millifarads = capacitance< int64_t, std::milli > |
| Capacitance with 1 mF precision. | |
| using | farads = capacitance< int64_t > |
| Capacitance with 1 F precision. | |
| using | nanohenries = inductance< int64_t, std::nano > |
| Inductance with 1 nH precision. | |
| using | microhenries = inductance< int64_t, std::micro > |
| Inductance with 1 µH precision. | |
| using | millihenries = inductance< int64_t, std::milli > |
| Inductance with 1 mH precision. | |
| using | henries = inductance< int64_t > |
| Inductance with 1 H precision. | |
Variables | |
| template<typename Reference > | |
| constexpr bool | is_power_reference_v = false |
| True if the reference measures a power quantity (10*log10). | |
| template<decibel_reference Reference> | |
| constexpr bool | is_power_reference_v< Reference > = Reference::factor == 10 |
| template<typename Reference > | |
| constexpr bool | is_field_reference_v = false |
| True if the reference measures a field quantity (20*log10). | |
| template<decibel_reference Reference> | |
| constexpr bool | is_field_reference_v< Reference > = Reference::factor == 20 |
| template<typename T > | |
| constexpr bool | is_level_v = is_level<T>::value |
| template<typename T > | |
| constexpr bool | is_quantity_v = is_quantity<T>::value |
| template<typename T > | |
| constexpr bool | treat_as_inexact_v = treat_as_inexact<T>::value |
Electrical unit types and utilities.
This library provides type-safe handling of the principal electrical quantities - voltage, current, resistance, power, charge, energy, capacitance and inductance - with support for multiple precisions (e.g. millivolts, microamperes, kiloohms), following the design of std::chrono::duration.
The library provides standard integer-based types for each quantity:
Quantities of different kinds combine according to the laws of circuit theory, producing results in the correct unit with the correct precision:
Conversions between precisions are implicit when lossless and require an explicit cast when lossy, exactly as with std::chrono::duration.
| using electro::centidecibels = typedef gain<int64_t, std::centi> |
Gain with 0.01 dB precision.
Definition at line 85 of file decibel.hpp.
Gain with 1 dB precision.
Definition at line 83 of file decibel.hpp.
A logarithmic ratio with configurable representation and precision.
Definition at line 80 of file decibel.hpp.
| using electro::millidecibels = typedef gain<int64_t, std::milli> |
Gain with 0.001 dB precision.
Definition at line 87 of file decibel.hpp.
|
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 610 of file electro.hpp.
References treat_as_inexact_v, and electro::quantity< Unit, Rep, Precision >::zero().
|
inline |
Combines the powers of two uncorrelated signals.
This is what "adding" two levels physically means, and it is deliberately not operator+: 10 dBm combined with 10 dBm is 13.01 dBm, not 20 dBm.
Only available for power references. For a field reference such as dBµV, summing the linear amplitudes would model coherent, in-phase addition instead, which is a different operation with a different answer.
Definition at line 535 of file decibel.hpp.
References to_linear(), and treat_as_inexact_v.
The gain corresponding to a linear amplitude ratio (20*log10(ratio)).
Definition at line 512 of file decibel.hpp.
References treat_as_inexact_v.
The linear amplitude ratio a gain represents (10^(dB/20)).
Definition at line 501 of file decibel.hpp.
References treat_as_inexact_v.
Converts a level to the target type, rounding toward positive infinity.
Definition at line 329 of file decibel.hpp.
References treat_as_inexact_v.
Converts a quantity to the target type, rounding toward positive infinity.
| 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 545 of file electro.hpp.
References treat_as_inexact_v.
Converts a level to the target type, rounding toward negative infinity.
Definition at line 322 of file decibel.hpp.
References treat_as_inexact_v.
Converts a quantity to the target type, rounding toward negative infinity.
| 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 514 of file electro.hpp.
References treat_as_inexact_v.
Converts a level to a different precision or representation.
| ToLevel | The target level type (must have the same reference). |
Definition at line 313 of file decibel.hpp.
References treat_as_inexact_v.
|
constexpr |
Returns the remainder of dividing a quantity by a scalar.
Definition at line 672 of file electro.hpp.
References treat_as_inexact_v.
|
constexpr |
Returns the remainder of dividing two quantities of the same unit.
Definition at line 681 of file electro.hpp.
References treat_as_inexact_v.
|
constexpr |
Multiplies a quantity by a scalar.
Definition at line 637 of file electro.hpp.
References treat_as_inexact_v.
|
constexpr |
Multiplies a scalar by a quantity.
Definition at line 646 of file electro.hpp.
References treat_as_inexact_v.
|
constexpr |
Applies a gain to a level.
Definition at line 353 of file decibel.hpp.
References treat_as_inexact_v.
|
constexpr |
Applies a gain to a level.
Definition at line 362 of file decibel.hpp.
References treat_as_inexact_v.
|
constexpr |
Returns the sum of two quantities of the same unit.
Definition at line 620 of file electro.hpp.
References treat_as_inexact_v.
|
constexpr |
The difference between two levels is a gain.
Definition at line 383 of file decibel.hpp.
References treat_as_inexact_v.
|
constexpr |
Applies a loss to a level.
Definition at line 369 of file decibel.hpp.
References treat_as_inexact_v.
|
constexpr |
Returns the difference of two quantities of the same unit.
Definition at line 628 of file electro.hpp.
References treat_as_inexact_v.
|
constexpr |
Divides a quantity by a scalar.
Definition at line 654 of file electro.hpp.
References treat_as_inexact_v.
|
constexpr |
Divides two quantities of the same unit, returning a scalar.
Definition at line 662 of file electro.hpp.
References electro::quantity< Unit, Rep, Precision >::count(), and treat_as_inexact_v.
|
constexpr |
Definition at line 394 of file decibel.hpp.
References treat_as_inexact_v.
|
constexpr |
Definition at line 695 of file electro.hpp.
References electro::quantity< Unit, Rep, Precision >::count(), and treat_as_inexact_v.
|
constexpr |
Definition at line 388 of file decibel.hpp.
References treat_as_inexact_v.
|
constexpr |
Definition at line 688 of file electro.hpp.
References electro::quantity< Unit, Rep, Precision >::count(), and treat_as_inexact_v.
The gain corresponding to a linear power ratio (10*log10(ratio)).
Definition at line 506 of file decibel.hpp.
References treat_as_inexact_v.
The linear power ratio a gain represents (10^(dB/10)).
Definition at line 487 of file decibel.hpp.
References treat_as_inexact_v.
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.
| 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 438 of file electro.hpp.
References treat_as_inexact_v.
Converts a level to the target type, rounding to nearest (ties to even).
Definition at line 336 of file decibel.hpp.
References treat_as_inexact_v.
Converts a quantity to the target type, rounding to nearest (ties to even).
| 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 576 of file electro.hpp.
References treat_as_inexact_v.
Converts a linear quantity to a level.
The quantity's unit must match the target level's reference unit. Values are rounded to nearest at the target precision, rather than truncated.
A zero quantity has no finite logarithm and maps to ToLevel::min(). A negative quantity has no logarithm at all; in a debug build this asserts, and otherwise returns ToLevel::min().
Definition at line 462 of file decibel.hpp.
References electro::quantity< Unit, Rep, Precision >::count(), and treat_as_inexact_v.
Converts a level to the equivalent linear quantity.
The result is expressed at the reference's own precision, so to_linear() of a dBm level yields milliwatts and to_linear() of a dBµV level yields microvolts. Use quantity_cast to retarget:
Definition at line 438 of file decibel.hpp.
References treat_as_inexact_v.
Referenced by add_powers().
|
inline |
Converts a level to a string, e.g.
"-73dBm" or "13.01dBm".
Definition at line 666 of file decibel.hpp.
References treat_as_inexact_v.
|
inline |
Converts a gain to a string, e.g.
"10dB" or "10.50dB".
Unlike the generic quantity to_string, a gain's precision is rendered as decimal places rather than as an SI prefix: nobody writes "1050cdB".
Definition at line 657 of file decibel.hpp.
References electro::decibel_unit::symbol, and treat_as_inexact_v.
|
inline |
Converts a quantity to a string with its unit suffix.
Available for quantities whose precision has a conventional suffix (standard SI prefixes plus mAh, Ah, Wh and kWh).
Definition at line 1259 of file electro.hpp.
References treat_as_inexact_v, and electro::quantity_suffix< Unit, Precision >::value().
True if the reference measures a field quantity (20*log10).
Definition at line 169 of file decibel.hpp.
|
inlineconstexpr |
Definition at line 172 of file decibel.hpp.
Definition at line 192 of file decibel.hpp.
True if the reference measures a power quantity (10*log10).
Definition at line 162 of file decibel.hpp.
|
inlineconstexpr |
Definition at line 165 of file decibel.hpp.
Definition at line 105 of file electro.hpp.
|
inlineconstexpr |
Definition at line 162 of file electro.hpp.
Referenced by abs(), add_powers(), amplitude_gain(), amplitude_ratio(), ceil(), ceil(), floor(), floor(), level_cast(), operator%(), operator%(), electro::quantity< Unit, Rep, Precision >::operator%=(), electro::quantity< Unit, Rep, Precision >::operator%=(), operator*(), operator*(), operator*(), operator*(), operator*(), operator*(), operator*(), operator*(), operator*(), operator*(), operator*(), operator*(), operator*(), operator*(), electro::quantity< Unit, Rep, Precision >::operator*=(), operator+(), operator+(), operator+(), electro::level< Reference, Rep, Precision >::operator+=(), electro::quantity< Unit, Rep, Precision >::operator+=(), operator-(), operator-(), operator-(), electro::level< Reference, Rep, Precision >::operator-=(), electro::quantity< Unit, Rep, Precision >::operator-=(), operator/(), operator/(), operator/(), operator/(), operator/(), operator/(), operator/(), operator/(), operator/(), operator/(), operator/(), operator/(), operator/(), operator/(), operator/(), electro::quantity< Unit, Rep, Precision >::operator/=(), operator<=>(), operator<=>(), operator==(), operator==(), power_gain(), power_ratio(), quantity_cast(), round(), round(), to_level(), to_linear(), to_string(), to_string(), to_string(), and electro::quantity_values< Rep >::zero().