-1

Any reason why c shouldn't equal 0.321?

>>> from math import ceil
>>> a = 123.321
>>> b = a % 60
>>> b
3.320999999999998
>>> ceil(b)
4.0
>>> c = ceil(b) - b
>>> c
0.679000000000002

Update:

For anyone wanting to know how I got the value I was looking for:

>>> c = b - floor(b)
Mark L
  • 12,405
  • 4
  • 28
  • 41
  • 3
    simply clicking on the `floating-point` tag will lead you to the answer. Doesn't matter which language... reason is always the same. Voting to close. http://stackoverflow.com/questions/1089018/why-cant-decimal-numbers-be-represented-exactly-in-binary – spender Nov 21 '11 at 16:57
  • 4
    This is not a problem with floating point accuracy but simple math... `4 - 3.32` equals `0.68`. Try using a calculator instead. – Constantinius Nov 21 '11 at 16:58
  • Sorry, I really couldn't see where I was going wrong in the equation. – Mark L Nov 21 '11 at 17:06
  • 1
    Please READ the question before you close something as an exact duplicate. This question has nothing to do with floating point accuracy in Python. – Andrew Rasmussen Nov 21 '11 at 17:09
  • This is definitely not down-vote material, it's just an honest mistake that could happen to anybody and it is programming related. In my opinion it should have been closed but not as a duplicate, since it has nothing to do with floating point accuracy, it should be closed as too specific or similar since it won't be helpful to others. But definitely not down-vote worthy! – Trufa Nov 21 '11 at 17:22

1 Answers1

7

You're going to facepalm in about five minutes :P

ceil(b) is 4.0
b is 3.320999999999998

So ceil(b) - b should be 4.0 - 3.320999999999998. Try putting that in your calculator and you'll see why c isn't equal to 0.321.

If you're trying to get c equal to .321, you should probably do b - floor(b).

Andrew Rasmussen
  • 14,912
  • 10
  • 45
  • 81