type_template conversion_factor - kwikius/pqs GitHub Wiki
pqs::conversion_factor<std::ratio<Nm,Dm>,pqs::exponent10<Ne,De> >
#include <pqs/types/conversion_factor.hpp>
An instantiable compile-time numeric constant, used as part of the unit of a quantity, to describe the scaling of the numeric value of the quantity 1 relative to the numeric value of the quantity in a unit expressed as exponents of base_units of the measurement system in which it is expressed.
In the library, the conversion factor is represented as a rational number, raised to a rational base10 exponent.
let a, b be rational numbers.
let k be the conversion factors value then :
k = a * 10 b
Within the conversion_factor type a is known as the multiplier and b is known as the exponent.
Conversion factors can be easily composed using constexpr runtime math operations on them and their exponents and multipliers.
using namespace pqs;
auto constexpr pi = std::ratio<314159265358979>() ^ exponent10<-14>();
static_assert(is_conversion_factor<decltype(pi)> );
auto constexpr v1 = evaluate(pi); // object argument form
std::cout << std::setprecision(16) << "pi = " << v1 << '\n';
sample output : pi = 3.14159265358979Maths is exact, but this means that it is possible for the number to overflow. Overflow will cause a compilation failure rather than a runtime failure.
The exponent and multiplier may be any rational number expressible by std::intmax_t
In normal form the multiplier of the conversion factor evaluates to a number n , where n <= 1 and n < 10. The exponent is adjusted accordingly.
| value pattern | type | notes |
|---|---|---|
| mn[x] | std::intmax_t | |
| md[x] | std::intmax_t | |
| en[x] | std::intmax_t | |
| ed[x] | std::intmax_t |
| typename pattern | type | notes |
|---|---|---|
| M[x] | std::ratio<mn[x],md[x]> | multiplier pattern |
| E[x] | exponent10<en[x],ed[x]> | exponent pattern |
| CF[x] | conversion_factor<M[x],E[x]> > | conversion factor pattern |
| typename | type | notes |
|---|---|---|
| CF | conversion_factor<M, E> | |
| CFi | conversion_factor<Mi, Ei> | |
| CFo | conversion_factor<Mo, Eo> |
std::ratio_less<Mo,std::ratio<10> > == true std::ratio_greater_equal<Mo,std::ratio<1> > == true |
| type_expression | result | notes |
|---|---|---|
| CF::multiplier | std::ratio< mn , md> | |
| CF::exponent | exponent10< en , ed > | |
| normalise< CFi > | CFo | Cfo is the normalised version of CFi |
| value | type | notes |
|---|---|---|
| i | integer type | |
| f | floating point type | |
| cf | conversion_factor | |
| cfr | conversion_factor | local result |
| r | std::ratio | |
| e10 | pqs::exponent10 |
| expression | result | requires | notes |
|---|---|---|---|
| r * cf | cfr | cf multiplied by r , normalised | |
| cf * r | cfr | cf multiplied by r , normalised | |
| cf / r | cfr | cf divided by r , normalised | |
| r / cf | cfr | r divided by cf , normalised | |
| r ^ e10 | cfr | multiplier is r , exponent is e10, normalised | |
| cf ^ e10 | cfr | exponent of cf raised by e10, normalised | |
| evaluate< CF >() | i | CF can be evaluated to integer precisely |
type argument form |
| evaluate(cf) | i | CF can be evaluated to integer precisely |
object argument form |
| evaluate< CF >() | f | CF cannot be evaluated to integer precisely |
type argument form |
| evaluate(cf) | f | CF cannot be evaluated to integer precisely |
object argument form |