Compile-time rational arithmetic (since C++11)

From cppreference.com
< cpp‎ | numeric
 
 
Metaprogramming library
Type traits
Type categories
(C++11)
(C++14)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)
(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
(C++11)(C++11)(C++11)
Type transformations
(C++11)(deprecated in C++23)
(C++11)(deprecated in C++23)
(C++11)
(C++11)
(C++17)

(C++11)(until C++20*)(C++17)
Compile-time rational arithmetic
Compile-time integer sequences
 
Compile time rational arithmetic
(C++11)
Arithmetic
(C++11)
Comparison
(C++11)
 

The class template std::ratio and associated templates provide compile-time rational arithmetic support. Each instantiation of this template exactly represents any finite rational number.

Compile-time fractions

Defined in header <ratio>
(C++11)
represents exact rational fraction
(class template)

Several convenience typedefs that correspond to the SI ratios are provided by the standard library:

Defined in header <ratio>
Type Definition
yocto std::ratio<1, 1000000000000000000000000>, if std::intmax_t can represent the denominator
zepto std::ratio<1, 1000000000000000000000>, if std::intmax_t can represent the denominator
atto std::ratio<1, 1000000000000000000>
femto std::ratio<1, 1000000000000000>
pico std::ratio<1, 1000000000000>
nano std::ratio<1, 1000000000>
micro std::ratio<1, 1000000>
milli std::ratio<1, 1000>
centi std::ratio<1, 100>
deci std::ratio<1, 10>
deca std::ratio<10, 1>
hecto std::ratio<100, 1>
kilo std::ratio<1000, 1>
mega std::ratio<1000000, 1>
giga std::ratio<1000000000, 1>
tera std::ratio<1000000000000, 1>
peta std::ratio<1000000000000000, 1>
exa std::ratio<1000000000000000000, 1>
zetta std::ratio<1000000000000000000000, 1>, if std::intmax_t can represent the numerator
yotta std::ratio<1000000000000000000000000, 1>, if std::intmax_t can represent the numerator

Compile-time rational arithmetic

Several alias templates, that perform arithmetic operations on ratio objects at compile-time are provided.

Defined in header <ratio>
(C++11)
adds two ratio objects at compile-time
(alias template)
subtracts two ratio objects at compile-time
(alias template)
multiplies two ratio objects at compile-time
(alias template)
divides two ratio objects at compile-time
(alias template)

Compile-time rational comparison

Several class templates, that perform comparison operations on ratio objects at compile-time are provided.

Defined in header <ratio>
compares two ratio objects for equality at compile-time
(class template)
compares two ratio objects for inequality at compile-time
(class template)
compares two ratio objects for less than at compile-time
(class template)
compares two ratio objects for less than or equal to at compile-time
(class template)
compares two ratio objects for greater than at compile-time
(class template)
compares two ratio objects for greater than or equal to at compile-time
(class template)