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

No modulo operator? #13909

Closed
atg opened this issue May 3, 2014 · 1 comment
Closed

No modulo operator? #13909

atg opened this issue May 3, 2014 · 1 comment

Comments

@atg
Copy link

atg commented May 3, 2014

It's rather disappointing to me that Rust has no working modulo operator. I had hoped that, like Ruby and Python, % would give correct results on negative numbers (e.g. -4 is 2 (mod 3)). Alas it has the same broken behaviour as C.

I see % has been retconned as a "remainder" operator, but realistically, most people expect correctness rather than historical artefacts. Apparently, I'm not the only one bothered by it.

If it were me, I would change % to match Ruby and Python, and introduce a "rem" keyword to fulfil the current behaviour. This is a neat solution because it doesn't give up any performance (for all those people doing modulo division in a tight loop, I guess?), but it doesn't give the middle finger to mathematicians either.

2¢.

@thestinger
Copy link
Contributor

A change to the language like this should be proposed through the RFC process.

It's intended to be the remainder operator because it maps to the functionality of hardware, just like in C. It maps directly to the urem and srem instructions in LLVM, and those have direct equivalents on the CPU. The corresponding trait is ops::Rem and the method is called rem, so there's a full awareness of the operator's semantics. The division operator is also explicitly defined as a truncating integer division like C, not one taking the floor like Python.

but it doesn't give the middle finger to mathematicians either.

It's not like % is the modulo operator in mathematical notation. AFAIK it's an ALGOL/C-ism and Rust is consistent with the origin of the operator. Mathematica and Matlab don't use the % operator for this either.

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

2 participants