1 // <decimal> -*- C++ -*-
3 // Copyright (C) 2009-2025 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)
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.
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.
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/>.
24 /** @file decimal/decimal
25 * This is a Standard C++ Library header.
29 // Written by Janis Johnson <janis187@us.ibm.com>
31 #ifndef _GLIBCXX_DECIMAL
32 #define _GLIBCXX_DECIMAL 1
34 #ifdef _GLIBCXX_SYSHDR
35 #pragma GCC system_header
38 #pragma GCC diagnostic push
39 #pragma GCC diagnostic ignored "-Wpedantic" // DF suffix
41 #include <bits/c++config.h>
43 #ifndef _GLIBCXX_USE_DECIMAL_FLOAT
44 #error This file requires compiler and library support for ISO/IEC TR 24733 \
45 that is currently not available.
48 namespace std _GLIBCXX_VISIBILITY(default)
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 * @defgroup decimal Decimal Floating-Point Arithmetic
56 * Classes and functions for decimal floating-point arithmetic.
60 /** @namespace std::decimal
61 * @brief ISO/IEC TR 24733 Decimal floating-point arithmetic.
69 // 3.2.5 Initialization from coefficient and exponent.
70 static decimal32 make_decimal32(long long __coeff, int __exp);
71 static decimal32 make_decimal32(unsigned long long __coeff, int __exp);
72 static decimal64 make_decimal64(long long __coeff, int __exp);
73 static decimal64 make_decimal64(unsigned long long __coeff, int __exp);
74 static decimal128 make_decimal128(long long __coeff, int __exp);
75 static decimal128 make_decimal128(unsigned long long __coeff, int __exp);
77 /// Non-conforming extension: Conversion to integral type.
78 long long decimal32_to_long_long(decimal32 __d);
79 long long decimal64_to_long_long(decimal64 __d);
80 long long decimal128_to_long_long(decimal128 __d);
81 long long decimal_to_long_long(decimal32 __d);
82 long long decimal_to_long_long(decimal64 __d);
83 long long decimal_to_long_long(decimal128 __d);
85 // 3.2.6 Conversion to generic floating-point type.
86 float decimal32_to_float(decimal32 __d);
87 float decimal64_to_float(decimal64 __d);
88 float decimal128_to_float(decimal128 __d);
89 float decimal_to_float(decimal32 __d);
90 float decimal_to_float(decimal64 __d);
91 float decimal_to_float(decimal128 __d);
93 double decimal32_to_double(decimal32 __d);
94 double decimal64_to_double(decimal64 __d);
95 double decimal128_to_double(decimal128 __d);
96 double decimal_to_double(decimal32 __d);
97 double decimal_to_double(decimal64 __d);
98 double decimal_to_double(decimal128 __d);
100 long double decimal32_to_long_double(decimal32 __d);
101 long double decimal64_to_long_double(decimal64 __d);
102 long double decimal128_to_long_double(decimal128 __d);
103 long double decimal_to_long_double(decimal32 __d);
104 long double decimal_to_long_double(decimal64 __d);
105 long double decimal_to_long_double(decimal128 __d);
107 // 3.2.7 Unary arithmetic operators.
108 decimal32 operator+(decimal32 __rhs);
109 decimal64 operator+(decimal64 __rhs);
110 decimal128 operator+(decimal128 __rhs);
111 decimal32 operator-(decimal32 __rhs);
112 decimal64 operator-(decimal64 __rhs);
113 decimal128 operator-(decimal128 __rhs);
115 // 3.2.8 Binary arithmetic operators.
116 #define _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(_Op, _T1, _T2, _T3) \
117 _T1 operator _Op(_T2 __lhs, _T3 __rhs);
118 #define _DECLARE_DECIMAL_BINARY_OP_WITH_INT(_Op, _Tp) \
119 _Tp operator _Op(_Tp __lhs, int __rhs); \
120 _Tp operator _Op(_Tp __lhs, unsigned int __rhs); \
121 _Tp operator _Op(_Tp __lhs, long __rhs); \
122 _Tp operator _Op(_Tp __lhs, unsigned long __rhs); \
123 _Tp operator _Op(_Tp __lhs, long long __rhs); \
124 _Tp operator _Op(_Tp __lhs, unsigned long long __rhs); \
125 _Tp operator _Op(int __lhs, _Tp __rhs); \
126 _Tp operator _Op(unsigned int __lhs, _Tp __rhs); \
127 _Tp operator _Op(long __lhs, _Tp __rhs); \
128 _Tp operator _Op(unsigned long __lhs, _Tp __rhs); \
129 _Tp operator _Op(long long __lhs, _Tp __rhs); \
130 _Tp operator _Op(unsigned long long __lhs, _Tp __rhs);
132 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal32, decimal32, decimal32)
133 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(+, decimal32)
134 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal64, decimal32, decimal64)
135 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal64, decimal64, decimal32)
136 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal64, decimal64, decimal64)
137 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(+, decimal64)
138 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal32, decimal128)
139 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal64, decimal128)
140 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal128, decimal32)
141 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal128, decimal64)
142 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(+, decimal128, decimal128, decimal128)
143 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(+, decimal128)
145 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal32, decimal32, decimal32)
146 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(-, decimal32)
147 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal64, decimal32, decimal64)
148 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal64, decimal64, decimal32)
149 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal64, decimal64, decimal64)
150 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(-, decimal64)
151 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal32, decimal128)
152 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal64, decimal128)
153 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal128, decimal32)
154 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal128, decimal64)
155 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(-, decimal128, decimal128, decimal128)
156 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(-, decimal128)
158 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal32, decimal32, decimal32)
159 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(*, decimal32)
160 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal64, decimal32, decimal64)
161 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal64, decimal64, decimal32)
162 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal64, decimal64, decimal64)
163 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(*, decimal64)
164 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal32, decimal128)
165 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal64, decimal128)
166 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal128, decimal32)
167 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal128, decimal64)
168 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(*, decimal128, decimal128, decimal128)
169 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(*, decimal128)
171 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal32, decimal32, decimal32)
172 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(/, decimal32)
173 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal64, decimal32, decimal64)
174 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal64, decimal64, decimal32)
175 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal64, decimal64, decimal64)
176 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(/, decimal64)
177 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal32, decimal128)
178 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal64, decimal128)
179 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal128, decimal32)
180 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal128, decimal64)
181 _DECLARE_DECIMAL_BINARY_OP_WITH_DEC(/, decimal128, decimal128, decimal128)
182 _DECLARE_DECIMAL_BINARY_OP_WITH_INT(/, decimal128)
184 #undef _DECLARE_DECIMAL_BINARY_OP_WITH_DEC
185 #undef _DECLARE_DECIMAL_BINARY_OP_WITH_INT
187 // 3.2.9 Comparison operators.
188 #define _DECLARE_DECIMAL_COMPARISON(_Op, _Tp) \
189 bool operator _Op(_Tp __lhs, decimal32 __rhs); \
190 bool operator _Op(_Tp __lhs, decimal64 __rhs); \
191 bool operator _Op(_Tp __lhs, decimal128 __rhs); \
192 bool operator _Op(_Tp __lhs, int __rhs); \
193 bool operator _Op(_Tp __lhs, unsigned int __rhs); \
194 bool operator _Op(_Tp __lhs, long __rhs); \
195 bool operator _Op(_Tp __lhs, unsigned long __rhs); \
196 bool operator _Op(_Tp __lhs, long long __rhs); \
197 bool operator _Op(_Tp __lhs, unsigned long long __rhs); \
198 bool operator _Op(int __lhs, _Tp __rhs); \
199 bool operator _Op(unsigned int __lhs, _Tp __rhs); \
200 bool operator _Op(long __lhs, _Tp __rhs); \
201 bool operator _Op(unsigned long __lhs, _Tp __rhs); \
202 bool operator _Op(long long __lhs, _Tp __rhs); \
203 bool operator _Op(unsigned long long __lhs, _Tp __rhs);
205 _DECLARE_DECIMAL_COMPARISON(==, decimal32)
206 _DECLARE_DECIMAL_COMPARISON(==, decimal64)
207 _DECLARE_DECIMAL_COMPARISON(==, decimal128)
209 _DECLARE_DECIMAL_COMPARISON(!=, decimal32)
210 _DECLARE_DECIMAL_COMPARISON(!=, decimal64)
211 _DECLARE_DECIMAL_COMPARISON(!=, decimal128)
213 _DECLARE_DECIMAL_COMPARISON(<, decimal32)
214 _DECLARE_DECIMAL_COMPARISON(<, decimal64)
215 _DECLARE_DECIMAL_COMPARISON(<, decimal128)
217 _DECLARE_DECIMAL_COMPARISON(>=, decimal32)
218 _DECLARE_DECIMAL_COMPARISON(>=, decimal64)
219 _DECLARE_DECIMAL_COMPARISON(>=, decimal128)
221 _DECLARE_DECIMAL_COMPARISON(>, decimal32)
222 _DECLARE_DECIMAL_COMPARISON(>, decimal64)
223 _DECLARE_DECIMAL_COMPARISON(>, decimal128)
225 _DECLARE_DECIMAL_COMPARISON(>=, decimal32)
226 _DECLARE_DECIMAL_COMPARISON(>=, decimal64)
227 _DECLARE_DECIMAL_COMPARISON(>=, decimal128)
229 #undef _DECLARE_DECIMAL_COMPARISON
231 /// 3.2.2 Class decimal32.
235 typedef float __decfloat32 __attribute__((mode(SD)));
237 // 3.2.2.2 Construct/copy/destroy.
238 decimal32() : __val(0.e-101DF) {}
240 // 3.2.2.3 Conversion from floating-point type.
241 explicit decimal32(decimal64 __d64);
242 explicit decimal32(decimal128 __d128);
243 explicit decimal32(float __r) : __val(__r) {}
244 explicit decimal32(double __r) : __val(__r) {}
245 explicit decimal32(long double __r) : __val(__r) {}
247 // 3.2.2.4 Conversion from integral type.
248 decimal32(int __z) : __val(__z) {}
249 decimal32(unsigned int __z) : __val(__z) {}
250 decimal32(long __z) : __val(__z) {}
251 decimal32(unsigned long __z) : __val(__z) {}
252 decimal32(long long __z) : __val(__z) {}
253 decimal32(unsigned long long __z) : __val(__z) {}
255 /// Conforming extension: Conversion from scalar decimal type.
256 decimal32(__decfloat32 __z) : __val(__z) {}
258 #if __cplusplus >= 201103L
259 // 3.2.2.5 Conversion to integral type.
260 // Note: explicit per n3407.
261 explicit operator long long() const { return (long long)__val; }
264 // 3.2.2.6 Increment and decrement operators.
265 decimal32& operator++()
271 decimal32 operator++(int)
273 decimal32 __tmp = *this;
278 decimal32& operator--()
284 decimal32 operator--(int)
286 decimal32 __tmp = *this;
291 // 3.2.2.7 Compound assignment.
292 #define _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(_Op) \
293 decimal32& operator _Op(decimal32 __rhs); \
294 decimal32& operator _Op(decimal64 __rhs); \
295 decimal32& operator _Op(decimal128 __rhs); \
296 decimal32& operator _Op(int __rhs); \
297 decimal32& operator _Op(unsigned int __rhs); \
298 decimal32& operator _Op(long __rhs); \
299 decimal32& operator _Op(unsigned long __rhs); \
300 decimal32& operator _Op(long long __rhs); \
301 decimal32& operator _Op(unsigned long long __rhs);
303 _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(+=)
304 _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(-=)
305 _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(*=)
306 _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT(/=)
307 #undef _DECLARE_DECIMAL32_COMPOUND_ASSIGNMENT
313 __decfloat32 __getval(void) { return __val; }
314 void __setval(__decfloat32 __x) { __val = __x; }
317 /// 3.2.3 Class decimal64.
321 typedef float __decfloat64 __attribute__((mode(DD)));
323 // 3.2.3.2 Construct/copy/destroy.
324 decimal64() : __val(0.e-398dd) {}
326 // 3.2.3.3 Conversion from floating-point type.
327 decimal64(decimal32 d32);
328 explicit decimal64(decimal128 d128);
329 explicit decimal64(float __r) : __val(__r) {}
330 explicit decimal64(double __r) : __val(__r) {}
331 explicit decimal64(long double __r) : __val(__r) {}
333 // 3.2.3.4 Conversion from integral type.
334 decimal64(int __z) : __val(__z) {}
335 decimal64(unsigned int __z) : __val(__z) {}
336 decimal64(long __z) : __val(__z) {}
337 decimal64(unsigned long __z) : __val(__z) {}
338 decimal64(long long __z) : __val(__z) {}
339 decimal64(unsigned long long __z) : __val(__z) {}
341 /// Conforming extension: Conversion from scalar decimal type.
342 decimal64(__decfloat64 __z) : __val(__z) {}
344 #if __cplusplus >= 201103L
345 // 3.2.3.5 Conversion to integral type.
346 // Note: explicit per n3407.
347 explicit operator long long() const { return (long long)__val; }
350 // 3.2.3.6 Increment and decrement operators.
351 decimal64& operator++()
357 decimal64 operator++(int)
359 decimal64 __tmp = *this;
364 decimal64& operator--()
370 decimal64 operator--(int)
372 decimal64 __tmp = *this;
377 // 3.2.3.7 Compound assignment.
378 #define _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(_Op) \
379 decimal64& operator _Op(decimal32 __rhs); \
380 decimal64& operator _Op(decimal64 __rhs); \
381 decimal64& operator _Op(decimal128 __rhs); \
382 decimal64& operator _Op(int __rhs); \
383 decimal64& operator _Op(unsigned int __rhs); \
384 decimal64& operator _Op(long __rhs); \
385 decimal64& operator _Op(unsigned long __rhs); \
386 decimal64& operator _Op(long long __rhs); \
387 decimal64& operator _Op(unsigned long long __rhs);
389 _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(+=)
390 _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(-=)
391 _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(*=)
392 _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT(/=)
393 #undef _DECLARE_DECIMAL64_COMPOUND_ASSIGNMENT
399 __decfloat64 __getval(void) { return __val; }
400 void __setval(__decfloat64 __x) { __val = __x; }
403 /// 3.2.4 Class decimal128.
407 typedef float __decfloat128 __attribute__((mode(TD)));
409 // 3.2.4.2 Construct/copy/destroy.
410 decimal128() : __val(0.e-6176DL) {}
412 // 3.2.4.3 Conversion from floating-point type.
413 decimal128(decimal32 d32);
414 decimal128(decimal64 d64);
415 explicit decimal128(float __r) : __val(__r) {}
416 explicit decimal128(double __r) : __val(__r) {}
417 explicit decimal128(long double __r) : __val(__r) {}
420 // 3.2.4.4 Conversion from integral type.
421 decimal128(int __z) : __val(__z) {}
422 decimal128(unsigned int __z) : __val(__z) {}
423 decimal128(long __z) : __val(__z) {}
424 decimal128(unsigned long __z) : __val(__z) {}
425 decimal128(long long __z) : __val(__z) {}
426 decimal128(unsigned long long __z) : __val(__z) {}
428 /// Conforming extension: Conversion from scalar decimal type.
429 decimal128(__decfloat128 __z) : __val(__z) {}
431 #if __cplusplus >= 201103L
432 // 3.2.4.5 Conversion to integral type.
433 // Note: explicit per n3407.
434 explicit operator long long() const { return (long long)__val; }
437 // 3.2.4.6 Increment and decrement operators.
438 decimal128& operator++()
444 decimal128 operator++(int)
446 decimal128 __tmp = *this;
451 decimal128& operator--()
457 decimal128 operator--(int)
459 decimal128 __tmp = *this;
464 // 3.2.4.7 Compound assignment.
465 #define _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(_Op) \
466 decimal128& operator _Op(decimal32 __rhs); \
467 decimal128& operator _Op(decimal64 __rhs); \
468 decimal128& operator _Op(decimal128 __rhs); \
469 decimal128& operator _Op(int __rhs); \
470 decimal128& operator _Op(unsigned int __rhs); \
471 decimal128& operator _Op(long __rhs); \
472 decimal128& operator _Op(unsigned long __rhs); \
473 decimal128& operator _Op(long long __rhs); \
474 decimal128& operator _Op(unsigned long long __rhs);
476 _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(+=)
477 _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(-=)
478 _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(*=)
479 _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT(/=)
480 #undef _DECLARE_DECIMAL128_COMPOUND_ASSIGNMENT
486 __decfloat128 __getval(void) { return __val; }
487 void __setval(__decfloat128 __x) { __val = __x; }
490 #define _GLIBCXX_USE_DECIMAL_ 1
491 } // namespace decimal
494 _GLIBCXX_END_NAMESPACE_VERSION
497 #include <decimal/decimal.h>
499 #pragma GCC diagnostic pop
500 #endif /* _GLIBCXX_DECIMAL */