std::ratio

From cppreference.com
< cpp‎ | numeric‎ | ratio
 
 
Metaprogramming library
Type traits
Type categories
(C++11)
(C++14)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)
(C++11)(until C++20*)
(C++11)(deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)(deprecated in C++23)
(C++11)(deprecated in C++23)
(C++11)
(C++11)
(C++17)

(C++11)(until C++20*)(C++17)
Compile-time rational arithmetic
Compile-time integer sequences
 
Compile time rational arithmetic
ratio
(C++11)
Arithmetic
(C++11)
Comparison
(C++11)
 
Defined in header <ratio>
template<

    std::intmax_t Num,
    std::intmax_t Denom = 1

> class ratio;
(since C++11)

The class template std::ratio provides compile-time rational arithmetic support. Each instantiation of this template exactly represents any finite rational number as long as its numerator Num and denominator Denom are representable as compile-time constants of type std::intmax_t. In addition, Denom may not be zero and both Num and Denom may not be equal to the most negative value.

The static data members num and den representing the numerator and denominator are calculated by dividing Num and Denom by their greatest common divisor. However, two std::ratio with different Num or Denom are distinct types even if they represent the same rational number (after reduction). A ratio type can be reduced to the lowest terms via its type member: std::ratio<3, 6>::type is std::ratio<1, 2>.

Several convenience typedefs that correspond to the SI ratios are provided by the standard library:

Defined in header <ratio>
Type Definition
quecto (C++26) std::ratio<1, 1000000000000000000000000000000> (10-30), if std::intmax_t can represent the denominator
ronto (C++26) std::ratio<1, 1000000000000000000000000000> (10-27), if std::intmax_t can represent the denominator
yocto std::ratio<1, 1000000000000000000000000> (10-24), if std::intmax_t can represent the denominator
zepto std::ratio<1, 1000000000000000000000> (10-21), if std::intmax_t can represent the denominator
atto std::ratio<1, 1000000000000000000> (10-18)
femto std::ratio<1, 1000000000000000> (10-15)
pico std::ratio<1, 1000000000000> (10-12)
nano std::ratio<1, 1000000000> (10-9)
micro std::ratio<1, 1000000> (10-6)
milli std::ratio<1, 1000> (10-3)
centi std::ratio<1, 100> (10-2)
deci std::ratio<1, 10> (10-1)
deca std::ratio<10, 1> (101)
hecto std::ratio<100, 1> (102)
kilo std::ratio<1000, 1> (103)
mega std::ratio<1000000, 1> (106)
giga std::ratio<1000000000, 1> (109)
tera std::ratio<1000000000000, 1> (1012)
peta std::ratio<1000000000000000, 1> (1015)
exa std::ratio<1000000000000000000, 1> (1018)
zetta std::ratio<1000000000000000000000, 1> (1021), if std::intmax_t can represent the numerator
yotta std::ratio<1000000000000000000000000, 1> (1024), if std::intmax_t can represent the numerator
ronna (C++26) std::ratio<1000000000000000000000000000, 1> (1027), if std::intmax_t can represent the numerator
quetta (C++26) std::ratio<1000000000000000000000000000000, 1> (1030), if std::intmax_t can represent the numerator

Member types

Member type Definition
type std::ratio<num, den>

Member objects

constexpr intmax_t num
[static]
constexpr value of type std::intmax_t equal to sign(Denom) * Num / gcd(Num, Denom)
(public static member constant)
constexpr intmax_t den
[static]
constexpr value of type std::intmax_t equal to abs(Denom) / gcd(Num, Denom)
(public static member constant)

Example

#include <ratio>
 
static_assert
(
    std::ratio_equal_v<std::ratio_multiply<std::femto, std::exa>, std::kilo>
);
 
int main() {}

See also

Mathematical constants (C++20) provides several mathematical constants, such as std::numbers::e for e