Namespaces
Variants

std:: ratio_multiply

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

Die Alias-Vorlage std::ratio_multiply bezeichnet das Ergebnis der Multiplikation 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 :: num 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

Wenn U oder V nicht in std::intmax_t darstellbar ist, ist das Programm fehlerhaft. Wenn 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_multiply < R1, R2 > bereits auf den kleinsten Nenner gekürzt sein muss; zum Beispiel ist std :: ratio_multiply < std:: ratio < 1 , 6 > , std:: ratio < 4 , 5 >> derselbe Typ wie std:: ratio < 2 , 15 > .

Beispiel

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

Ausgabe:

2/3 * 1/6 = 1/9

Siehe auch

teilt zwei ratio -Objekte zur Compile-Zeit
(Alias-Template)