0

In PYTHON(3.4.3): How come in the code below x is 3.5... and y is 100. Shouldn't x be 0.

weight = 356
weight2 = 3.56
x = weight%weight2
y = weight/weight2
print(x)
print(y)

But in this code x does = 0 as expected:

weight = 35600
weight2 = 356
x = weight%weight2
y = weight/weight2
print(x)
print(y)

If you have any explanation please answer and give a workaround if you have one!! :)

1 Answers1

1

It is because float arithmetic is not exact. the decimal module show the 'real' value of what you think to be 3.56 :

In[202]  decimal.Decimal(3.56)
Out[202]: Decimal('3.560000000000000053290705182007513940334320068359375')

So a lot of rounding problems occurs.

B. M.
  • 18,243
  • 2
  • 35
  • 54