Modulo Bug?

Thiez thiezz at gmail.com
Sun Aug 12 07:20:07 PDT 2012


On Sunday, 12 August 2012 at 13:11:36 UTC, David wrote:
> My implementation of py_mod:
>
> int py_mod(int a, int b) {
>     return cast(uint)a % b;
> }
>
> So a simple cast from signed to unsigned does exactly what I 
> need (Prf_Jakob showed me that in IRC)

It seems to work when b is a power of 2, but not for other 
numbers. Example: -3 % 7 = 4, but cast(uint)-3 = 4294967293, and 
4294967293 % 7 = 1.

> How much does a cast cost? I mean is that even an extra asm 
> instruction?

Casting ints to uints and back is free because the bits can 
remain the same (only the interpretation changes).


More information about the Digitalmars-d mailing list