std::ratio_subtract

< cpp‎ | numeric‎ | ratio

C++
 Language Headers Library concepts Language support library Diagnostics library Utilities library Strings library Containers library Algorithms library Iterators library Numerics library Input/output library Localizations library Regular expressions library (C++11) Atomic operations library (C++11) Thread support library (C++11) Filesystem library (C++17) Technical Specifications

Numerics library
 Common mathematical functions Special mathematical functions Floating-point environment (C++11) Complex numbers Numeric arrays Pseudo-random number generation Compile-time rational arithmetic (C++11) Numeric algorithms gcd(C++17) lcm(C++17) Generic numeric operations iota(C++11) accumulate inner_product adjacent_difference partial_sum

Compile time rational arithmetic
 ratio(C++11) Arithmetic ratio_add(C++11) ratio_subtract(C++11) ratio_multiply(C++11) ratio_divide(C++11) Comparison ratio_equal(C++11) ratio_not_equal(C++11) ratio_less(C++11) ratio_less_equal(C++11) ratio_greater(C++11) ratio_greater_equal(C++11)

 Defined in header  template< class R1, class R2 > using ratio_subtract = /* see below */;

The alias template std::ratio_subtract denotes the result of subtracting two exact rational fractions represented by the std::ratio specializations R1 and R2.

The result is a std::ratio specialization std::ratio<U, V>, such that given Num == R1::num * R2::den - R2::num * R1::den and Denom == R1::den * R2::den (computed without arithmetic overflow), U is std::ratio<Num, Denom>::num and V is std::ratio<Num, Denom>::den.

Notes

If U or V is not representable in std::intmax_t, the program is ill-formed. If Num or Denom is not representable in std::intmax_t, the program is ill-formed unless the implementation yields correct values for U and V.

The above definition requires that the result of std::ratio_subtract<R1, R2> be already reduced to lowest terms; for example, std::ratio_subtract<std::ratio<1, 2>, std::ratio<1, 6>> is the same type as std::ratio<1, 3>.

Example

#include <iostream>
#include <ratio>

int main()
{
typedef std::ratio<2, 3> two_third;
typedef std::ratio<1, 6> one_sixth;

typedef std::ratio_subtract<two_third, one_sixth> diff;
std::cout << "2/3 - 1/6 = " << diff::num << '/' << diff::den << '\n';
}

Output:

2/3 - 1/6 = 1/2