Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
xmh0511 opened this issue Jan 10, 2023 · 0 comments
Open

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

xmh0511 opened this issue Jan 10, 2023 · 0 comments

Comments

@xmh0511
Copy link

xmh0511 commented Jan 10, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant