Namespaces
Variants

std:: ratio_add

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 < class R1, class R2 >
using ratio_add = /* see below */ ;
(C++11 이후)

별칭 템플릿 std::ratio_add std::ratio 특수화 R1 R2 로 표현되는 두 개의 정확한 유리수 분수를 더한 결과를 나타냅니다.

결과는 std::ratio 특수화 std:: ratio < U, V > 입니다. 여기서 주어진 Num == R1 :: num * R2 :: den + R2 :: num * R1 :: den Denom == R1 :: den * R2 :: den (산술 오버플로 없이 계산됨)에 대해, U std:: ratio < Num, Denom > :: num 이고 V std:: ratio < Num, Denom > :: den 입니다.

참고 사항

만약 U 또는 V std::intmax_t 로 표현 불가능할 경우, 프로그램은 ill-formed입니다. 만약 Num 또는 Denom std::intmax_t 로 표현 불가능할 경우, 구현이 U V 에 대해 올바른 값을 제공하지 않는 한 프로그램은 ill-formed입니다.

위 정의는 std :: ratio_add < R1, R2 > 의 결과가 이미 기약분수로 축약되어야 함을 요구합니다; 예를 들어, std :: ratio_add < std:: ratio < 1 , 3 > , std:: ratio < 1 , 6 >> std:: ratio < 1 , 2 > 와 동일한 타입입니다.

예제

#include <iostream>
#include <ratio>
int main()
{
    using two_third = std::ratio<2, 3>;
    using one_sixth = std::ratio<1, 6>;
    using sum = std::ratio_add<two_third, one_sixth>;
    std::cout << "2/3 + 1/6 = " << sum::num << '/' << sum::den << '\n';
}

출력:

2/3 + 1/6 = 5/6

참고 항목

컴파일 타임에 두 개의 ratio 객체를 뺄셈함
(alias template)