electro 0.3.0
Type-safe electrical units library modeled after std::chrono
Loading...
Searching...
No Matches
electro Namespace Reference

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.
 

Functions

template<typename ToLevel , typename Reference , typename Rep , typename Precision >
constexpr ToLevel level_cast (const level< Reference, Rep, Precision > &l)
 Converts a level to a different precision or representation.
 
template<typename ToLevel , typename Reference , typename Rep , typename Precision >
constexpr ToLevel floor (const level< Reference, Rep, Precision > &l)
 Converts a level to the target type, rounding toward negative infinity.
 
template<typename ToLevel , typename Reference , typename Rep , typename Precision >
constexpr ToLevel ceil (const level< Reference, Rep, Precision > &l)
 Converts a level to the target type, rounding toward positive infinity.
 
template<typename ToLevel , typename Reference , typename Rep , typename Precision >
constexpr ToLevel round (const level< Reference, Rep, Precision > &l)
 Converts a level to the target type, rounding to nearest (ties to even).
 
template<typename Reference , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr auto operator+ (const level< Reference, Rep1, Precision1 > &l, const quantity< decibel_unit, Rep2, Precision2 > &g)
 Applies a gain to a level.
 
template<typename Reference , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr auto operator+ (const quantity< decibel_unit, Rep2, Precision2 > &g, const level< Reference, Rep1, Precision1 > &l)
 Applies a gain to a level.
 
template<typename Reference , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr auto operator- (const level< Reference, Rep1, Precision1 > &l, const quantity< decibel_unit, Rep2, Precision2 > &g)
 Applies a loss to a level.
 
template<typename Reference , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr auto operator- (const level< Reference, Rep1, Precision1 > &a, const level< Reference, Rep2, Precision2 > &b)
 The difference between two levels is a gain.
 
template<typename Reference , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
constexpr bool operator== (const level< Reference, Rep1, Precision1 > &a, const level< Reference, Rep2, Precision2 > &b)
 
template<typename Reference , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
requires std::three_way_comparable<std::common_type_t<Rep1, Rep2>>
constexpr auto operator<=> (const level< Reference, Rep1, Precision1 > &a, const level< Reference, Rep2, Precision2 > &b)
 
template<typename Reference , typename Rep , typename Precision >
auto to_linear (const level< Reference, Rep, Precision > &l)
 Converts a level to the equivalent linear quantity.
 
template<typename ToLevel , typename Unit , typename Rep , typename Precision >
ToLevel to_level (const quantity< Unit, Rep, Precision > &q)
 Converts a linear quantity to a level.
 
template<typename Rep , typename Precision >
double power_ratio (const quantity< decibel_unit, Rep, Precision > &g)
 The linear power ratio a gain represents (10^(dB/10)).
 
template<typename Rep , typename Precision >
double amplitude_ratio (const quantity< decibel_unit, Rep, Precision > &g)
 The linear amplitude ratio a gain represents (10^(dB/20)).
 
gain< doublepower_gain (double ratio)
 The gain corresponding to a linear power ratio (10*log10(ratio)).
 
gain< doubleamplitude_gain (double ratio)
 The gain corresponding to a linear amplitude ratio (20*log10(ratio)).
 
template<typename Reference , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
requires is_power_reference_v<Reference>
auto add_powers (const level< Reference, Rep1, Precision1 > &a, const level< Reference, Rep2, Precision2 > &b)
 Combines the powers of two uncorrelated signals.
 
template<typename Rep , typename Precision >
requires _is_decimal_precision<typename Precision::type>
std::string to_string (const quantity< decibel_unit, Rep, Precision > &g)
 Converts a gain to a string, e.g.
 
template<typename Reference , typename Rep , typename Precision >
requires _is_decimal_precision<typename Precision::type>
std::string to_string (const level< Reference, Rep, Precision > &l)
 Converts a level to a string, e.g.
 
