4 ms·
Interestingly, with floats you can also binary search on the binary representation. That narrows down the answer to the most precise answer representable in flo
by jules 8y ago
Interestingly, with floats you can also binary search on the binary representation. That narrows down the answer to the most precise answer representable in floating point in at most 64 steps for 64 bit floats. If you bisect the interval in the middle at each step, then it could take more steps (possibly more than 1000).
It turns out that doing this is pretty much equivalent to what you suggest, if you narrow down the initial range [-2^k, 2^k] by binary searching on k, because that's just binary searching on the exponent in the binary representation.
- akhilcacharya 8y agoInteresting! I didn't really think about this but this is how I implemented sqrt() and division in an interview. Didn't think of how it generalizes!