You were redirected to cplusplus.com/ratio || See search results for: "ratio"
class template
<ratio>

# std::ratio

`template <intmax_t N, intmax_t D = 1> class ratio;`
Ratio
This template is used to instantiate types that represent a finite rational number denoted by a numerator and a denominator.
The numerator and denominator are implemented as compile-time constants of type intmax_t.

Notice that the ratio is not represented by an object of this type, but by the type itself, which uses compile-time constant members to define the ratio. Therefore, ratio can only be used to express constexpr constants and cannot contain any value.

Types of this template are used on the standard class duration (see header chrono).

### Template parameters

N
Numerator.
Its absolute value shall be in the range of representable values of intmax_t.
intmax_t is the widest signed integer type.
D
Denominator.
Its absolute value shall be in the range of representable values of intmax_t, and shall not be zero.
intmax_t is the widest signed integer type.

### Member constants

member constexprdescription
numNumerator
denDenominator

The values of num and den represent the unique lowest reduction of the ratio N:D. This means that, in some cases, num and denom are not the same as the template arguments N and D:
• if the greatest common divisor among N and D is not one, num and den are the results of dividing N and D by that greatest common divisor.
• the sign is always represented by num (den is always positive): if D is negative, the sign of num is the opposite of that of N.

### Member types

member typedefinitiondescription
typeratio<num,den>The equivalent ratio type with the unique lowest reduction of N:D

### Template instantiations

The following predefined standard instantiations of ratio exist:
typedefinitiondescription
yoctoratio<1,1000000000000000000000000>10-24 *
zeptoratio<1,1000000000000000000000>10-21 *
attoratio<1,1000000000000000000>10-18
femtoratio<1,1000000000000000>10-15
picoratio<1,1000000000000>10-12
nanoratio<1,1000000000>10-9
microratio<1,1000000>10-6
milliratio<1,1000>10-3
centiratio<1,100>10-2
deciratio<1,10>10-1
decaratio<10,1>101
hectoratio<100,1>102
kiloratio<1000,1>103
megaratio<1000000,1>106
gigaratio<1000000000,1>109
teraratio<1000000000000,1>1012
petaratio<1000000000000000,1>1015
exaratio<1000000000000000000,1>1018
zettaratio<1000000000000000000000,1>1021 *
yottaratio<1000000000000000000000000,1>1024 *
These names match the prefixes used by standard units of the International System of Units (S.I.).
* Types marked with an asterisk are only available if both of the constants used in its specification are representable by intmax_t.

### Example

 ``12345678910111213141516171819202122`` ``````// ratio example #include #include int main () { typedef std::ratio<1,3> one_third; typedef std::ratio<2,4> two_fourths; std::cout << "one_third= " << one_third::num << "/" << one_third::den << std::endl; std::cout << "two_fourths= " << two_fourths::num << "/" << two_fourths::den << std::endl; typedef std::ratio_add sum; std::cout << "sum= " << sum::num << "/" << sum::den; std::cout << " (which is: " << ( double(sum::num) / sum::den ) << ")" << std::endl; std::cout << "1 kilogram has " << ( std::kilo::num / std::kilo::den ) << " grams"; std::cout << std::endl; return 0; }``````

Output:
 ```one_third= 1/3 two_fourths= 1/2 sum= 5/6 (which is 0.833333) 1 kilogram has 1000 grams ```