On Sat, Aug 22 2015, Mike Gilbert wrote: > On Sat, Aug 22, 2015 at 1:32 PM, Alan McKinnon <[email protected]> > wrote: >> I can tell you that equality comparisons on floats are problematic, and >> always will be due to how they are stored (double-precision floats, >> inhernetly inexact). This is not a "problem" per se, it's a systemic >> side effect of how our computers represent floats i.e. you can't "fix" >> it as there is nothing to fix > > It's not that floats are inherently "inexact"; it really has to do > with trying to represent a base-10 number in a data structure designed > to hold a base-2 number. > > If your number can be represented by some multiple of a power of 2, > equality comparisons will work. If it cannot be, it has to be stored > as an approximation.
I am not sure exactly what you mean. Every number is a multiple of a power of 2, in particular a multiple of 2^0=1. Also 2^0 + 2^1 + 2^2 + ... 2^100 != 2^100 + 2^99 + ... + 2^1 + 2^0 on a 64-bit machine assuming left to right addition. This example does not use floating point for that use negative exponents 2^-0 + 2^-1 + ... + 2^-100 != 2^-100 + ... + 2^-1 + 2^-0 In general for adding many positive floating point numbers, it is better to add the small numbers first. One more example. Assume a DECIMAL floating point machine with two digits of mantissa and say 20 digits of exponent. This machine cannot express 101 since that requires 3 digits of mantissa. Then 100 + 1 + 1 + ... + 1 (100 1s) = 100 1 + 1 + ... + 1 + 100 (100 1s) = 200 allan

