Skip to content

Results of the Built-in Function divmod(a,b) with negative a and/or b #160958

Closed as not planned

Description

@Gabriel88-147

Bug report

Built-in Function divmod(a,b)
Negative integer or float values for dividend and/or divisor are possible. For (a,b) = (7, 3) or (a, b) = (2.5, 1.5) there are the following results:
divmod(7,3) = (2, 1) (because 2 * 3 + 1 = 7) but:
divmod(7,-3) = (-3, -2) (because -3 * (-3) - 2 = 7) and
divmod(2.5, 1.5) = (1.0, 1.0) (because 1.5 * 1.0 + 1.0 = 2.5 but:
divmod(2.5, -1.5) = (-2.0, -0.5) (because -2.0 * (-1.5) - 0.5 = 2.5)
Similar results with divmod(-7, 3) and divmod(-2.5, 1.5), divmod(-7,-3) = (2, -1).

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Activity

  1. hugovk commented on Oct 30, 2026

    @hugovk
    Member

    As documented:

    if a % b is non-zero it has the same sign as b

    https://docs.python.org/3/builtins/functions.html#divmod

  2. Gabriel88-147 commented on Oct 30, 2026

    @Gabriel88-147
    Author

    I expected that the results of a // b and a // (-b) are the same.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions