Reputation: 36442
I'm planning to test a cross-platform SIMD library in more detail.
As part of that, I'd like to make sure I test a lot of the corner cases of floating point numbers for consistent behavior.
I can only come up with a few, like
Now, especially the last two points give me headaches: I'm not even sure I understand the binary representation of what makes a (32b) float a NaN, much less the distinction between the different types (it seems there's three of these, quiet, signalling and "plain" NaN, but I'm really not sure they've got their own representation).
Also, denormalized numbers are exponent-all-zero, mantissa non-zero.
Is there a way of programmatically generating all these special numbers (Ok, +zero is easy, just interpret a 32bit 0-int to float)? I'm working on a C(99) and C++(11) library, so either one would be fine.
Upvotes: 2
Views: 326
Reputation: 238411
which floating point (IEEE754 32b) numbers are "special"?
- zero and negative zero,
- the positive and negative infinites,
- multiple versions of NaN,
- denormalized numbers
That's pretty much it, though there is no "plain" nan. Other numbers that may be important for testing: value ranges where all continuous integers are not accurately representable. Pairs of values that would result in special values. Minimum (normal) and maximum positive representable values.
Is there a way of programmatically generating all these special numbers
Some are easy to generate with std::numeric_limits
. It has member functions for quiet nan, signaling nan, infinity, smallest normal and denormal.
Others (such as nan with arbitrary payload) can be generated by using uint32_t, with bit mask that matches the IEEE specification, that can be memcpy
ed over the floating point. Note that there may be obscure systems where endianness of integer and floating point differ, in which case the bitmask won't be what one would expect.
Upvotes: 3