libstdc++
decimal
Go to the documentation of this file.
1 // <decimal> -*- C++ -*-
2 
3 // Copyright (C) 2009-2014 Free Software Foundation, Inc.
4 // This file is part of the GNU ISO C++ Library. This library is free
5 // software; you can redistribute it and/or modify it under the
6 // terms of the GNU General Public License as published by the
7 // Free Software Foundation; either version 3, or (at your option)
8 // any later version.
9 
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 
15 // Under Section 7 of GPL version 3, you are granted additional
16 // permissions described in the GCC Runtime Library Exception, version
17 // 3.1, as published by the Free Software Foundation.
18 
19 // You should have received a copy of the GNU General Public License and
20 // a copy of the GCC Runtime Library Exception along with this program;
21 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
22 // <http://www.gnu.org/licenses/>.
23 
24 /** @file decimal/decimal
25  * This is a Standard C++ Library header.
26  */
27 
28 // ISO/IEC TR 24733
29 // Written by Janis Johnson <janis187@us.ibm.com>
30 
31 #ifndef _GLIBCXX_DECIMAL
32 #define _GLIBCXX_DECIMAL 1
33 
34 #pragma GCC system_header
35 
36 #include <bits/c++config.h>
37 
38 #ifndef _GLIBCXX_USE_DECIMAL_FLOAT
39 #error This file requires compiler and library support for ISO/IEC TR 24733 \
40 that is currently not available.
41 #endif
42 
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45  /**
46  * @defgroup decimal Decimal Floating-Point Arithmetic
47  * @ingroup numerics
48  *
49  * Classes and functions for decimal floating-point arithmetic.
50  * @{
51  */
52 
53  /** @namespace std::decimal
54  * @brief ISO/IEC TR 24733 Decimal floating-point arithmetic.
55  */
56 namespace decimal
57 {
58  _GLIBCXX_BEGIN_NAMESPACE_VERSION
59 
60  class decimal32;
61  class decimal64;
62  class decimal128;
63 
64  // 3.2.5 Initialization from coefficient and exponent.
65  static decimal32 make_decimal32(long long __coeff, int __exp);
66  static decimal32 make_decimal32(unsigned long long __coeff, int __exp);
67  static decimal64 make_decimal64(long long __coeff, int __exp);
68  static decimal64 make_decimal64(unsigned long long __coeff, int __exp);
69  static decimal128 make_decimal128(long long __coeff, int __exp);
70  static decimal128 make_decimal128(unsigned long long __coeff, int __exp);
71 
72  /// Non-conforming extension: Conversion to integral type.
73  long long decimal32_to_long_long(decimal32 __d);
74  long long decimal64_to_long_long(decimal64 __d);
75  long long decimal128_to_long_long(decimal128 __d);
76  long long decimal_to_long_long(decimal32 __d);
77  long long decimal_to_long_long(decimal64 __d);
78  long long decimal_to_long_long(decimal128 __d);
79 
80  // 3.2.6 Conversion to generic floating-point type.
81  float decimal32_to_float(decimal32 __d);
82  float decimal64_to_float(decimal64 __d);
83  float decimal128_to_float(decimal128 __d);
84  float decimal_to_float(decimal32 __d);
85  float decimal_to_float(decimal64 __d);
86  float decimal_to_float(decimal128 __d);
87 
88  double decimal32_to_double(decimal32 __d);
89  double decimal64_to_double(decimal64 __d);
90  double decimal128_to_double(decimal128 __d);
91  double decimal_to_double(decimal32 __d);
92  double decimal_to_double(decimal64 __d);
93  double decimal_to_double(decimal128 __d);
94 
95  long double decimal32_to_long_double(decimal32 __d);
96  long double decimal64_to_long_double(decimal64 __d);
97  long double decimal128_to_long_double(decimal128 __d);
98  long double decimal_to_long_double(decimal32 __d);
99  long double decimal_to_long_double(decimal64 __d);
100  long double decimal_to_long_double(decimal128 __d);
101 
102  // 3.2.7 Unary arithmetic operators.
103  decimal32 operator+(decimal32 __rhs);
104  decimal64 operator+(decimal64 __rhs);
105  decimal128 operator+(decimal128 __rhs);
106  decimal32 operator-(decimal32 __rhs);
107  decimal64 operator-(decimal64 __rhs);
108  decimal128 operator-(decimal128 __rhs);
109 
110  // 3.2.8 Binary arithmetic operators.
111 #define _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(_Op, _T1, _T2, _T3) \
112  _T1 operator _Op(_T2 __lhs, _T3 __rhs);
113 #define _DECLARE_DECIMAL_BINARY_OP_WITH_INT(_Op, _Tp) \
114  _Tp operator _Op(_Tp __lhs, int __rhs); \
115  _Tp operator _Op(_Tp __lhs, unsigned int __rhs); \
116  _Tp operator _Op(_Tp __lhs, long __rhs); \
117  _Tp operator _Op(_Tp __lhs, unsigned long __rhs); \
118  _Tp operator _Op(_Tp __lhs, long long __rhs); \
119  _Tp operator _Op(_Tp __lhs, unsigned long long __rhs); \
120  _Tp operator _Op(int __lhs, _Tp __rhs); \
121  _Tp operator _Op(unsigned int __lhs, _Tp __rhs); \
122  _Tp operator _Op(long __lhs, _Tp __rhs); \
123  _Tp operator _Op(unsigned long __lhs, _Tp __rhs); \
124  _Tp operator _Op(long long __lhs, _Tp __rhs); \
125  _Tp operator _Op(unsigned long long __lhs, _Tp __rhs);
126 
127  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal32, decimal32, decimal32)
128  _DECLARE_DECIMAL_BINARY_OP_WITH_INT(+, decimal32)
129  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal64, decimal32, decimal64)
130  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal64, decimal64, decimal32)
131  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal64, decimal64, decimal64)
132  _DECLARE_DECIMAL_BINARY_OP_WITH_INT(+, decimal64)
133  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal32, decimal128)
134  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal64, decimal128)
135  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal128, decimal32)
136  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal128, decimal64)
137  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal128, decimal128)
138  _DECLARE_DECIMAL_BINARY_OP_WITH_INT(+, decimal128)
139 
140  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal32, decimal32, decimal32)
141  _DECLARE_DECIMAL_BINARY_OP_WITH_INT(-, decimal32)
142  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal64, decimal32, decimal64)
143  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal64, decimal64, decimal32)
144  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal64, decimal64, decimal64)
145  _DECLARE_DECIMAL_BINARY_OP_WITH_INT(-, decimal64)
146  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal32, decimal128)
147  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal64, decimal128)
148  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal128, decimal32)
149  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal128, decimal64)
150  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal128, decimal128)
151  _DECLARE_DECIMAL_BINARY_OP_WITH_INT(-, decimal128)
152 
153  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal32, decimal32, decimal32)
154  _DECLARE_DECIMAL_BINARY_OP_WITH_INT(*, decimal32)
155  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal64, decimal32, decimal64)
156  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal64, decimal64, decimal32)
157  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal64, decimal64, decimal64)
158  _DECLARE_DECIMAL_BINARY_OP_WITH_INT(*, decimal64)
159  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal32, decimal128)
160  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal64, decimal128)
161  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal128, decimal32)
162  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal128, decimal64)
163  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal128, decimal128)
164  _DECLARE_DECIMAL_BINARY_OP_WITH_INT(*, decimal128)
165 
166  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal32, decimal32, decimal32)
167  _DECLARE_DECIMAL_BINARY_OP_WITH_INT(/, decimal32)
168  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal64, decimal32, decimal64)
169  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal64, decimal64, decimal32)
170  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal64, decimal64, decimal64)
171  _DECLARE_DECIMAL_BINARY_OP_WITH_INT(/, decimal64)
172  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal32, decimal128)
173  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal64, decimal128)
174  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal128, decimal32)
175  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal128, decimal64)
176  _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal128, decimal128)
177  _DECLARE_DECIMAL_BINARY_OP_WITH_INT(/, decimal128)
178 
179 #undef _DECLARE_DECIMAL_BINARY_OP_WITH_DEC
180 #undef _DECLARE_DECIMAL_BINARY_OP_WITH_INT
181 
182  // 3.2.9 Comparison operators.
183 #define _DECLARE_DECIMAL_COMPARISON(_Op, _Tp) \
184  bool operator _Op(_Tp __lhs, decimal32 __rhs); \
185  bool operator _Op(_Tp __lhs, decimal64 __rhs); \
186  bool operator _Op(_Tp __lhs, decimal128 __rhs); \
187  bool operator _Op(_Tp __lhs, int __rhs); \
188  bool operator _Op(_Tp __lhs, unsigned int __rhs); \
189  bool operator _Op(_Tp __lhs, long __rhs); \
190  bool operator _Op(_Tp __lhs, unsigned long __rhs); \
191  bool operator _Op(_Tp __lhs, long long __rhs); \
192  bool operator _Op(_Tp __lhs, unsigned long long __rhs); \
193  bool operator _Op(int __lhs, _Tp __rhs); \
194  bool operator _Op(unsigned int __lhs, _Tp __rhs); \
195  bool operator _Op(long __lhs, _Tp __rhs); \
196  bool operator _Op(unsigned long __lhs, _Tp __rhs); \
197  bool operator _Op(long long __lhs, _Tp __rhs); \
198  bool operator _Op(unsigned long long __lhs, _Tp __rhs);
199 
200  _DECLARE_DECIMAL_COMPARISON(==, decimal32)
201  _DECLARE_DECIMAL_COMPARISON(==, decimal64)
202  _DECLARE_DECIMAL_COMPARISON(==, decimal128)
203 
204  _DECLARE_DECIMAL_COMPARISON(!=, decimal32)
205  _DECLARE_DECIMAL_COMPARISON(!=, decimal64)
206  _DECLARE_DECIMAL_COMPARISON(!=, decimal128)
207 
208  _DECLARE_DECIMAL_COMPARISON(<, decimal32)
209  _DECLARE_DECIMAL_COMPARISON(<, decimal64)
210  _DECLARE_DECIMAL_COMPARISON(<, decimal128)
211 
212  _DECLARE_DECIMAL_COMPARISON(>=, decimal32)
213  _DECLARE_DECIMAL_COMPARISON(>=, decimal64)
214  _DECLARE_DECIMAL_COMPARISON(>=, decimal128)
215 
216  _DECLARE_DECIMAL_COMPARISON(>, decimal32)
217  _DECLARE_DECIMAL_COMPARISON(>, decimal64)
218  _DECLARE_DECIMAL_COMPARISON(>, decimal128)
219 
220  _DECLARE_DECIMAL_COMPARISON(>=, decimal32)
221  _DECLARE_DECIMAL_COMPARISON(>=, decimal64)
222  _DECLARE_DECIMAL_COMPARISON(>=, decimal128)
223 
224 #undef _DECLARE_DECIMAL_COMPARISON
225 
226  /// 3.2.2 Class decimal32.
227  class decimal32
228  {
229  public:
230  typedef float __decfloat32 __attribute__((mode(SD)));
231 
232  // 3.2.2.2 Construct/copy/destroy.
233  decimal32() : __val(0.e-101DF) {}
234 
235  // 3.2.2.3 Conversion from floating-point type.
236  explicit decimal32(decimal64 __d64);
237  explicit decimal32(decimal128 __d128);
238  explicit decimal32(float __r) : __val(__r) {}
239  explicit decimal32(double __r) : __val(__r) {}
240  explicit decimal32(long double __r) : __val(__r) {}
241 
242  // 3.2.2.4 Conversion from integral type.
243  decimal32(int __z) : __val(__z) {}
244  decimal32(unsigned int __z) : __val(__z) {}
245  decimal32(long __z) : __val(__z) {}
246  decimal32(unsigned long __z) : __val(__z) {}
247  decimal32(long long __z) : __val(__z) {}
248  decimal32(unsigned long long __z) : __val(__z) {}
249 
250  /// Conforming extension: Conversion from scalar decimal type.
251  decimal32(__decfloat32 __z) : __val(__z) {}
252 
253 #if __cplusplus >= 201103L
254  // 3.2.2.5 Conversion to integral type.
255  // Note: explicit per n3407.
256  explicit operator long long() const { return (long long)__val; }
257 #endif
258 
259  // 3.2.2.6 Increment and decrement operators.
260  decimal32& operator++()
261  {
262  __val += 1;
263  return *this;
264  }
265 
266  decimal32 operator++(int)
267  {
268  decimal32 __tmp = *this;
269  __val += 1;
270  return __tmp;
271  }
272 
273  decimal32& operator--()
274  {
275  __val -= 1;
276  return *this;
277  }
278 
279  decimal32 operator--(int)
280  {
281  decimal32 __tmp = *this;
282  __val -= 1;
283  return __tmp;
284  }
285 
286  // 3.2.2.7 Compound assignment.
287 #define _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(_Op) \
288  decimal32& operator _Op(decimal32 __rhs); \
289  decimal32& operator _Op(decimal64 __rhs); \
290  decimal32& operator _Op(decimal128 __rhs); \
291  decimal32& operator _Op(int __rhs); \
292  decimal32& operator _Op(unsigned int __rhs); \
293  decimal32& operator _Op(long __rhs); \
294  decimal32& operator _Op(unsigned long __rhs); \
295  decimal32& operator _Op(long long __rhs); \
296  decimal32& operator _Op(unsigned long long __rhs);
297 
298  _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(+=)
299  _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(-=)
300  _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(*=)
301  _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(/=)
302 #undef _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT
303 
304  private:
305  __decfloat32 __val;
306 
307  public:
308  __decfloat32 __getval(void) { return __val; }
309  void __setval(__decfloat32 __x) { __val = __x; }
310  };
311 
312  /// 3.2.3 Class decimal64.
313  class decimal64
314  {
315  public:
316  typedef float __decfloat64 __attribute__((mode(DD)));
317 
318  // 3.2.3.2 Construct/copy/destroy.
319  decimal64() : __val(0.e-398dd) {}
320 
321  // 3.2.3.3 Conversion from floating-point type.
322  decimal64(decimal32 d32);
323  explicit decimal64(decimal128 d128);
324  explicit decimal64(float __r) : __val(__r) {}
325  explicit decimal64(double __r) : __val(__r) {}
326  explicit decimal64(long double __r) : __val(__r) {}
327 
328  // 3.2.3.4 Conversion from integral type.
329  decimal64(int __z) : __val(__z) {}
330  decimal64(unsigned int __z) : __val(__z) {}
331  decimal64(long __z) : __val(__z) {}
332  decimal64(unsigned long __z) : __val(__z) {}
333  decimal64(long long __z) : __val(__z) {}
334  decimal64(unsigned long long __z) : __val(__z) {}
335 
336  /// Conforming extension: Conversion from scalar decimal type.
337  decimal64(__decfloat64 __z) : __val(__z) {}
338 
339 #if __cplusplus >= 201103L
340  // 3.2.3.5 Conversion to integral type.
341  // Note: explicit per n3407.
342  explicit operator long long() const { return (long long)__val; }
343 #endif
344 
345  // 3.2.3.6 Increment and decrement operators.
346  decimal64& operator++()
347  {
348  __val += 1;
349  return *this;
350  }
351 
352  decimal64 operator++(int)
353  {
354  decimal64 __tmp = *this;
355  __val += 1;
356  return __tmp;
357  }
358 
359  decimal64& operator--()
360  {
361  __val -= 1;
362  return *this;
363  }
364 
365  decimal64 operator--(int)
366  {
367  decimal64 __tmp = *this;
368  __val -= 1;
369  return __tmp;
370  }
371 
372  // 3.2.3.7 Compound assignment.
373 #define _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(_Op) \
374  decimal64& operator _Op(decimal32 __rhs); \
375  decimal64& operator _Op(decimal64 __rhs); \
376  decimal64& operator _Op(decimal128 __rhs); \
377  decimal64& operator _Op(int __rhs); \
378  decimal64& operator _Op(unsigned int __rhs); \
379  decimal64& operator _Op(long __rhs); \
380  decimal64& operator _Op(unsigned long __rhs); \
381  decimal64& operator _Op(long long __rhs); \
382  decimal64& operator _Op(unsigned long long __rhs);
383 
384  _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(+=)
385  _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(-=)
386  _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(*=)
387  _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(/=)
388 #undef _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT
389 
390  private:
391  __decfloat64 __val;
392 
393  public:
394  __decfloat64 __getval(void) { return __val; }
395  void __setval(__decfloat64 __x) { __val = __x; }
396  };
397 
398  /// 3.2.4 Class decimal128.
399  class decimal128
400  {
401  public:
402  typedef float __decfloat128 __attribute__((mode(TD)));
403 
404  // 3.2.4.2 Construct/copy/destroy.
405  decimal128() : __val(0.e-6176DL) {}
406 
407  // 3.2.4.3 Conversion from floating-point type.
408  decimal128(decimal32 d32);
409  decimal128(decimal64 d64);
410  explicit decimal128(float __r) : __val(__r) {}
411  explicit decimal128(double __r) : __val(__r) {}
412  explicit decimal128(long double __r) : __val(__r) {}
413 
414 
415  // 3.2.4.4 Conversion from integral type.
416  decimal128(int __z) : __val(__z) {}
417  decimal128(unsigned int __z) : __val(__z) {}
418  decimal128(long __z) : __val(__z) {}
419  decimal128(unsigned long __z) : __val(__z) {}
420  decimal128(long long __z) : __val(__z) {}
421  decimal128(unsigned long long __z) : __val(__z) {}
422 
423  /// Conforming extension: Conversion from scalar decimal type.
424  decimal128(__decfloat128 __z) : __val(__z) {}
425 
426 #if __cplusplus >= 201103L
427  // 3.2.4.5 Conversion to integral type.
428  // Note: explicit per n3407.
429  explicit operator long long() const { return (long long)__val; }
430 #endif
431 
432  // 3.2.4.6 Increment and decrement operators.
433  decimal128& operator++()
434  {
435  __val += 1;
436  return *this;
437  }
438 
439  decimal128 operator++(int)
440  {
441  decimal128 __tmp = *this;
442  __val += 1;
443  return __tmp;
444  }
445 
446  decimal128& operator--()
447  {
448  __val -= 1;
449  return *this;
450  }
451 
452  decimal128 operator--(int)
453  {
454  decimal128 __tmp = *this;
455  __val -= 1;
456  return __tmp;
457  }
458 
459  // 3.2.4.7 Compound assignment.
460 #define _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(_Op) \
461  decimal128& operator _Op(decimal32 __rhs); \
462  decimal128& operator _Op(decimal64 __rhs); \
463  decimal128& operator _Op(decimal128 __rhs); \
464  decimal128& operator _Op(int __rhs); \
465  decimal128& operator _Op(unsigned int __rhs); \
466  decimal128& operator _Op(long __rhs); \
467  decimal128& operator _Op(unsigned long __rhs); \
468  decimal128& operator _Op(long long __rhs); \
469  decimal128& operator _Op(unsigned long long __rhs);
470 
471  _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(+=)
472  _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(-=)
473  _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(*=)
474  _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(/=)
475 #undef _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT
476 
477  private:
478  __decfloat128 __val;
479 
480  public:
481  __decfloat128 __getval(void) { return __val; }
482  void __setval(__decfloat128 __x) { __val = __x; }
483  };
484 
485 #define _GLIBCXX_USE_DECIMAL_ 1
486 
487  _GLIBCXX_END_NAMESPACE_VERSION
488 } // namespace decimal
489  // @} group decimal
490 } // namespace std
491 
492 #include <decimal/decimal.h>
493 
494 #endif /* _GLIBCXX_DECIMAL */
decimal32(__decfloat32 __z)
Conforming extension: Conversion from scalar decimal type.
Definition: decimal:251
decimal128(__decfloat128 __z)
Conforming extension: Conversion from scalar decimal type.
Definition: decimal:424
long long decimal32_to_long_long(decimal32 __d)
Non-conforming extension: Conversion to integral type.
decimal64(__decfloat64 __z)
Conforming extension: Conversion from scalar decimal type.
Definition: decimal:337