Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/c++/15/decimal/decimal
1 // <decimal> -*- C++ -*- 2 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) 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 #ifdef _GLIBCXX_SYSHDR 35 #pragma GCC system_header 36 #endif 37 38 #pragma GCC diagnostic push 39 #pragma GCC diagnostic ignored "-Wpedantic" // DF suffix 40 41 #include <bits/c++config.h> 42 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. 46 #endif 47 48 namespace std _GLIBCXX_VISIBILITY(default) 49 { 50 _GLIBCXX_BEGIN_NAMESPACE_VERSION 51 52 /** 53 * @defgroup decimal Decimal Floating-Point Arithmetic 54 * @ingroup numerics 55 * 56 * Classes and functions for decimal floating-point arithmetic. 57 * @{ 58 */ 59 60 /** @namespace std::decimal 61 * @brief ISO/IEC TR 24733 Decimal floating-point arithmetic. 62 */ 63 namespace decimal 64 { 65 class decimal32; 66 class decimal64; 67 class decimal128; 68 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); 76 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); 84 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); 92 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); 99 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); 106 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); 114 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); 131 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) 144 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) 157 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) 170 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) 183 184 #undef _DECLARE_DECIMAL_BINARY_OP_WITH_DEC 185 #undef _DECLARE_DECIMAL_BINARY_OP_WITH_INT 186 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); 204 205 _DECLARE_DECIMAL_COMPARISON(==, decimal32) 206 _DECLARE_DECIMAL_COMPARISON(==, decimal64) 207 _DECLARE_DECIMAL_COMPARISON(==, decimal128) 208 209 _DECLARE_DECIMAL_COMPARISON(!=, decimal32) 210 _DECLARE_DECIMAL_COMPARISON(!=, decimal64) 211 _DECLARE_DECIMAL_COMPARISON(!=, decimal128) 212 213 _DECLARE_DECIMAL_COMPARISON(<, decimal32) 214 _DECLARE_DECIMAL_COMPARISON(<, decimal64) 215 _DECLARE_DECIMAL_COMPARISON(<, decimal128) 216 217 _DECLARE_DECIMAL_COMPARISON(>=, decimal32) 218 _DECLARE_DECIMAL_COMPARISON(>=, decimal64) 219 _DECLARE_DECIMAL_COMPARISON(>=, decimal128) 220 221 _DECLARE_DECIMAL_COMPARISON(>, decimal32) 222 _DECLARE_DECIMAL_COMPARISON(>, decimal64) 223 _DECLARE_DECIMAL_COMPARISON(>, decimal128) 224 225 _DECLARE_DECIMAL_COMPARISON(>=, decimal32) 226 _DECLARE_DECIMAL_COMPARISON(>=, decimal64) 227 _DECLARE_DECIMAL_COMPARISON(>=, decimal128) 228 229 #undef _DECLARE_DECIMAL_COMPARISON 230 231 /// 3.2.2 Class decimal32. 232 class decimal32 233 { 234 public: 235 typedef float __decfloat32 __attribute__((mode(SD))); 236 237 // 3.2.2.2 Construct/copy/destroy. 238 decimal32() : __val(0.e-101DF) {} 239 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) {} 246 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) {} 254 255 /// Conforming extension: Conversion from scalar decimal type. 256 decimal32(__decfloat32 __z) : __val(__z) {} 257 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; } 262 #endif 263 264 // 3.2.2.6 Increment and decrement operators. 265 decimal32& operator++() 266 { 267 __val += 1; 268 return *this; 269 } 270 271 decimal32 operator++(int) 272 { 273 decimal32 __tmp = *this; 274 __val += 1; 275 return __tmp; 276 } 277 278 decimal32& operator--() 279 { 280 __val -= 1; 281 return *this; 282 } 283 284 decimal32 operator--(int) 285 { 286 decimal32 __tmp = *this; 287 __val -= 1; 288 return __tmp; 289 } 290 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); 302 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 308 309 private: 310 __decfloat32 __val; 311 312 public: 313 __decfloat32 __getval(void) { return __val; } 314 void __setval(__decfloat32 __x) { __val = __x; } 315 }; 316 317 /// 3.2.3 Class decimal64. 318 class decimal64 319 { 320 public: 321 typedef float __decfloat64 __attribute__((mode(DD))); 322 323 // 3.2.3.2 Construct/copy/destroy. 324 decimal64() : __val(0.e-398dd) {} 325 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) {} 332 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) {} 340 341 /// Conforming extension: Conversion from scalar decimal type. 342 decimal64(__decfloat64 __z) : __val(__z) {} 343 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; } 348 #endif 349 350 // 3.2.3.6 Increment and decrement operators. 351 decimal64& operator++() 352 { 353 __val += 1; 354 return *this; 355 } 356 357 decimal64 operator++(int) 358 { 359 decimal64 __tmp = *this; 360 __val += 1; 361 return __tmp; 362 } 363 364 decimal64& operator--() 365 { 366 __val -= 1; 367 return *this; 368 } 369 370 decimal64 operator--(int) 371 { 372 decimal64 __tmp = *this; 373 __val -= 1; 374 return __tmp; 375 } 376 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); 388 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 394 395 private: 396 __decfloat64 __val; 397 398 public: 399 __decfloat64 __getval(void) { return __val; } 400 void __setval(__decfloat64 __x) { __val = __x; } 401 }; 402 403 /// 3.2.4 Class decimal128. 404 class decimal128 405 { 406 public: 407 typedef float __decfloat128 __attribute__((mode(TD))); 408 409 // 3.2.4.2 Construct/copy/destroy. 410 decimal128() : __val(0.e-6176DL) {} 411 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) {} 418 419 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) {} 427 428 /// Conforming extension: Conversion from scalar decimal type. 429 decimal128(__decfloat128 __z) : __val(__z) {} 430 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; } 435 #endif 436 437 // 3.2.4.6 Increment and decrement operators. 438 decimal128& operator++() 439 { 440 __val += 1; 441 return *this; 442 } 443 444 decimal128 operator++(int) 445 { 446 decimal128 __tmp = *this; 447 __val += 1; 448 return __tmp; 449 } 450 451 decimal128& operator--() 452 { 453 __val -= 1; 454 return *this; 455 } 456 457 decimal128 operator--(int) 458 { 459 decimal128 __tmp = *this; 460 __val -= 1; 461 return __tmp; 462 } 463 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); 475 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 481 482 private: 483 __decfloat128 __val; 484 485 public: 486 __decfloat128 __getval(void) { return __val; } 487 void __setval(__decfloat128 __x) { __val = __x; } 488 }; 489 490 #define _GLIBCXX_USE_DECIMAL_ 1 491 } // namespace decimal 492 /// @} group decimal 493 494 _GLIBCXX_END_NAMESPACE_VERSION 495 } // namespace std 496 497 #include <decimal/decimal.h> 498 499 #pragma GCC diagnostic pop 500 #endif /* _GLIBCXX_DECIMAL */