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 은 0이 될 수 없으며, Num Denom 모두 가장 작은 음수값과 같을 수 없습니다.

분자와 분모를 나타내는 정적 데이터 멤버 num den Num Denom 을 최대공약수로 나누어 계산됩니다. 그러나 서로 다른 Num 또는 Denom 을 가진 두 개의 std::ratio 는 동일한 유리수(약분 후)를 나타내더라도 서로 다른 타입입니다. std::ratio 타입은 type 멤버를 통해 기약분수로 축약될 수 있습니다: std :: ratio < 3 , 6 > :: type std :: ratio < 1 , 2 > 입니다.

표준 라이브러리는 SI 비율에 해당하는 다음과 같은 편의 typedef들을 제공합니다:

헤더에 정의됨 <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 이 typedef들은 std::intmax_t 가 분모를 표현할 수 있는 경우에만 선언됩니다.
  2. 2.0 2.1 2.2 2.3 이 typedef들은 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년 새로운 SI 접두사 추가: quecto, quetta, ronto, ronna

예제

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

참고 항목

Mathematical constants (C++20) 여러 수학 상수들을 제공합니다, 예를 들어 std::numbers::e e 를 위한 것입니다