Skip to content

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

@xmh0511

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()

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