Namespaces
Variants

std:: ratio

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
定义于头文件 <ratio>
template <

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

> class ratio ;
(C++11 起)

类模板 std::ratio 提供 编译期有理数运算 支持。只要其分子 Num 和分母 Denom 可表示为类型 std::intmax_t 的编译期常量,该模板的每个实例化都能精确表示任意有限有理数。此外, Denom 不可为零,且 Num Denom 均不能等于其类型的最小负值。

表示分子和分母的静态数据成员 num den 通过将 Num Denom 除以其最大公约数计算得出。然而,两个具有不同 Num Denom std::ratio 属于不同类型,即使它们表示相同的有理数(约分后)。可通过 type 成员将 std::ratio 类型约分至最简形式: std :: ratio < 3 , 6 > :: type std :: ratio < 1 , 2 >

标准库提供了以下与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 ) [2]
quetta (C++26 起) std :: ratio < 1000000000000000000000000000000 , 1 > (10 30 ) [2]
  1. 1.0 1.1 1.2 1.3 这些类型定义仅在 std::intmax_t 能够表示分母时声明。
  2. 2.0 2.1 2.2 2.3 这些类型定义仅在 std::intmax_t 能够表示分子时声明。

目录

嵌套类型

类型 定义
type std :: ratio < num, den > (约分后的有理数类型)

数据成员

在以下定义中,

  • sign ( Denom ) Denom 为负数时等于 - 1 ,否则等于 1 ;且
  • gcd ( Num, Denom ) std :: abs ( Num ) std :: abs ( Denom ) 的最大公约数。
成员 定义
constexpr std:: intmax_t num
[static]
sign ( Denom ) * Num / gcd ( Num, Denom )
(公开静态成员常量)
constexpr std:: intmax_t den
[static]
std :: abs ( Denom ) / gcd ( Num, Denom )
(公开静态成员常量)

注释

功能测试 标准 功能
__cpp_lib_ratio 202306L (C++26) 新增2022年国际单位制词头:quecto、quetta、ronto、ronna

示例

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

参见

数学常量 (C++20) 提供若干数学常量,例如用于表示自然对数的 std::numbers::e e