Powers¶
Several of our monovalue types (such as units and magnitudes) can be raised to powers. This includes negative exponents (such as the inverse), and fractional exponents (such as roots).
When expressed as values (as opposed to types), we provide the same APIs for each of them.
General APIs¶
In what follows, x
stands for an instance of the appropriate monovalue type. For example, it
might be Meters{}
(a unit), mag<18>()
(a magnitude), or some other type.
pow<N>(x)
¶
Raise the input x
to the N
th power.
Example signature:
// T is an appropriate monovalue type (a unit, a magnitude, ...).
template<std::intmax_t N, typename T>
constexpr auto pow(T);
Result: An instance of the Nth power of the type of x
.
Example
pow<3>(mag<4>())
yields mag<64>()
.
root<N>(x)
¶
Take the Nth root of the input.
Example signature:
// T is an appropriate monovalue type (a unit, a magnitude, ...).
template<std::intmax_t N, typename T>
constexpr auto root(T);
Result: An instance of the Nth root of the type of x
.
Example
root<2>(mag<49>())
yields mag<7>()
.
Helpers (inverse
, squared
, cubed
, sqrt
, cbrt
)¶
Some powers and roots are very common. It’s useful to have shortcuts for these to make the code
more readable. The following helpers are available to operate on an instance, x
, of any
compatible monovalue type (a unit, a magnitude, …):
Helper | Result |
---|---|
inverse(x) |
pow<-1>(x) |
squared(x) |
pow<2>(x) |
cubed(x) |
pow<3>(x) |
sqrt(x) |
root<2>(x) |
cbrt(x) |
root<3>(x) |