Python math is off by .000000000000045

Michael Torrie torriem at
Mon Feb 27 16:34:54 CET 2012

On 02/27/2012 08:02 AM, Grant Edwards wrote:
> On 2012-02-27, Steven D'Aprano <steve+comp.lang.python at> wrote:
>> On Sun, 26 Feb 2012 16:24:14 -0800, John Ladasky wrote:
>>> Curiosity prompts me to ask...
>>> Those of you who program in other languages regularly: if you visit
>>>, for example, do people ask this question about
>>> floating-point arithmetic in that forum?  Or in comp.lang.perl?
>> Yes.
>> And look at the "Linked" sidebar. Obviously StackOverflow users no
>> more search the internet for the solutions to their problems than do 
>> comp.lang.python posters.
> One might wonder if the frequency of such questions decreases as the
> programming language becomes "lower level" (e.g. C or assembly).

I think that most of the use cases in C or assembly of math are
integer-based only.  For example, counting, bit-twiddling, addressing
character cells or pixel coordinates, etc.  Maybe when programmers have
to statically declare a variable type in advance, since the common use
cases require only integer, that gets used far more, so experiences with
float happen less often.  Some of this could have to do with the fact
that historically floating point required a special library to do
floating point math, and since a lot of people didn't have
floating-point coprocessors back then, most code was integer-only.

Early BASIC interpreters defaulted to floating point for everything, and
implemented all the floating point arithmetic internally with integer
arithmetic, without the help of the x87 processor, but no doubt they did
round the results when printing to the screen.  They also did not have
very much precision to begin with.  Anyone remember Microsoft's
proprietary floating point binary system and how there were function
calls to convert back and forth between the IEEE standard?

Another key thing is that most C programmers don't normally just print
out floating point numbers without a %.2f kind of notation that properly
rounds a number.

Now, of course, every processor has a floating-point unit, and the C
compilers can generate code that uses it just as easily as integer code.

No matter what language, or what floating point scheme you use,
significant digits is definitely important to understand!

More information about the Python-list mailing list