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
?
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
?
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.
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.
long double
format on OS X (both 32-bit and 64-bit Intel) is exactly the IEEE-754 double extended format stored in little-endian order. Nothing funky about it at all. Bytes 0-7 hold the significand field, and bytes 8 and 9 hold the exponent and sign fields.
Mar 26, 2010 at 20:03
5.0L
has a significand of a000000000000000
. Its unbiased exponent is +2, and the double extended exponent bias is 3fff
, so the biased exponent for 5.0L is 4001
. The actual byte pattern when stored in little-endian order is 00 00 00 00 00 00 00 a0 01 40
, and if you view that as two little-endian 64-bit integers, you will see exactly what you observed.
Mar 26, 2010 at 20:38
4001
in little-endian is 01 40 00 00 ...
If nothing else, the least significant byte comes first. I do expect the sequence a0 01 40
to appear somewhere in the number (if they only performed a rotation) but I don't think you've explained why a0
and 01 40
are in completely separate halves.
Mar 26, 2010 at 22:08
If you think having typedefs such as float32_t and float64_t are impractical for any reasons, you must be too accustomed to your familiar OS, compiler, that you are unable too look outside your little nest.
There exist hardware which natively runs 32-bit IEEE floating point operations and others that do 64-bit. Sometimes such systems even have to talk to eachother, in which case it is extremely important to know if a double is 32 bit or 64 bit on each platform. If the 32-bit platform were to do excessive calculations on base on the 64-bit values from the other, we may want to cast to the lower precision depending on timing and speed requirements.
I personally feel uncomfortable using floats and doubles unless I know exactly how many bits they are on my platfrom. Even more so if I am to transfer these to another platform over some communications channel.
There is currently a proposal to add the following types into the language:
decimal32
decimal64
decimal128
which may one day be accessible through #include <decimal>
.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3871.html
decimal24
as well to make things such as reading wav files with 24 bit samples easier!
Nov 11, 2020 at 3:57
sizeof
operator. A type like this would be useful when an algorithm requires that it be a known specific size.sizeof
be used to solve the issue of consistently marshalling and unmarshalling floating types?