PDA

View Full Version : Python question



jeneverboy
March 2nd, 2012, 05:00 PM
I maybe want to give python a try. But this is remarkable:


>>> 0.26*2
0.52000000000000002


any thoughts on it?

diesch
March 2nd, 2012, 05:07 PM
That's nothing special to python but a usual problem with floating point numbers (http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems)

Aielyn
March 2nd, 2012, 06:01 PM
I maybe want to give python a try. But this is remarkable:


>>> 0.26*2
0.52000000000000002


any thoughts on it?

It is, as diesch said, due to the way that floating point numbers are handled.

For more details, see this:

http://docs.python.org/tutorial/floatingpoint.html

Also see this for the way to get around the problem:

http://docs.python.org/library/decimal.html

jeneverboy
March 5th, 2012, 01:20 PM
thanks, i guess i should have known that. ;)