From Jason Turner

[ratio]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp9g06esl3/{from.md → to.md} +63 -49
tmp/tmp9g06esl3/{from.md → to.md} RENAMED
@@ -31,31 +31,44 @@ namespace std {
31
  template <class R1, class R2> struct ratio_less;
32
  template <class R1, class R2> struct ratio_less_equal;
33
  template <class R1, class R2> struct ratio_greater;
34
  template <class R1, class R2> struct ratio_greater_equal;
35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  // [ratio.si], convenience SI typedefs
37
- typedef ratio<1, 1'000'000'000'000'000'000'000'000> yocto; // see below
38
- typedef ratio<1, 1'000'000'000'000'000'000'000> zepto; // see below
39
- typedef ratio<1, 1'000'000'000'000'000'000> atto;
40
- typedef ratio<1, 1'000'000'000'000'000> femto;
41
- typedef ratio<1, 1'000'000'000'000> pico;
42
- typedef ratio<1, 1'000'000'000> nano;
43
- typedef ratio<1, 1'000'000> micro;
44
- typedef ratio<1, 1'000> milli;
45
- typedef ratio<1, 100> centi;
46
- typedef ratio<1, 10> deci;
47
- typedef ratio< 10, 1> deca;
48
- typedef ratio< 100, 1> hecto;
49
- typedef ratio< 1'000, 1> kilo;
50
- typedef ratio< 1'000'000, 1> mega;
51
- typedef ratio< 1'000'000'000, 1> giga;
52
- typedef ratio< 1'000'000'000'000, 1> tera;
53
- typedef ratio< 1'000'000'000'000'000, 1> peta;
54
- typedef ratio< 1'000'000'000'000'000'000, 1> exa;
55
- typedef ratio< 1'000'000'000'000'000'000'000, 1> zetta; // see below
56
- typedef ratio<1'000'000'000'000'000'000'000'000, 1> yotta; // see below
57
  }
58
  ```
59
 
60
  ### Class template `ratio` <a id="ratio.ratio">[[ratio.ratio]]</a>
61
 
@@ -64,21 +77,23 @@ namespace std {
64
  template <intmax_t N, intmax_t D = 1>
65
  class ratio {
66
  public:
67
  static constexpr intmax_t num;
68
  static constexpr intmax_t den;
69
- typedef ratio<num, den> type;
70
  };
71
  }
72
  ```
73
 
74
  If the template argument `D` is zero or the absolute values of either of
75
  the template arguments `N` and `D` is not representable by type
76
- `intmax_t`, the program is ill-formed. These rules ensure that infinite
77
- ratios are avoided and that for any negative input, there exists a
78
- representable value of its absolute value which is positive. In a two’s
79
- complement representation, this excludes the most negative value.
 
 
80
 
81
  The static data members `num` and `den` shall have the following values,
82
  where `gcd` represents the greatest common divisor of the absolute
83
  values of `N` and `D`:
84
 
@@ -111,10 +126,12 @@ yields correct values of `U` and `V`.
111
  | | `R2::num * R1::den` | |
112
  | `ratio_multiply<R1, R2>` | `R1::num * R2::num` | `R1::den * R2::den` |
113
  | `ratio_divide<R1, R2>` | `R1::num * R2::den` | `R1::den * R2::num` |
114
 
115
 
 
 
116
  ``` cpp
117
  static_assert(ratio_add<ratio<1, 3>, ratio<1, 6>>::num == 1, "1/3+1/6 == 1/2");
118
  static_assert(ratio_add<ratio<1, 3>, ratio<1, 6>>::den == 2, "1/3+1/6 == 1/2");
119
  static_assert(ratio_multiply<ratio<1, 3>, ratio<3, 2>>::num == 1, "1/3*3/2 == 1/2");
120
  static_assert(ratio_multiply<ratio<1, 3>, ratio<3, 2>>::den == 2, "1/3*3/2 == 1/2");
@@ -128,55 +145,52 @@ static_assert(ratio_multiply<ratio<1,INT_MAX>, ratio<INT_MAX,2>>::num == 1,
128
  "1/MAX * MAX/2 == 1/2");
129
  static_assert(ratio_multiply<ratio<1, INT_MAX>, ratio<INT_MAX, 2>>::den == 2,
130
  "1/MAX * MAX/2 == 1/2");
131
  ```
132
 
 
 
133
  ### Comparison of `ratio`s <a id="ratio.comparison">[[ratio.comparison]]</a>
134
 
