You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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¢.
The text was updated successfully, but these errors were encountered:
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.
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¢.
The text was updated successfully, but these errors were encountered: