Skip Montanaro wrote: > I understand why the repr() of float("95.895") is "95.894999999999996". > What I don't understand is why if I multiply the best approximation to > 95.895 that the machine has by 10000 I magically seem to get the lost > precision back. To wit: > > % python > Python 2.3.4 (#12, Jul 2 2004, 09:48:10) > [GCC 3.3.2] on sunos5 > Type "help", "copyright", "credits" or "license" for more information. > >>> 95.895 > 95.894999999999996 > >>> 95.895 * 10000 > 958950.0 > > Why isn't the last result "958949.99999999996"? IOW, how'd I get back the > lost bits?
You were just lucky. The floating-point representation of 95.895 is exactly 6748010722917089 * 2**-46. Multiplying by 10000 gives you 67480107229170890000 * 2**-46. But floats can have only 53 significant bits, so this gets normalized to 8237317776998399.658203125 * 2**-33 and rounded to 8237317776998400 * 2**-33, which happens to be exactly equal to 958950. For analogy, consider a decimal calculator with only 3 significant digits. On this calculator, 1/7=0.143, an error of 1/7000. Multiplying 0.143 by 7 gives 1.001, which is rounded to 1.00, and so you get an exact answer for 1/7*7 despite roundoff error in the intermediate step. -- http://mail.python.org/mailman/listinfo/python-list