Seeing some unexpected behavior with Python tonight. Why is the following printing out 'not equal'?!
num = 1.00
num -= .95
nickel = .05
if nickel != num:
print 'not equal'
else:
print 'equal'
Seeing some unexpected behavior with Python tonight. Why is the following printing out 'not equal'?!
num = 1.00
num -= .95
nickel = .05
if nickel != num:
print 'not equal'
else:
print 'equal'
What every computer scientist should know about floating point arithmetic.
This is a common floating point problem with computers. It has to do with how the computer stores floating point numbers. I would suggest giving What Every Computer Scientist Should Know About Floating-Point Arithmetic a quick read through.
You might find the decimal module useful.
Or, alternatively:
I inferred from your naming of the
nickel
variable that you were thinking about money. Obviously, floating point is the wrong Type for that.