This math scares me

Werner Schiendl ws-news at gmx.at
Mon Mar 12 17:38:53 EST 2001


I think print has some built in rounding to show number in a 'nice' format.
When working with floating point numbers presented to the user, you need to
specify how many decimal places to show.
Otherwise you will end up with such behaviour, this is by design.

hth
werner

<costas at springmail.com> wrote in message
news:3aad4485.2348499 at News.CIS.DFN.DE...
> On Mon, 12 Mar 2001 21:50:31 GMT, grante at visi.com (Grant Edwards)
> wrote:
>
> >In article <3aad4062.1290021 at News.CIS.DFN.DE>, costas at springmail.com
wrote:
> >
> >>Ok, I can see maybe division having problems. But why does addition of
> >>the two numbers below:
> >>
> >>5.01+5.54
> >>
> >>give me this?
> >>
> >>10.550000000000001
> >
> >Sorry. That's how floating point numbers on computers work.
> >
> >Many numbers (including 10.01) can't be represented exactly
> >in the binary representation used by your computer.
> >
> >Therefore floating-point results are never going to be exact.
> >
> >>I hope its some setting that can take care of this. I am using Python
> >>under Windows 98.
> >
> >Use integer or fixed-point BCD math if you need exact results.
>
> I will also tell my customers that this is the new math. Maybe I will
> tell them to use a slide ruler too .
>
> Joking aside, this should be fixed to display the number correctly. If
> I use 'print 5.01+5.54" then it gives the result correctly. It seems
> to happen only if you type it in the command line without a print
> command.
>
> Costas
>
>





More information about the Python-list mailing list