This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Floor division operator and floats
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Floor division is not the same as the floor of division
View: 27463
Assigned To: Nosy List: Kostis Gourgoulias, mark.dickinson, ned.deily, ronaldoussoren
Priority: normal Keywords:

Created on 2020-02-20 16:25 by Kostis Gourgoulias, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg362330 - (view) Author: Kostis Gourgoulias (Kostis Gourgoulias) Date: 2020-02-20 16:25
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?
msg362332 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-02-20 17:07
> 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 `log(n) / log(2)` versus `log2(n)`, or `round(x, 2)` versus `round(100.0*x)/100.0`.

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.
msg362334 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-02-20 17:10
Ah, found the duplicate (or at least one of them): https://bugs.python.org/issue27463
msg362335 - (view) Author: Kostis Gourgoulias (Kostis Gourgoulias) Date: 2020-02-20 17:12
Aha, I see! Thanks and apologies for missing the duplicate.
msg362336 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2020-02-20 17:13
> Thanks and apologies for missing the duplicate.

Not a problem: much better to have a potential bug reported twice than not at all.
History
Date User Action Args
2022-04-11 14:59:26adminsetgithub: 83884
2020-02-20 17:13:04mark.dickinsonsetmessages: + msg362336
2020-02-20 17:12:01Kostis Gourgouliassetmessages: + msg362335
2020-02-20 17:10:10mark.dickinsonsetstatus: open -> closed
superseder: Floor division is not the same as the floor of division
messages: + msg362334

components: + Interpreter Core, - macOS
resolution: duplicate
stage: resolved
2020-02-20 17:07:59mark.dickinsonsetnosy: + mark.dickinson
messages: + msg362332
2020-02-20 16:25:56Kostis Gourgouliascreate