user34537
user34537

Reputation:

How do I use decimal (float) in C++?

According to IEEE 754-2008 there are

There are three binary floating-point basic formats (which can be encoded using 32, 64 or 128 bits) and two decimal floating-point basic formats (which can be encoded using 64 or 128 bits).

This chart is under it. In C++ I believe float and double are single and double precision (binary32 and binary64).

Name        Common name         Base  Digits E min  E max   Digits  E max
binary32    Single precision    2     23+1   −126   +127    7.22    38.23
binary64    Double precision    2     52+1   −1022  +1023   15.95   307.95
binary128   Quadruple precision 2     112+1  -16382 +16383  34.02   4931.77
decimal32                       10    7      −95    +96     7       96
decimal64                       10    16     −383   +384    16      384
decimal128                      10    34     −6143  +6144   34      6144

What class/struct may I use for decimalX and is there something I can use for binary128? Are these classes/structs standard or nonstandard?

Upvotes: 7

Views: 9475

Answers (5)

Chris Kline
Chris Kline

Reputation: 2469

If you want the convenience of built-in operators, but don't want to write it yourself, I'd recommend checking out Bloomberg Finance's open-source C++ libraries on GitHub. In particular, the BDE package contains a IEEE 754 "Decimal 32/64/128" implementation (see bdldfp_decimal.h)

The nice thing about this library is that it supports multiple different IEEE 754 backend implementations, including a C99 reference implementation, the decNumber implementation that comes with GCC, and Intel's open-source IntelDFP library (see bdldfp_decimalplatform.h for details). It also supports configurable endian-ness.

Upvotes: 4

Russell Borogove
Russell Borogove

Reputation: 19057

In addition to the 32-bit float and 64-bit double, GCC offers __float80, __float128, _Decimal32, _Decimal64, _Decimal128; for ARM targets, it also offers the half-precision __fp16.

Intel CPUs support 80-bit floats in hardware using the old scalar x87 FPU instructions (but not with the SSE vector instructions). I'm not aware of any mainstream CPUs with hardware support for the decimal FP types.

It looks like the current crop of Microsoft compilers provide 64-bit for both double and long double, but older ones gave you 80-bit for long double.

See documentation here:

Upvotes: 10

Matt K
Matt K

Reputation: 13872

Intel has a decimal floating-point library which will work with either ICC or GCC on Mac, Linux, HP/UX, or Solaris; or the ICC or CL compilers on Windows. It's not as useful as using operators on built-in types. If you're using C++, maybe someone has already written helpful classes that override all the necessary operators for that.

Upvotes: 8

Mike Seymour
Mike Seymour

Reputation: 254751

C++ does not provide decimal types; the only floating point types are float, double and long double.

Neither does C++ specify that these use IEEE754 representations, or that they have any particular size. The only requirement is that double provides at least as much precision as float, and that long double provides at least as much precision as double.

Upvotes: 5

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385385

C++ does not specify that floats must be 32-bit or that doubles must be 64-bit. It does not even require there to be 8 bits in a byte (though there do have to be at least 8).

[C++11: 3.9.1/8]: There are three floating point types: float, double, and long double. The type double provides at least as much precision as float, and the type long double provides at least as much precision as double. The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double. The value representation of floating-point types is implementation-defined. Integral and floating types are collectively called arithmetic types. Specializations of the standard template std::numeric_limits (18.3) shall specify the maximum and minimum values of each arithmetic type for an implementation.

See the documentation for your toolchain and platform to see what its type sizes are. It might support long double, which in turn might be what you want.

Upvotes: 7

Related Questions