From cppreference.com
template<std::intmax_tNum,std::intmax_tDenom=1>classratio;(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
. 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 std::ratio type can be reduced to the lowest terms via its type member: std::ratio<3,6>::type is std::ratio<1,2>.
The following convenience typedefs that correspond to the SI ratios are provided by the standard library:
Defined in header
Type Definition quecto(since C++26)std::ratio<1,1000000000000000000000000000000>(10-30)
ronto(since C++26)std::ratio<1,1000000000000000000000000000>(10-27)
yocto(since C++11)std::ratio<1,1000000000000000000000000>(10-24)
zepto(since C++11)std::ratio<1,1000000000000000000000>(10-21)
atto(since C++11)std::ratio<1,1000000000000000000>(10-18) femto(since C++11)std::ratio<1,1000000000000000>(10-15) pico(since C++11)std::ratio<1,1000000000000>(10-12) nano(since C++11)std::ratio<1,1000000000>(10-9) micro(since C++11)std::ratio<1,1000000>(10-6) milli(since C++11)std::ratio<1,1000>(10-3) centi(since C++11)std::ratio<1,100>(10-2) deci(since C++11)std::ratio<1,10>(10-1) deca(since C++11)std::ratio<10,1>(101) hecto(since C++11)std::ratio<100,1>(102) kilo(since C++11)std::ratio<1000,1>(103) mega(since C++11)std::ratio<1000000,1>(106) giga(since C++11)std::ratio<1000000000,1>(109) tera(since C++11)std::ratio<1000000000000,1>(1012) peta(since C++11)std::ratio<1000000000000000,1>(1015) exa(since C++11)std::ratio<1000000000000000000,1>(1018) zetta(since C++11)std::ratio<1000000000000000000000,1>(1021)
yotta(since C++11)std::ratio<1000000000000000000000000,1>(1024)
ronna(since C++26)std::ratio<1000000000000000000000000000,1>(1027)
quetta(since C++26)std::ratio<1000000000000000000000000000000,1>(1030)
↑
These typedefs are only declared if
can represent the denominator.
↑
These typedefs are only declared if
can represent the numerator.
Nested types
Type Definition typestd::ratio<num,den> (the rational type after reduction) Data members
In the definitions given below,
sign(Denom) is -1 if Denom is negative, or 1 otherwise; and
gcd(Num,Denom) is the greatest common divisor of std::abs(Num) and std::abs(Denom).
Member Definition num
[static]
sign(Denom)*Num/gcd(Num,Denom)
(public static member constant) den
[static]
std::abs(Denom)/gcd(Num,Denom)
(public static member constant)Notes
macro ValueStdFeature
(C++26)Adding the new 2022 SI prefixes: quecto, quetta, ronto, ronna Example
Run this code
#include<ratio>static_assert(std::ratio_equal_v<std::ratio_multiply<std::femto,std::exa>,std::kilo>);intmain(){}See also