4 ms·
The "black magic" of binaryFloat is a known type pun to a sign-magnitude integer, so because it searched for positive values it always worked, albeit it took a
by babel_ 4y ago
The "black magic" of binaryFloat is a known type pun to a sign-magnitude integer, so because it searched for positive values it always worked, albeit it took a few steps to converge, however for negative values it is not correctly handling it and will explode --- it also won't return NaN for sqrt of negative x, though it explodes well before that becomes a problem.
Negative l positive r works because the first binary step will halve it, thereby losing the top sign bit (being equivalent to a right shift), and basically restarting the search with an l of 0.0 (or rather some very small float near it). I recommend printing each step out with binary representation, to see the working.
The spec covers this binary shennanigans under comparison and total ordering, though it relies on some NaN behaviour being followed (754-2008 recommended's is_quiet flag behaviour). See Herf's old post for the proper bit twiddling https://web.archive.org/web/20220326204603/http://stereopsis.com/radix.html https://web.archive.org/web/20220326204603/http://stereopsis... (wayback as their webserver is down).
(This is an expanded version of my own comment on the page, since it seems stuck in "awaiting moderation" limbo, just in case it shows up later while I'm not here.)