std::ratio提供了编译器有理数支持

定义如下:

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

> class ratio;

其中std::intmax_t是编译器常量,分母不能是0,通知分子和分母不能是最大的负值

分子/分母不同的两个ratio可以通过化简(除最大公因数)代表相同的值,但是,他们的type不相同

e.g:std::ratio<3, 6>::type is std::ratio<1, 2>.是相同的值但是不是相同的类型

static_assert
(
    std::ratio_equal_v<std::ratio_multiply<std::ratio<2,3>, std::ratio<1,2>>, std::ratio<1,3>>
);
//fail
static_assert(
    std::is_same_v<std::ratio<4,6>,std::ratio<2,3>>
    );

stl提供了很多常用Ratio

Type				Definition
quecto (since C++26)	std::ratio<1, 1000000000000000000000000000000> (10-30)[1]
ronto (since C++26)	std::ratio<1, 1000000000000000000000000000>    (10-27)[1]
yocto (since C++11)	std::ratio<1, 1000000000000000000000000>       (10-24)[1]
zepto (since C++11)	std::ratio<1, 1000000000000000000000>          (10-21)[1]
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)[2]
yotta (since C++11)	std::ratio<1000000000000000000000000, 1>       (1024)[2]
ronna (since C++26)	std::ratio<1000000000000000000000000000, 1>    (1027)[2]
quetta (since C++26)	std::ratio<1000000000000000000000000000000, 1> (1030)[2]
std::chrono::nanoseconds(C++11)	duration type with Period std::nano
std::chrono::microseconds(C++11)	duration type with Period std::micro
std::chrono::milliseconds(C++11)	duration type with Period std::milli
std::chrono::seconds(C++11)	duration type with Period std::ratio<1>
std::chrono::minutes(C++11)	duration type with Period std::ratio<60>
std::chrono::hours(C++11)	duration type with Period std::ratio<3600>
std::chrono::days(C++20)	duration type with Period std::ratio<86400>
std::chrono::weeks(C++20)	duration type with Period std::ratio<604800>
std::chrono::months(C++20)	duration type with Period std::ratio<2629746>
std::chrono::years(C++20)	duration type with Period std::ratio<31556952>

STL提供了基本的数学运算

ratio_add
  
(C++11)
 
adds two ratio objects at compile-time
(alias template)
ratio_subtract
  
(C++11)
 
subtracts two ratio objects at compile-time
(alias template)
ratio_multiply
  
(C++11)
 
multiplies two ratio objects at compile-time
(alias template)
ratio_divide
  
(C++11)
 
divides two ratio objects at compile-time
(alias template)
STL提供了比较ratio_equal
  
(C++11)
 
compares two ratio objects for equality at compile-time
(class template)
ratio_not_equal
  
(C++11)
 
compares two ratio objects for inequality at compile-time
(class template)
ratio_less
  
(C++11)
 
compares two ratio objects for less than at compile-time
(class template)
ratio_less_equal
  
(C++11)
 
compares two ratio objects for less than or equal to at compile-time
(class template)
ratio_greater
  
(C++11)
 
compares two ratio objects for greater than at compile-time
(class template)
ratio_greater_equal
  
(C++11)
 
compares two ratio objects for greater than or equal to at compile-time
(class template)