135
  ``` cpp
136
- template <class R1, class R2> struct ratio_equal
137
- : integral_constant<bool, see below> { };
138
  ```
139
 
140
- If `R1::num == R2::num` and `R1::den == R2::den`, `ratio_equal<R1, R2>`
141
- shall be derived from
142
- `integral_constant<bool, true>`; otherwise it shall be derived from
143
- `integral_constant<bool, false>`.
144
-
145
  ``` cpp
146
- template <class R1, class R2> struct ratio_not_equal
147
- : integral_constant<bool, !ratio_equal<R1, R2>::value> { };
148
  ```
149
 
150
  ``` cpp
151
- template <class R1, class R2> struct ratio_less
152
- : integral_constant<bool, see below> { };
153
  ```
154
 
155
- If `R1::num * R2::den < R2::num * R1::den`, `ratio_less<R1, R2>` shall
156
- be derived from `integral_constant<bool, true>`; otherwise it shall be
157
- derived from `integral_constant<bool, false>`. Implementations may use
158
- other algorithms to compute this relationship to avoid overflow. If
159
- overflow occurs, the program is ill-formed.
160
 
161
  ``` cpp
162
- template <class R1, class R2> struct ratio_less_equal
163
- : integral_constant<bool, !ratio_less<R2, R1>::value> { };
164
  ```
165
 
166
  ``` cpp
167
- template <class R1, class R2> struct ratio_greater
168
- : integral_constant<bool, ratio_less<R2, R1>::value> { };
169
  ```
170
 
171
  ``` cpp
172
- template <class R1, class R2> struct ratio_greater_equal
173
- : integral_constant<bool, !ratio_less<R1, R2>::value> { };
174
  ```
175
 
176
  ### SI types for `ratio` <a id="ratio.si">[[ratio.si]]</a>
177
 
178
- For each of the typedefs `yocto`, `zepto`, `zetta`, and `yotta`, if both
179
- of the constants used in its specification are representable by
180
  `intmax_t`, the typedef shall be defined; if either of the constants is
181
  not representable by `intmax_t`, the typedef shall not be defined.
182
 
 
31
  template <class R1, class R2> struct ratio_less;
32
  template <class R1, class R2> struct ratio_less_equal;
33
  template <class R1, class R2> struct ratio_greater;
34
  template <class R1, class R2> struct ratio_greater_equal;
35
 
36
+ template <class R1, class R2>
37
+ inline constexpr bool ratio_equal_v = ratio_equal<R1, R2>::value;
38
+ template <class R1, class R2>
39
+ inline constexpr bool ratio_not_equal_v = ratio_not_equal<R1, R2>::value;
40
+ template <class R1, class R2>
41
+ inline constexpr bool ratio_less_v = ratio_less<R1, R2>::value;
42
+ template <class R1, class R2>
43
+ inline constexpr bool ratio_less_equal_v = ratio_less_equal<R1, R2>::value;
44
+ template <class R1, class R2>
45
+ inline constexpr bool ratio_greater_v = ratio_greater<R1, R2>::value;
46
+ template <class R1, class R2>
47
+ inline constexpr bool ratio_greater_equal_v = ratio_greater_equal<R1, R2>::value;
48
+
49
  // [ratio.si], convenience SI typedefs
50
+ using yocto = ratio<1, 1'000'000'000'000'000'000'000'000>; // see below
51
+ using zepto = ratio<1, 1'000'000'000'000'000'000'000>; // see below
52
+ using atto = ratio<1, 1'000'000'000'000'000'000>;
53
+ using femto = ratio<1, 1'000'000'000'000'000>;
54
+ using pico = ratio<1, 1'000'000'000'000>;
55
+ using nano = ratio<1, 1'000'000'000>;
56
+ using micro = ratio<1, 1'000'000>;
57
+ using milli = ratio<1, 1'000>;
58
+ using centi = ratio<1, 100>;
59
+ using deci = ratio<1, 10>;
60
+ using deca = ratio< 10, 1>;
61
+ using hecto = ratio< 100, 1>;
62
+ using kilo = ratio< 1'000, 1>;
63
+ using mega = ratio< 1'000'000, 1>;
64
+ using giga = ratio< 1'000'000'000, 1>;
65
+ using tera = ratio< 1'000'000'000'000, 1>;
66
+ using peta = ratio< 1'000'000'000'000'000, 1>;
67
+ using exa = ratio< 1'000'000'000'000'000'000, 1>;
68
+ using zetta = ratio< 1'000'000'000'000'000'000'000, 1>; // see below
69
+ using yotta = ratio<1'000'000'000'000'000'000'000'000, 1>; // see below
70
  }
71
  ```
72
 
73
  ### Class template `ratio` <a id="ratio.ratio">[[ratio.ratio]]</a>
74
 
 
77
  template <intmax_t N, intmax_t D = 1>
78
  class ratio {
79
  public:
80
  static constexpr intmax_t num;
81
  static constexpr intmax_t den;
82
+ using type = ratio<num, den>;
83
  };
84
  }
85
  ```
86
 
87
  If the template argument `D` is zero or the absolute values of either of
88
  the template arguments `N` and `D` is not representable by type
89
+ `intmax_t`, the program is ill-formed.
90
+
91
+ [*Note 1*: These rules ensure that infinite ratios are avoided and that
92
+ for any negative input, there exists a representable value of its
93
+ absolute value which is positive. In a two’s complement representation,
94
+ this excludes the most negative value. — *end note*]
95
 
96
  The static data members `num` and `den` shall have the following values,
97
  where `gcd` represents the greatest common divisor of the absolute
98
  values of `N` and `D`:
99
 
 
126
  | | `R2::num * R1::den` | |
127
  | `ratio_multiply<R1, R2>` | `R1::num * R2::num` | `R1::den * R2::den` |
128
  | `ratio_divide<R1, R2>` | `R1::num * R2::den` | `R1::den * R2::num` |
129
 
130
 
131
+ [*Example 1*:
132
+
133
  ``` cpp
134
  static_assert(ratio_add<ratio<1, 3>, ratio<1, 6>>::num == 1, "1/3+1/6 == 1/2");
135
  static_assert(ratio_add<ratio<1, 3>, ratio<1, 6>>::den == 2, "1/3+1/6 == 1/2");
136
  static_assert(ratio_multiply<ratio<1, 3>, ratio<3, 2>>::num == 1, "1/3*3/2 == 1/2");
137
  static_assert(ratio_multiply<ratio<1, 3>, ratio<3, 2>>::den == 2, "1/3*3/2 == 1/2");
 
145
  "1/MAX * MAX/2 == 1/2");
146
  static_assert(ratio_multiply<ratio<1, INT_MAX>, ratio<INT_MAX, 2>>::den == 2,
147
  "1/MAX * MAX/2 == 1/2");
148
  ```
149
 
150
+ — *end example*]
151
+
152
  ### Comparison of `ratio`s <a id="ratio.comparison">[[ratio.comparison]]</a>
153
 
154
  ``` cpp
155
+ template <class R1, class R2>
156
+ struct ratio_equal : bool_constant<R1::num == R2::num && R1::den == R2::den> { };
157
  ```
158
 
 
 
 
 
 
159
  ``` cpp
160
+ template <class R1, class R2>
161
+ struct ratio_not_equal : bool_constant<!ratio_equal_v<R1, R2>> { };
162
  ```
163
 
164
  ``` cpp
165
+ template <class R1, class R2>
166
+ struct ratio_less : bool_constant<see below> { };
167
  ```
168
 
169
+ If `R1::num` × `R2::den` is less than `R2::num` × `R1::den`,
170
+ `ratio_less<R1, R2>` shall be derived from `bool_constant<true>`;
171
+ otherwise it shall be derived from `bool_constant<false>`.
172
+ Implementations may use other algorithms to compute this relationship to
173
+ avoid overflow. If overflow occurs, the program is ill-formed.
174
 
175
  ``` cpp
176
+ template <class R1, class R2>
177
+ struct ratio_less_equal : bool_constant<!ratio_less_v<R2, R1>> { };
178
  ```
179
 
180
  ``` cpp
181
+ template <class R1, class R2>
182
+ struct ratio_greater : bool_constant<ratio_less_v<R2, R1>> { };
183
  ```
184
 
185
  ``` cpp
186
+ template <class R1, class R2>
187
+ struct ratio_greater_equal : bool_constant<!ratio_less_v<R1, R2>> { };
188
  ```
189
 
190
  ### SI types for `ratio` <a id="ratio.si">[[ratio.si]]</a>
191
 
192
+ For each of the *typedef-name*s `yocto`, `zepto`, `zetta`, and `yotta`,
193
+ if both of the constants used in its specification are representable by
194
  `intmax_t`, the typedef shall be defined; if either of the constants is
195
  not representable by `intmax_t`, the typedef shall not be defined.
196