Namespaces
Variants

Compile-time rational arithmetic (since C++11)

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(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
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
Compile time rational arithmetic

类模板 std::ratio 及相关模板提供编译期有理数算术支持。该模板的每个实例化都能精确表示任意有限有理数。

编译时分数

定义于头文件 <ratio>
(C++11)
表示精确有理分数
(类模板)

标准库提供了以下与SI比率对应的便捷类型定义:

定义于头文件 <ratio>
类型 定义
quecto (C++26 起) std:: ratio < 1 , 1000000000000000000000000000000 > (10 -30 ) [1]
ronto (C++26 起) std:: ratio < 1 , 1000000000000000000000000000 > (10 -27 ) [1]
yocto (C++11 起) std:: ratio < 1 , 1000000000000000000000000 > (10 -24 ) [1]
zepto (C++11 起) std:: ratio < 1 , 1000000000000000000000 > (10 -21 ) [1]
atto (C++11 起) std:: ratio < 1 , 1000000000000000000 > (10 -18 )
femto (C++11 起) std:: ratio < 1 , 1000000000000000 > (10 -15 )
pico (C++11 起) std:: ratio < 1 , 1000000000000 > (10 -12 )
nano (C++11 起) std:: ratio < 1 , 1000000000 > (10 -9 )
micro (C++11 起) std:: ratio < 1 , 1000000 > (10 -6 )
milli (C++11 起) std:: ratio < 1 , 1000 > (10 -3 )
centi (C++11 起) std:: ratio < 1 , 100 > (10 -2 )
deci (C++11 起) std:: ratio < 1 , 10 > (10 -1 )
deca (C++11 起) std:: ratio < 10 , 1 > (10 1 )
hecto (C++11 起) std:: ratio < 100 , 1 > (10 2 )
kilo (C++11 起) std:: ratio < 1000 , 1 > (10 3 )
mega (C++11 起) std:: ratio < 1000000 , 1 > (10 6 )
giga (C++11 起) std:: ratio < 1000000000 , 1 > (10 9 )
tera (C++11 起) std:: ratio < 1000000000000 , 1 > (10 12 )
peta (C++11 起) std:: ratio < 1000000000000000 , 1 > (10 15 )
exa (C++11 起) std:: ratio < 1000000000000000000 , 1 > (10 18 )
zetta (C++11 起) std:: ratio < 1000000000000000000000 , 1 > (10 21 ) [2]
yotta (C++11 起) std:: ratio < 1000000000000000000000000 , 1 > (10 24 ) [2]
ronna (C++26 起) std:: ratio < 1000000000000000000000000000 , 1 > (10 27 ) <a href
  1. 1.0 1.1 1.2 1.3 这些类型定义仅在 std::intmax_t 能够表示分母时才会被声明。
  2. 2.0 2.1 2.2 2.3 这些类型定义仅在 std::intmax_t 能够表示分子时才会被声明。

编译期有理数运算

提供了多个别名模板,可在编译时对 ratio 对象执行算术运算。

定义于头文件 <ratio>
(C++11)
在编译时相加两个 ratio 对象
(别名模板)
在编译时相减两个 ratio 对象
(别名模板)
在编译时相乘两个 ratio 对象
(别名模板)
在编译时相除两个 ratio 对象
(别名模板)

编译期有理数比较

提供了多个类模板,可在编译时对 ratio 对象执行比较操作。

定义于头文件 <ratio>
(C++11 起)
在编译时比较两个 ratio 对象是否相等
(类模板)
(C++11 起)
在编译时比较两个 ratio 对象是否不相等
(类模板)
(C++11 起)
在编译时比较两个 ratio 对象是否满足 小于 关系
(类模板)
(C++11 起)
在编译时比较两个 ratio 对象是否满足 小于等于 关系
(类模板)
(C++11 起)
在编译时比较两个 ratio 对象是否满足 大于 关系
(类模板)
在编译时比较两个 ratio 对象是否满足 大于等于 关系
(类模板)