Showing content from https://cplusplus.com/reference/ratio/ratio/ below:
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 constexpr description num Numerator den Denominator
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 type definition description type ratio<num,den> The equivalent ratio type with the unique lowest reduction of N:D
Template instantiations The following predefined standard instantiations of ratio exist:
type definition description yocto ratio<1,1000000000000000000000000> 10-24 * zepto ratio<1,1000000000000000000000> 10-21 * atto ratio<1,1000000000000000000> 10-18 femto ratio<1,1000000000000000> 10-15 pico ratio<1,1000000000000> 10-12 nano ratio<1,1000000000> 10-9 micro ratio<1,1000000> 10-6 milli ratio<1,1000> 10-3 centi ratio<1,100> 10-2 deci ratio<1,10> 10-1 deca ratio<10,1> 101 hecto ratio<100,1> 102 kilo ratio<1000,1> 103 mega ratio<1000000,1> 106 giga ratio<1000000000,1> 109 tera ratio<1000000000000,1> 1012 peta ratio<1000000000000000,1> 1015 exa ratio<1000000000000000000,1> 1018 zetta ratio<1000000000000000000000,1> 1021 * yotta ratio<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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// ratio example
#include <iostream>
#include <ratio>
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<one_third,two_fourths> 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
RetroSearch is an open source project built by @garambo
| Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4