Modulo that 'wraps' the number?

Steven Schveighoffer schveiguy at gmail.com
Mon Jan 21 18:39:27 UTC 2019


On 1/21/19 10:54 AM, Matheus wrote:
> On Monday, 21 January 2019 at 15:01:27 UTC, Steven Schveighoffer wrote:
>>
>> Probably, this optimizes into better code, but maybe the optimizer 
>> already does this with the expression above:
>>
>> auto tmp = n % 3;
>> if(tmp < 0)
>>    tmp += 3;
>>
>> It's just not a nice single expression.
>>
> 
> I don't think you can do this, imagine this case:
> 
> auto tmp = -1 % -3; // Note divisor in negative too.
> 
> tmp will be "-1" which already matches the Python way, so you can't add 
> divisor anymore.

If the divisor is unknown, I hadn't considered the case. I was only 
considering the case where the divisor is a constant.

In that case, one can probably determine if the divisor is less than 0 
before starting.

Not a Python user, just hoping to help answer questions :)

-Steve


More information about the Digitalmars-d-learn mailing list