Should % ever "overflow"?

Shachar Shemesh via Digitalmars-d digitalmars-d at puremagic.com
Sat Jun 25 22:28:53 PDT 2016


What deadalnix (how did you choose a nickname that is more difficult to 
write than your given name anyway?) said was that the definition of % 
only makes sense if, for every n and every m:
(n/m)+(n%m)=n

What this means is that, if n/m is rounded up for negative numbers, n%m 
must be negative.

Since n/m and n%m are, usually, implemented by the CPU's hardware, 
performance dictates that we do whatever it is that the CPU is doing. On 
most modern CPUs, n/m rounds up for negative results, and n%m is negative.

So, we can do it your way. This would mean:
1. Losing performance for every division and modulus that *might* be 
negative
and
2. Being different than other programming languages out there

or we can do what we're doing.

Shachar


More information about the Digitalmars-d mailing list