Should % ever "overflow"?

Ola Fosheim Grøstad via Digitalmars-d digitalmars-d at puremagic.com
Sun Jun 26 00:20:49 PDT 2016


On Sunday, 26 June 2016 at 05:28:53 UTC, Shachar Shemesh wrote:
> So, we can do it your way. This would mean:
> 1. Losing performance for every division and modulus that 
> *might* be negative

I don't think the performance issue is relevant. It was relevant 
when it was left implementation defined in C, but it is no longer 
the case so it was defined to be consistent with signed integer 
division in C-99. The sane thing to do from a system programming 
point of view is to have strong typing and 3 versions:

1. "x % y" only defined on unsigned integers
2. rem(x,y) for reminder from truncated division
3. mod(x,y) for reminder from floored division

Unfortunately implictly casting ints to uints is a very bad idea 
when you want a reasonably safe type-system.



More information about the Digitalmars-d mailing list