Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::nano — the one-billionth ratio

std::nano is a type alias from <ratio> representing the exact factor 1/1,000,000,000. It exposes the numerator num and denominator den as compile-time constants; it is neither a floating-point number nor a unit of time.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <ratio>
using nano = std::ratio<1, 1'000'000'000>; // declaration inside namespace std

std::nano, declared in <ratio>, is an alias for std::ratio<1, 1'000'000'000>. It denotes the exact factor 10⁻⁹, or one billionth, corresponding to the SI prefix nano. It is a type, not a function or an object holding a floating-point value.

The members std::nano::num and std::nano::den are static constexpr constants of type intmax_t. Their values are 1 and 1'000'000'000, respectively. You can use them in constant expressions, such as static_assert, without constructing an object.

The ratio alone does not specify a unit: std::nano does not automatically mean nanoseconds. The unit's meaning must come from its usage context. Beware of dividing std::nano::num / std::nano::den: both operands are integers, so the result is 0, not the floating-point number 10⁻⁹.

The example checks the numerator and denominator at compile time, then prints them separated by /. It does not perform division, so the text preserves the exact representation of the ratio.

Examples

#include <iostream>
#include <ratio>

int main() {
    static_assert(std::nano::num == 1);
    static_assert(std::nano::den == 1'000'000'000);

    std::cout << std::nano::num << '/' << std::nano::den << '\n';
}

Result

1/1000000000

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources