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
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
This is as correct as your computer's FPU can made it :)
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
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
>>> 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
> 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
> >>> 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/
Thanks for clarification
Steffen