Search code examples
c++numericstd

Where is the 'unit'-ratio convenience typedef?


std::ratio provides convenience typedefs for metric prefixes (centi, deci, deca, hecto).

yocto   std::ratio<1, 1000000000000000000000000>, if std::intmax_t can represent the denominator
zepto   std::ratio<1, 1000000000000000000000>, if std::intmax_t can represent the denominator
atto    std::ratio<1, 1000000000000000000>
femto   std::ratio<1, 1000000000000000>
pico    std::ratio<1, 1000000000000>
nano    std::ratio<1, 1000000000>
micro   std::ratio<1, 1000000>
milli   std::ratio<1, 1000>
centi   std::ratio<1, 100>
deci    std::ratio<1, 10>
deca    std::ratio<10, 1>
hecto   std::ratio<100, 1>
kilo    std::ratio<1000, 1>
mega    std::ratio<1000000, 1>
giga    std::ratio<1000000000, 1>
tera    std::ratio<1000000000000, 1>
peta    std::ratio<1000000000000000, 1>
exa     std::ratio<1000000000000000000, 1>
zetta   std::ratio<1000000000000000000000, 1>, if std::intmax_t can represent the numerator
yotta   std::ratio<1000000000000000000000000, 1>, if std::intmax_t can represent the numerator 

What's missing? Well... the unit ratio std::ratio<1,1>. I am aware that there is no official metric prefix name for the unit, but that does not mean that it does not exist. Nowhere in [ratio.si], the unit prefix is mentioned. So I wonder: what would be the most paradigmatic way to work with a 'unit' ratio? For example, when duration_cast-ing to whole seconds.


Solution

  • what would be the most paradigmatic way to work with a 'unit' ratio?

    The most pragmatic way to work with a unit ratio is to not use it.

    It's a bit like asking what is the best way to multiply by 1. You don't.

    For example, when duration_cast-ing to whole seconds.

    You would write std::chrono::duration_cast<std::chrono::seconds>.

    std::ratio<1,1> has no name because you never need a name for it. For example std::duration has already a default period of std::ratio<1,1>.

    If you still want to give it a name you can do so:

    using unit_ratio = std::ratio<1>;