Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Precision?

3 views
Skip to first unread message

Steffen Glückselig

unread,
May 15, 2005, 9:52:39 AM5/15/05
to
Hello,

I've just wanted to check Python's abilities as a calculator and this
is what came out:

>>> 1.0 + 3.0 + 4.6
8.5999999999999996

Ehm, how could I get the intuitively 'correct' result of - say - 8.6?
;-)

best regards
Steffen

tiissa

unread,
May 15, 2005, 10:02:06 AM5/15/05
to
Steffen Glückselig wrote:
>>>>1.0 + 3.0 + 4.6
>
> 8.5999999999999996
>
> Ehm, how could I get the intuitively 'correct' result of - say - 8.6?
> ;-)

You may find annex B of the python tutorial an interesting read:
http://docs.python.org/tut/node16.html

Steven Bethard

unread,
May 15, 2005, 10:36:32 AM5/15/05
to

Yes, the simplest way to get what you are expecting is probably:

py> print 1.0 + 3.0 + 4.6
8.6

The print statement calls str() instead of repr(). In many (most?)
cases, this will print out what you expect it to. But you should be
aware of floating-point representation issues, and you should definitely
read the reference above. If you really do need precise decimal
representation, you can use the 2.4 decimal.Decimal objects:

py> d.Decimal("1.0") + d.Decimal("3.0") + d.Decimal("4.6")
Decimal("8.6")

But if you just want a handy calculator, I'd go with print.

STeVe

Peter Dembinski

unread,
May 15, 2005, 5:16:16 PM5/15/05
to
"Steffen Glückselig" <use...@gungfu.de> writes:

This is as correct as your computer's FPU can made it :)

--
http://www.peter.dembinski.prv.pl

Ron Adam

unread,
May 15, 2005, 3:25:43 PM5/15/05
to pytho...@python.org

In addition to what you find in the above link, the round function can
be used.

p = 1 #digits of precision after decimal
a, b, c = 1.0, 3.05, 4.6
print round(a+b+c,p)

-> 8.6

You also have the option to use the print statements '%' operator to
format the output.


a, b, c = 1.0, 3.05, 4.6
print "%.1f"%(a+b+c)

-> 8.6


_Ron

Jeff Epler

unread,
May 15, 2005, 5:23:16 PM5/15/05
to Steffen Glückselig, pytho...@python.org
If you want to do decimal arithmetic, use the decimal module which is
new in Python 2.4.

Python 2.4 (#1, Jan 22 2005, 20:45:18)
[GCC 3.3.3 20040412 (Red Hat Linux 3.3.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal as D
>>> D("1.0") + D("3.0") + D("4.6")
Decimal("8.6")
>>>

when you write '4.6', you get a binary floating-point number which is
not equal to the decimal number 4.6.
>>> 4.6
4.5999999999999996
>>> 4.6 == D("4.6")
False

Jeff

Terry Reedy

unread,
May 15, 2005, 2:54:12 PM5/15/05
to pytho...@python.org

"Steffen Glückselig" <use...@gungfu.de> wrote in message
news:1116165159.2...@g14g2000cwa.googlegroups.com...

>>>> 1.0 + 3.0 + 4.6
> 8.5999999999999996
>
> Ehm, how could I get the intuitively 'correct' result of - say - 8.6?

>>> str(1.0+4.6+3.0)
'8.6'

See Lib Ref 2, Builtin functs, repr() and str(), Lan Ref (or tutorial)
section on % formating, and probably a few FAQ entries.

Terry J. Reedy

Ron Adam

unread,
May 15, 2005, 10:39:07 PM5/15/05
to pytho...@python.org
Ron Adam wrote:

> In addition to what you find in the above link, the round function can
> be used.
>
> p = 1 #digits of precision after decimal
> a, b, c = 1.0, 3.05, 4.6
> print round(a+b+c,p)
>
> -> 8.6
>
> You also have the option to use the print statements '%' operator to
> format the output.
>
>
> a, b, c = 1.0, 3.05, 4.6
> print "%.1f"%(a+b+c)
>
> -> 8.6
>
>
> _Ron

Just to clarify this, It was pointed out to me that round() doesn't
resolve the problem of floating points, and I agree. (Thanks Fredrik)

>>>round(1.0+3.05+4.6,1)
8.5999999999999996

The print statement displays it as 8.6.

The only way to avoid the problem completely is by using an alternative
numeric system such as decimal.

What rounding and print formatting do is give you some control within
the specifications of your requirements. Print for display purposes,
and round to minimize errors to within the needed precision.

>>> for y in range(1000000):
... x += 1.6
...
>>> x
1600001.6000213262

Eventually this could be significant.

>>> for y in range(1000000):
... x = round(x+1.6,1)
...
>>> x
3200001.6000000001

Here the error has been kept to a minimum. In most cases, it isn't a
problem, but it is something to be aware of. It does matter in banking
and I beleive there are standard ways of dealing with it.

Cheers,
_Ron


Facundo Batista

unread,
May 15, 2005, 11:28:04 PM5/15/05
to r...@ronadam.com, pytho...@python.org
On 5/15/05, Ron Adam <r...@ronadam.com> wrote:


> >>> x
> 3200001.6000000001
>
> Here the error has been kept to a minimum. In most cases, it isn't a
> problem, but it is something to be aware of. It does matter in banking
> and I beleive there are standard ways of dealing with it.

Yes, use Decimal:

http://docs.python.org/lib/module-decimal.html

Remember that you can also use it in Python 2.3.x:

http://www.taniquetil.com.ar/facundo/bdvfiles/get_decimal.html

. Facundo

Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/

Steffen Glückselig

unread,
May 16, 2005, 2:31:25 AM5/16/05
to
So for using the python-interpreter as a simple calculator using
'print' seems to be the simplest and still exact way...


Thanks for clarification
Steffen

0 new messages