Skip to content

(-1).modulo(-2) gets the wrong result  #3

Description

@xmh0511

According to the implementation of trait method modulo

  let result = self % rhs.clone();
  if result < Self::Output::default() {
      result + rhs
  } 

The modulo result will be wrong if result and rhs both are negative numbers. (-1).modulo(-2) gives the wrong result -3, it should be 1 anyway.

The fix may be result + | rhs |? Namely, result + rhs.abs()

Activity

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