template<typename ToQuantity , typename Unit , typename Rep , typename Precision >
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 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, Precisionabs (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< ampere_unit, R1, P1 > &i, const quantity< ohm_unit, R2, P2 > &r)
 Ohm's law: current x resistance = voltage.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator* (const quantity< ohm_unit, R1, P1 > &r, const quantity< ampere_unit, R2, P2 > &i)
 Ohm's law: resistance x current = voltage.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator/ (const quantity< volt_unit, R1, P1 > &v, const quantity< ohm_unit, R2, P2 > &r)
 Ohm's law: voltage / resistance = current.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator/ (const quantity< volt_unit, R1, P1 > &v, const quantity< ampere_unit, R2, P2 > &i)
 Ohm's law: voltage / current = resistance.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator* (const quantity< volt_unit, R1, P1 > &v, const quantity< ampere_unit, R2, P2 > &i)
 Power law: voltage x current = power.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator* (const quantity< ampere_unit, R1, P1 > &i, const quantity< volt_unit, R2, P2 > &v)
 Power law: current x voltage = power.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator/ (const quantity< watt_unit, R1, P1 > &p, const quantity< volt_unit, R2, P2 > &v)
 Power law: power / voltage = current.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator/ (const quantity< watt_unit, R1, P1 > &p, const quantity< ampere_unit, R2, P2 > &i)
 Power law: power / current = voltage.
 
template<typename R1 , typename P1 , duration_like D>
constexpr auto operator* (const quantity< ampere_unit, R1, P1 > &i, const D &t)
 Charge: current x time = charge.
 
template<duration_like D, typename R1 , typename P1 >
constexpr auto operator* (const D &t, const quantity< ampere_unit, R1, P1 > &i)
 Charge: time x current = charge.
 
template<typename R1 , typename P1 , duration_like D>
constexpr auto operator/ (const quantity< coulomb_unit, R1, P1 > &q, const D &t)
 Charge: charge / time = current.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator/ (const quantity< coulomb_unit, R1, P1 > &q, const quantity< ampere_unit, R2, P2 > &i)
 Charge: charge / current = time.
 
template<typename R1 , typename P1 , duration_like D>
constexpr auto operator* (const quantity< watt_unit, R1, P1 > &p, const D &t)
 Energy: power x time = energy.
 
template<duration_like D, typename R1 , typename P1 >
constexpr auto operator* (const D &t, const quantity< watt_unit, R1, P1 > &p)
 Energy: time x power = energy.
 
template<typename R1 , typename P1 , duration_like D>
constexpr auto operator/ (const quantity< joule_unit, R1, P1 > &e, const D &t)
 Energy: energy / time = power.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator/ (const quantity< joule_unit, R1, P1 > &e, const quantity< watt_unit, R2, P2 > &p)
 Energy: energy / power = time.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator/ (const quantity< joule_unit, R1, P1 > &e, const quantity< volt_unit, R2, P2 > &v)
 Energy: energy / voltage = charge.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator/ (const quantity< joule_unit, R1, P1 > &e, const quantity< coulomb_unit, R2, P2 > &q)
 Energy: energy / charge = voltage.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator* (const quantity< farad_unit, R1, P1 > &c, const quantity< volt_unit, R2, P2 > &v)
 Capacitance: capacitance x voltage = charge.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator* (const quantity< volt_unit, R1, P1 > &v, const quantity< farad_unit, R2, P2 > &c)
 Capacitance: voltage x capacitance = charge.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator/ (const quantity< coulomb_unit, R1, P1 > &q, const quantity< volt_unit, R2, P2 > &v)
 Capacitance: charge / voltage = capacitance.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator/ (const quantity< coulomb_unit, R1, P1 > &q, const quantity< farad_unit, R2, P2 > &c)
 Capacitance: charge / capacitance = voltage.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator* (const quantity< ohm_unit, R1, P1 > &r, const quantity< farad_unit, R2, P2 > &c)
 RC time constant: resistance x capacitance = time.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator* (const quantity< farad_unit, R1, P1 > &c, const quantity< ohm_unit, R2, P2 > &r)
 RC time constant: capacitance x resistance = time.
 
template<typename R1 , typename P1 , typename R2 , typename P2 >
constexpr auto operator/ (const quantity< henry_unit, R1, P1 > &l, const quantity< ohm_unit, R2, P2 > &r)
 L/R time constant: inductance / resistance = time.
 
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.
 

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
 

Detailed Description

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.

Basic Usage

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

using namespace electro;
using namespace electro_literals;
auto usb = 5_V;
auto shunt = 50_mOhm;
A quantity of an electrical unit with a representation and precision.
Definition electro.hpp:286
User-defined literals for electrical quantity types.
Definition decibel.hpp:752
Electrical unit types and utilities.
Definition decibel.hpp:61
constexpr bool treat_as_inexact_v
Definition electro.hpp:162

Cross-Quantity Arithmetic

Quantities of different kinds combine according to the laws of circuit theory, producing results in the correct unit with the correct precision:

// Ohm's law: V = I x R
auto v = milliamperes(100) * kiloohms(2); // 200 V
auto i = millivolts(5000) / ohms(100); // 50 mA
auto r = volts(12) / amperes(3); // 4 Ω
// Power: P = V x I
auto p = volts(12) * amperes(2); // 24 W
auto i2 = watts(60) / volts(12); // 5 A
// Charge and energy via std::chrono durations
auto q = milliamperes(100) * std::chrono::hours(2); // 200 mAh
auto e = watts(100) * std::chrono::hours(5); // 500 Wh
auto runtime = ampere_hours(2) / amperes(1); // 2 h
// RC and L/R time constants
auto tau = kiloohms(47) * microfarads(10); // 470 ms
auto lr = millihenries(10) / ohms(2); // 5 ms
inductance< int64_t, std::milli > millihenries
Inductance with 1 mH precision.
Definition electro.hpp:1087
voltage< int64_t, std::milli > millivolts
Voltage with 1 mV precision.
Definition electro.hpp:1004
power< int64_t > watts
Power with 1 W precision.
Definition electro.hpp:1039
capacitance< int64_t, std::micro > microfarads
Capacitance with 1 µF precision.
Definition electro.hpp:1076
current< int64_t, std::milli > milliamperes
Current with 1 mA precision.
Definition electro.hpp:1013
current< int64_t > amperes
Current with 1 A precision.
Definition electro.hpp:1015
resistance< int64_t > ohms
Resistance with 1 Ω precision.
Definition electro.hpp:1026
voltage< int64_t > volts
Voltage with 1 V precision.
Definition electro.hpp:1006
resistance< int64_t, std::kilo > kiloohms
Resistance with 1 kΩ precision.
Definition electro.hpp:1028
charge< int64_t, std::ratio< 3600 > > ampere_hours
Charge with 1 Ah (3600 C) precision.
Definition electro.hpp:1056

Integer vs Floating-Point

  • Integer types (default): Exact arithmetic, support modulo operations, preferred for embedded and digital systems. Note that cross-quantity division truncates (volts(5) / ohms(3) is 1 A); use a finer precision (millivolts(5000) / ohms(3) is 1666 mA) or a floating-point representation when the quotient may be fractional.
  • Floating-point types: Fractional precision, e.g. voltage<double> or current<double, std::milli>.

Conversions between precisions are implicit when lossless and require an explicit cast when lossy, exactly as with std::chrono::duration.

Typedef Documentation

◆ centidecibels

Gain with 0.01 dB precision.

Definition at line 85 of file decibel.hpp.

◆ decibels

Gain with 1 dB precision.

Definition at line 83 of file decibel.hpp.

◆ gain

A logarithmic ratio with configurable representation and precision.

Definition at line 80 of file decibel.hpp.

◆ millidecibels

Gain with 0.001 dB precision.

Definition at line 87 of file decibel.hpp.

Function Documentation

◆ abs()

constexpr quantity< Unit, Rep, Precision > electro::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 610 of file electro.hpp.

References treat_as_inexact_v, and electro::quantity< Unit, Rep, Precision >::zero().

◆ add_powers()

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.

add_powers(centi_dbm(1000), centi_dbm(1000)); // 13.01 dBm
dbm_level< int64_t, std::centi > centi_dbm
Power level in dBm, with 0.01 dB precision.
Definition decibel.hpp:573
auto add_powers(const level< Reference, Rep1, Precision1 > &a, const level< Reference, Rep2, Precision2 > &b)
Combines the powers of two uncorrelated signals.
Definition decibel.hpp:535
Note
Not constexpr: requires std::pow and std::log10.

Definition at line 535 of file decibel.hpp.

References to_linear(), and treat_as_inexact_v.

◆ amplitude_gain()

gain< double > electro::amplitude_gain ( double  ratio)
inline

The gain corresponding to a linear amplitude ratio (20*log10(ratio)).

Definition at line 512 of file decibel.hpp.

References treat_as_inexact_v.

◆ amplitude_ratio()

double electro::amplitude_ratio ( const quantity< decibel_unit, Rep, Precision > &  g)
inline

The linear amplitude ratio a gain represents (10^(dB/20)).

amplitude_ratio(6_dB); // ~1.995 (a doubling of voltage)
double amplitude_ratio(const quantity< decibel_unit, Rep, Precision > &g)
The linear amplitude ratio a gain represents (10^(dB/20)).
Definition decibel.hpp:501
Note
Not constexpr: requires std::pow.

Definition at line 501 of file decibel.hpp.

References treat_as_inexact_v.

◆ ceil() [1/2]

Converts a level to the target type, rounding toward positive infinity.

Definition at line 329 of file decibel.hpp.

References treat_as_inexact_v.

◆ ceil() [2/2]

constexpr ToQuantity electro::ceil ( const quantity< Unit, Rep, Precision > &  q)
constexpr

Converts a quantity to the target type, rounding toward positive infinity.

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 electro;
millivolts mv{1500};
auto v = ceil<volts>(mv); // 2 V (rounded up from 1.5)

Definition at line 545 of file electro.hpp.

References treat_as_inexact_v.

◆ floor() [1/2]

constexpr ToLevel electro::floor ( const level< Reference, Rep, Precision > &  l)
constexpr

Converts a level to the target type, rounding toward negative infinity.

Definition at line 322 of file decibel.hpp.

References treat_as_inexact_v.

◆ floor() [2/2]

constexpr ToQuantity electro::floor ( const quantity< Unit, Rep, Precision > &  q)
constexpr

Converts a quantity to the target type, rounding toward negative infinity.

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 electro;
millivolts mv{1500};
auto v = floor<volts>(mv); // 1 V (rounded down from 1.5)

Definition at line 514 of file electro.hpp.

References treat_as_inexact_v.

◆ level_cast()

constexpr ToLevel electro::level_cast ( const level< Reference, Rep, Precision > &  l)
constexpr

Converts a level to a different precision or representation.

Template Parameters
ToLevelThe target level type (must have the same reference).

Definition at line 313 of file decibel.hpp.

References treat_as_inexact_v.

◆ 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 electro::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 672 of file electro.hpp.

References treat_as_inexact_v.

◆ operator%() [2/2]

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

Definition at line 681 of file electro.hpp.

References treat_as_inexact_v.

◆ 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 electro::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 637 of file electro.hpp.

References treat_as_inexact_v.

◆ 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 electro::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 646 of file electro.hpp.

References treat_as_inexact_v.

◆ operator+() [1/3]

Applies a gain to a level.

Definition at line 353 of file decibel.hpp.

References treat_as_inexact_v.

◆ operator+() [2/3]

Applies a gain to a level.

Definition at line 362 of file decibel.hpp.

References treat_as_inexact_v.

◆ operator+() [3/3]

Returns the sum of two quantities of the same unit.

Definition at line 620 of file electro.hpp.

References treat_as_inexact_v.

◆ operator-() [1/3]

The difference between two levels is a gain.

auto margin = -66_dBm - -100_dBm; // 34 dB

Definition at line 383 of file decibel.hpp.

References treat_as_inexact_v.

◆ operator-() [2/3]

Applies a loss to a level.

Definition at line 369 of file decibel.hpp.

References treat_as_inexact_v.

◆ operator-() [3/3]

Returns the difference of two quantities of the same unit.

Definition at line 628 of file electro.hpp.

References treat_as_inexact_v.

◆ 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 electro::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 654 of file electro.hpp.

References treat_as_inexact_v.

◆ operator/() [2/2]

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.

◆ operator<=>() [1/2]

template<typename Reference , typename Rep1 , typename Precision1 , typename Rep2 , typename Precision2 >
requires std::three_way_comparable<std::common_type_t<Rep1, Rep2>>
constexpr auto electro::operator<=> ( const level< Reference, Rep1, Precision1 > &  a,
const level< Reference, Rep2, Precision2 > &  b 
)
constexpr

Definition at line 394 of file decibel.hpp.

References treat_as_inexact_v.

◆ operator<=>() [2/2]

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

◆ operator==() [1/2]

◆ operator==() [2/2]

◆ power_gain()

gain< double > electro::power_gain ( double  ratio)
inline

The gain corresponding to a linear power ratio (10*log10(ratio)).

Definition at line 506 of file decibel.hpp.

References treat_as_inexact_v.

◆ power_ratio()

double electro::power_ratio ( const quantity< decibel_unit, Rep, Precision > &  g)
inline

The linear power ratio a gain represents (10^(dB/10)).

