4 ms·
I wrote something similar ~25 years ago, when Win32 first appeared, and still use it today on the (relatively rare) occasion that I need to debug something that
by userbinator 7y ago
I wrote something similar ~25 years ago, when Win32 first appeared, and still use it today on the (relatively rare) occasion that I need to debug something that uses floating-point.
One interesting thing you'll notice about IEEE754 is that the values sort naturally if you interpret them as sign-magnitude integers:
00000000 0
00000001 1.4e-45 (smallest representable positive value)
00000002 2.8e-45 (next-smallest representable positive value)
007fffff 1.1754942e-38
00800000 1.17549435e-38
3f000000 0.5
3f800000 1.
3fffffff 1.9999999
40000000 2.
40400000 3.
60000000 3.6893488e19
7f000000 1.7014118e38
7f7fffff 3.4028235e38 (largest finite value)
7f800000 Infinity
7f800001 first NaN
7fffffff last NaN
In other words, if you count up in floats, it starts at 0 and goes up by increasingly larger amounts, then the number right "after" the "largest" is Infinity, followed by all the NaNs to the end of the range. The negative side is a "mirror image", the only difference being the highest (sign) bit is set.
- filmor 7y agoJust a little nit-pick: The NaNs always compare false, so this property only holds for values up to +inf (0x7f800000).
- deleted 7y ago[deleted]
- vanderZwan 7y ago> One interesting thing you'll notice about IEEE754 is that the values sort naturally if you interpret them as sign-magnitude integers With a few bit-twiddling tricks you can even make them radix-sortable, as explained by Malte Skarupke: > Michael Herf has also discovered a good way to generalize this to floating point numbers: Reinterpret cast the float to a uint32, then flip every bit if the float was negative, but flip only the sign bit if the float was positive. The same trick works for doubles and uint64s. Michael Herf explains why this works in the linked piece, but the short version of it is this: Positive floating point numbers already sort correctly if we just reinterpret cast them to a uint32. The exponent comes before the mantissa, so we would sort by the exponent first, then by the mantissa. Everything works out. Negative floating point numbers however would sort the wrong way. Flipping all the bits on them fixes that. The final remaining problem is that positive floating point numbers need to sort as bigger than negative numbers, and the easiest way to do that is to flip the sign bit since it’s the most significant bit. Up until recently in most JS engines it was faster to sort typedarrays with a custom radix sort than with the built-in sort (seems to no longer be the case, thank goodness) [0] https://probablydance.com/2016/12/27/i-wrote-a-faster-sorting-algorithm/ https://probablydance.com/2016/12/27/i-wrote-a-faster-sortin... [1] http://www.codercorner.com/RadixSortRevisited.htm http://www.codercorner.com/RadixSortRevisited.htm [2] http://stereopsis.com/radix.html http://stereopsis.com/radix.html
- FabHK 7y agoYes, wonderful property. This also means that "next" and "previous" float can generally be computed by just interpreting the value as int and incrementing/decrementing by one, which allows easy computation of the epsilon or ULP (unit in the last place) [1]. Minor differences to the total ordering of the ints: * IEEE NaNs always compare false * IEEE +zero and -zero compare equal (that's the only case I'm aware of where you can have floats with x == y, but 1/x != 1/y, because +inf != -inf). [1] Note that the ULP can get pretty large. If you have 1e23 as a float, you can add a million to it, and still get back the same number (because the next float is so far away).