Negative integer modulo/division

Steven Schveighoffer schveiguy at yahoo.com
Wed Mar 14 04:27:46 PDT 2012


On Tue, 13 Mar 2012 15:13:01 -0400, bearophile <bearophileHUGS at lycos.com>  
wrote:


> For the modulus I sometimes use:
> ((x % y) + y) % y

This might be more efficient (assuming z is your target for this):

if((z = x % y) < 0) z += y;

Though I don't know, maybe the optimizer will reduce to this.

Hard to do it in a single expression without using a function.

-Steve


More information about the Digitalmars-d mailing list