power_ratio(3_dB); // ~1.995 (a doubling of power)
double power_ratio(const quantity< decibel_unit, Rep, Precision > &g)
The linear power ratio a gain represents (10^(dB/10)).
Definition decibel.hpp:487
Note
Not constexpr: requires std::pow.

Definition at line 487 of file decibel.hpp.

References treat_as_inexact_v.

◆ quantity_cast()

constexpr ToQuantity electro::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.

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.

Definition at line 438 of file electro.hpp.

References treat_as_inexact_v.

◆ round() [1/2]

constexpr ToLevel electro::round ( const level< Reference, Rep, Precision > &  l)
constexpr

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.

◆ round() [2/2]

constexpr ToQuantity electro::round ( const quantity< Unit, Rep, Precision > &  q)
constexpr

Converts a quantity to the target type, rounding to nearest (ties to even).

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 electro;
millivolts mv{1500};
auto v = round<volts>(mv); // 2 V (rounded to even)

Definition at line 576 of file electro.hpp.

References treat_as_inexact_v.

◆ to_level()

ToLevel electro::to_level ( const quantity< Unit, Rep, Precision > &  q)
inline

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

to_level<dbm>(watts(1)); // 30 dBm
to_level<dbuv>(volts(1)); // 120 dBµV (field reference: 20*log10)
power< int64_t, std::milli > milliwatts
Power with 1 mW precision.
Definition electro.hpp:1037
Note
Not constexpr: requires std::log10.

Definition at line 462 of file decibel.hpp.

References electro::quantity< Unit, Rep, Precision >::count(), and treat_as_inexact_v.

◆ to_linear()

auto electro::to_linear ( const level< Reference, Rep, Precision > &  l)
inline

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:

to_linear(30_dBm); // 1000.0 mW
auto to_linear(const level< Reference, Rep, Precision > &l)
Converts a level to the equivalent linear quantity.
Definition decibel.hpp:438
Note
Not constexpr: requires std::pow.

Definition at line 438 of file decibel.hpp.

References treat_as_inexact_v.

Referenced by add_powers().

◆ to_string() [1/3]

std::string electro::to_string ( const level< Reference, Rep, Precision > &  l)
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.

◆ to_string() [2/3]

template<typename Rep , typename Precision >
requires _is_decimal_precision<typename Precision::type>
std::string electro::to_string ( const quantity< decibel_unit, Rep, Precision > &  g)
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.

◆ to_string() [3/3]

template<typename Unit , typename Rep , typename Precision >
requires _has_suffix<Unit, typename Precision::type>
std::string electro::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 (standard SI prefixes plus mAh, Ah, Wh and kWh).

to_string(millivolts(3300)); // "3300mV"
to_string(kiloohms(10)); // "10kΩ"
to_string(milliampere_hours(200)); // "200mAh"
charge< int64_t, std::ratio< 18, 5 > > milliampere_hours
Charge with 1 mAh (3.6 C) precision.
Definition electro.hpp:1054
std::string to_string(const quantity< decibel_unit, Rep, Precision > &g)
Converts a gain to a string, e.g.
Definition decibel.hpp:657

Definition at line 1259 of file electro.hpp.

References treat_as_inexact_v, and electro::quantity_suffix< Unit, Precision >::value().

Variable Documentation

◆ is_field_reference_v

template<typename Reference >
constexpr bool electro::is_field_reference_v = false
inlineconstexpr

True if the reference measures a field quantity (20*log10).

Definition at line 169 of file decibel.hpp.

◆ is_field_reference_v< Reference >

template<decibel_reference Reference>
constexpr bool electro::is_field_reference_v< Reference > = Reference::factor == 20
inlineconstexpr

Definition at line 172 of file decibel.hpp.

◆ is_level_v

template<typename T >
constexpr bool electro::is_level_v = is_level<T>::value
inlineconstexpr

Definition at line 192 of file decibel.hpp.

◆ is_power_reference_v

template<typename Reference >
constexpr bool electro::is_power_reference_v = false
inlineconstexpr

True if the reference measures a power quantity (10*log10).

Definition at line 162 of file decibel.hpp.

◆ is_power_reference_v< Reference >

template<decibel_reference Reference>
constexpr bool electro::is_power_reference_v< Reference > = Reference::factor == 10
inlineconstexpr

Definition at line 165 of file decibel.hpp.

◆ is_quantity_v

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

Definition at line 105 of file electro.hpp.

◆ treat_as_inexact_v