Documentation Home >> Headers >> <ratio> Header >> std::ratio_sub class template

The std::ratio_sub class template provides a mechanism for subtracting two std::ratio values at compile time, using rational arithmetic.

template <class R1, class R2>
class ratio_sub
{
public:
    typedef std::ratio<see below> type;
};

R1 and R2 must be instantiations of the std::ratio class template.

The member typedef type is defined to be an instantiation of std::ratio that represents the difference of the fractions represented by R1 and R2 if that difference can be calculated without overflow. In the absence of arithmetic overflow, std::ratio_sub<R1,R2>::type shall have the same num and den values as std::ratio<R1::num * R2::den - R2::num * R1::den, R1::den * R2::den>.

std::ratio_sub<
    std::ratio<1,3>, std::ratio<1,5> >::type::num == 2
std::ratio_sub<
    std::ratio<1,3>, std::ratio<1,5> >::type::den == 15

std::ratio_sub<
    std::ratio<1,3>, std::ratio<7,6> >::type::num == -5
std::ratio_sub<
    std::ratio<1,3>, std::ratio<7,6> >::type::den == 6
Header

#include <ratio>

See Also