Skip to content

I wish it were clearer what % does on negative numbers #978

Closed
@mcclure

Description

@mcclure

In Rust reference 8.2.4, the table explains only that the % operator performs "remainder".

I write video games and I spend a lot of time performing the modulo operation on negative numbers. I get very easily confused about what, and I don't feel I clearly understand from reading 8.2.4 what Rust % would do if one or both operands were negative.

Maybe there are people out there for whom the use of "remainder" rather than "modulo", combined with "Integer division rounds towards zero", would be unambiguous, but I am not one :(

"Expected behavior": It would be nice to have at least a line of text indicating how we should interpret "remainder", in particular in the case of negative operands. It would be even better if the "examples" block with the assert_eqs included some different negative-number examples for %.

I asked about this on Twitter and it was pointed out to me the stdlib entry for numbers is much more explicit. I don't know exactly what your rules are for which things need to be included in the Reference and which should be deferred to the stdlib.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions