Skip to content

behaviour of the modulo operator #129297

Description

@SerpentByte

Feature or enhancement

Proposal:

The behaviour of the modulo operator should be made consistent across Python and C/C++.

3%2 # this is 1
-3%2 # this can be represented as -1 or 1 depending of the divisor.
# -3%2 is -1 in C/C++.

Has this already been discussed elsewhere?

I don't think so.

Activity

  1. sobolevn commented on Jan 25, 2025

    @sobolevn
    Member

    Looks like a duplicate of #75204

  2. ronaldoussoren commented on Jan 25, 2025

    @ronaldoussoren
    Contributor

    The behaviour of the operator is intentional, see the answers in this stack overflow question for a good explanation of the rationale for this choice.

  3. terryjreedy commented on Jan 26, 2025

    @terryjreedy
    Member

    Issue #75204 is not really a duplicate; it is an open proposal to revise the int division programming FAQ, which involves the choice of modulus sign determination. I just opened PR #129313 for it. It needs coredev review.

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions