Skip to content
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

Closed
KostisGourgoulias mannequin opened this issue Feb 20, 2020 · 5 comments
Closed

Floor division operator and floats #83884

KostisGourgoulias mannequin opened this issue Feb 20, 2020 · 5 comments
Labels
3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@KostisGourgoulias
Copy link
Mannequin

KostisGourgoulias mannequin commented Feb 20, 2020

BPO 39703
Nosy @ronaldoussoren, @mdickinson, @ned-deily
Superseder
  • bpo-27463: Floor division is not the same as the floor of division
  • 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:

    assignee = None
    closed_at = <Date 2020-02-20.17:10:10.623>
    created_at = <Date 2020-02-20.16:25:56.001>
    labels = ['interpreter-core', 'type-bug', '3.7']
    title = 'Floor division operator and floats'
    updated_at = <Date 2020-02-20.17:13:04.169>
    user = 'https://bugs.python.org/KostisGourgoulias'

    bugs.python.org fields:

    activity = <Date 2020-02-20.17:13:04.169>
    actor = 'mark.dickinson'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-02-20.17:10:10.623>
    closer = 'mark.dickinson'
    components = ['Interpreter Core']
    creation = <Date 2020-02-20.16:25:56.001>
    creator = 'Kostis Gourgoulias'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 39703
    keywords = []
    message_count = 5.0
    messages = ['362330', '362332', '362334', '362335', '362336']
    nosy_count = 4.0
    nosy_names = ['ronaldoussoren', 'mark.dickinson', 'ned.deily', 'Kostis Gourgoulias']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '27463'
    type = 'behavior'
    url = 'https://bugs.python.org/issue39703'
    versions = ['Python 3.7']

    @KostisGourgoulias
    Copy link
    Mannequin Author

    KostisGourgoulias mannequin commented Feb 20, 2020

    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?

    @KostisGourgoulias KostisGourgoulias mannequin added OS-mac 3.7 (EOL) end of life type-bug An unexpected behavior, bug, or error labels Feb 20, 2020
    @mdickinson
    Copy link
    Member

    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.

    @mdickinson
    Copy link
    Member

    Ah, found the duplicate (or at least one of them): https://bugs.python.org/issue27463

    @mdickinson mdickinson added interpreter-core (Objects, Python, Grammar, and Parser dirs) and removed OS-mac labels Feb 20, 2020
    @mdickinson mdickinson added interpreter-core (Objects, Python, Grammar, and Parser dirs) and removed OS-mac labels Feb 20, 2020
    @KostisGourgoulias
    Copy link
    Mannequin Author

    KostisGourgoulias mannequin commented Feb 20, 2020

    Aha, I see! Thanks and apologies for missing the duplicate.

    @mdickinson
    Copy link
    Member

    Thanks and apologies for missing the duplicate.

    Not a problem: much better to have a potential bug reported twice than not at all.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant