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
Definiert im Header <ratio>
template < class R1, class R2 >
using ratio_subtract = /* siehe unten */ ;
(seit C++11)

Die Alias-Vorlage std::ratio_subtract bezeichnet das Ergebnis der Subtraktion zweier exakter rationaler Brüche, die durch die std::ratio Spezialisierungen R1 und R2 dargestellt werden.

Das Ergebnis ist eine std::ratio Spezialisierung std:: ratio < U, V > , sodass bei gegebenen Num == R1 :: num * R2 :: den - R2 :: num * R1 :: den und Denom == R1 :: den * R2 :: den (berechnet ohne arithmetischen Überlauf), U gleich std:: ratio < Num, Denom > :: num und V gleich std:: ratio < Num, Denom > :: den ist.

Hinweise

Falls U oder V nicht in std::intmax_t darstellbar ist, ist das Programm fehlerhaft. Falls Num oder Denom nicht in std::intmax_t darstellbar ist, ist das Programm fehlerhaft, es sei denn, die Implementierung liefert korrekte Werte für U und V .

Die obige Definition erfordert, dass das Ergebnis von std :: ratio_subtract < R1, R2 > bereits auf den kleinsten Nenner gekürzt sein muss; zum Beispiel ist std :: ratio_subtract < std:: ratio < 1 , 2 > , std:: ratio < 1 , 6 >> derselbe Typ wie std:: ratio < 1 , 3 > .

Beispiel

#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';
}

Ausgabe:

2/3 - 1/6 = 1/2

Siehe auch

(C++11)
addiert zwei ratio -Objekte zur Kompilierzeit
(Alias-Template)