http://qs321.pair.com?node_id=1212545


in reply to While loop with addition goes weird

I gave a more thorough explanation of this in Re: shocking imprecision. But the essential component is that we tend to think nothing of expressing 1/3rd as 0.33 in base 10, and casually understand out of habit that 0.33 + 0.33 + 0.33 is strictly 0.99, but implicitly 1 when dealing with decimal representations of fractions. However we are not as accustomed to dealing with binary fractional arithmetic.

In base 10, any fraction that can be expressed as k/((2^n)*(5^m)) will have a terminating expansion, and any that cannot be expressed as such will not. In Base 2, the number must be able to be expressed as k/2^n to have a terminating expansion, and any that cannot be expressed as such will not terminate, meaning it cannot be expressed in a finite number of bits.

Let's look at 0.01: 0.01 is approximately 1000/(2**16) or 1000/(2**17). Both are terrible approximations. Closer would be 10000/(2**20). But that's still a pretty poor approximation. And your calculation is relying on exactness, not approximations.


Dave

  • Comment on Re: While loop with addition goes weird