New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Floor division operator and floats #83884
Comments
This was brought to my attention by a colleague, Albert B. When considering the floor division // operator, 1//0.01 should return 100.0, but instead returns 99.0. My understanding is that this is because 0.01 is represented by Decimal('0.01000000000000000020816681711721685132943093776702880859375') which is greater than 0.01. math.floor(1/0.01) correctly outputs 100. Shouldn't the two approaches provide the same answer? |
In a word, no. :-) math.floor(1/0.01) involves *two* operations, and there's an intermediate rounding step which happens to round the true mathematical result of 1/0.01 up to 100.0. Taking the floor of that then (of course) gives 100.0. 1//0.01 is the single-operation equivalent, that doesn't include an intermediate round. There are lots of other cases where a combination of two or more operations is mathematically equivalent to a single operation, but produces a different result due to an intermediate round; for example, things like There's unfortunately no way to square the circle here that doesn't cause surprises in at least some corner cases. I'm almost sure this issue is a duplicate, but I haven't found a good target for that duplicate yet. I'll continue searching. |
Ah, found the duplicate (or at least one of them): https://bugs.python.org/issue27463 |
Aha, I see! Thanks and apologies for missing the duplicate. |
Not a problem: much better to have a potential bug reported twice than not at all. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: