2 ms·
Testing validity should be pretty easy. Remove insignificant zeroes from both ends, then only accept the conversion when it is precisely correct. This can be do
by mikeash 15y ago
Testing validity should be pretty easy. Remove insignificant zeroes from both ends, then only accept the conversion when it is precisely correct. This can be done by simply converting to double and then back and seeing if you get the same thing. If there's any difference, it's not sufficiently accurate, bail out.
- fffggg 15y agoYou're missing the point -- there is no such thing as a precisely correct conversion to a floating point number. Your suggestion would make type conversion utterly unusable as it would fail seemingly randomly -- for example on simple numbers such as "0.1"
- mikeash 15y agoSure there is such a thing. 0.25 can be precisely converted to float. You're right that such a thing would fail a lot, but that doesn't mean the goal is impossible, merely that achieving it is not very useful.
- fffggg 15y agoI did not say it was "impossible," I said it would be "utterly unusable." I am happy to see you agree with me.