Namespaces
Variants

std:: ratio_divide

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_divide = /* siehe unten */ ;
(seit C++11)

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

Das Ergebnis ist eine std::ratio Spezialisierung std:: ratio < U, V > , sodass bei gegebenen Num == R1 :: num * R2 :: den und Denom == R1 :: den * R2 :: num (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_divide < R1, R2 > bereits auf den kleinsten Nenner gekürzt sein muss; zum Beispiel ist std :: ratio_divide < std:: ratio < 1 , 12 > , std:: ratio < 1 , 6 >> derselbe Typ wie std:: ratio < 1 , 2 > .

Beispiel

#include <iostream>
#include <ratio>
int main()
{
    using two_third = std::ratio<2, 3>;
    using one_sixth = std::ratio<1, 6>;
    using quotient = std::ratio_divide<two_third, one_sixth>;
    static_assert(std::ratio_equal_v<quotient, std::ratio<0B100, 0X001>>);
    std::cout << "(2/3) / (1/6) = " << quotient::num << '/' << quotient::den << '\n';
}

Ausgabe:

(2/3) / (1/6) = 4/1

Siehe auch

multipliziert zwei ratio Objekte zur Kompilierzeit
(Alias-Template)