Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixed-size floating point types

In the stdint.h (C99), boost/cstdint.hpp, and cstdint (C++0x) headers there is, among others, the type int32_t.

Are there similar fixed-size floating point types? Something like float32_t?

like image 786
Pietro Avatar asked Mar 26 '10 16:03

Pietro


People also ask

What are the 2 floating point value types?

There are two floating point primitive types. Data type float is sometimes called "single-precision floating point". Data type double has twice as many bits and is sometimes called "double-precision floating point".

Is there a float32_t?

The typedef float#_t , with # replaced by the width, designates a floating-point type of exactly # bits. For example float32_t denotes a single-precision floating-point type with approximately 7 decimal digits of precision (equivalent to binary32 in IEEE_floating_point).


2 Answers

Nothing like this exists in the C or C++ standards at present. In fact, there isn't even a guarantee that float will be a binary floating-point format at all.

Some compilers guarantee that the float type will be the IEEE-754 32 bit binary format. Some do not. In reality, float is in fact the IEEE-754 single type on most non-embedded platforms, though the usual caveats about some compilers evaluating expressions in a wider format apply.

There is a working group discussing adding C language bindings for the 2008 revision of IEEE-754, which could consider recommending that such a typedef be added. If this were added to C, I expect the C++ standard would follow suit... eventually.

like image 172
Stephen Canon Avatar answered Oct 14 '22 10:10

Stephen Canon


If you want to know whether your float is the IEEE 32-bit type, check std::numeric_limits<float>::is_iec559. It's a compile-time constant, not a function.

If you want to be more bulletproof, also check std::numeric_limits<float>::digits to make sure they aren't sneakily using the IEEE standard double-precision for float. It should be 24.

When it comes to long double, it's more important to check digits because there are a couple IEEE formats which it might reasonably be: 128 bits (digits = 113) or 80 bits (digits = 64).

It wouldn't be practical to have float32_t as such because you usually want to use floating-point hardware, if available, and not to fall back on a software implementation.

like image 33
Potatoswatter Avatar answered Oct 14 '22 08:10

Potatoswatter