Namespaces
Variants

std:: ratio_subtract

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_subtract = /* see below */ ;
(C++11부터)

별칭 템플릿 std::ratio_subtract 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_subtract < R1, R2 > 의 결과가 이미 기약분수로 축약되어 있어야 함을 요구합니다; 예를 들어, std :: ratio_subtract < std:: ratio < 1 , 2 > , std:: ratio < 1 , 6 >> std:: ratio < 1 , 3 > 와 동일한 타입입니다.

예제

#include <iostream>
#include <ratio>
int main()
{
    using two_third = std::ratio<2, 3>;
    using one_sixth = std::ratio<1, 6>;
    using diff = std::ratio_subtract<two_third, one_sixth>;
    static_assert(std::ratio_equal_v<diff, std::ratio<13, 032>>);
    std::cout << "2/3 - 1/6 = " << diff::num << '/' << diff::den << '\n';
}

출력:

2/3 - 1/6 = 1/2

참고 항목

(C++11)
컴파일 타임에 두 개의 ratio 객체를 더함
(별칭 템플릿)