Precision?

  • Thread starter =?iso-8859-1?q?Steffen_Gl=FCckselig?=
  • Start date
?

=?iso-8859-1?q?Steffen_Gl=FCckselig?=

Hello,

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

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



best regards
Steffen
 
S

Steven Bethard

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

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
 
T

Terry Reedy

Steffen Glückselig said:
8.5999999999999996

Ehm, how could I get the intuitively 'correct' result of - say - 8.6?
'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
 
R

Ron Adam

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

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
 
P

Peter Dembinski

Steffen Glückselig said:
Hello,

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

8.5999999999999996

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

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

Jeff Epler

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.
when you write '4.6', you get a binary floating-point number which is
not equal to the decimal number 4.6.False

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFCh720Jd01MZaTXX0RAoZZAJ0Q3qngITn7Vs/7T6li2QZeGAbsYgCeJ3SJ
aLW5MCjK7lRPeC87PKRLEsA=
=eITH
-----END PGP SIGNATURE-----
 
R

Ron Adam

Ron said:
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)
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.
.... x += 1.6
....1600001.6000213262

Eventually this could be significant.
.... x = round(x+1.6,1)
....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
 
F

Facundo Batista

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/
 
?

=?iso-8859-1?q?Steffen_Gl=FCckselig?=

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,239
Messages
2,571,200
Members
47,838
Latest member
elibuskamoSeAve

Latest Threads

Top