Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
January 20, 2019 Modulo that 'wraps' the number? | ||||
---|---|---|---|---|
| ||||
In Python -1%3 == 2 however in D -1%3 == -1 Is there a standard library function or something that gives me the Python version of modulo? |
January 20, 2019 Re: Modulo that 'wraps' the number? | ||||
---|---|---|---|---|
| ||||
Posted in reply to faissaloo | On 1/20/19 1:28 PM, faissaloo wrote:
> In Python -1%3 == 2 however in D -1%3 == -1
> Is there a standard library function or something that gives me the Python version of modulo?
Hm... (n%3+3)%3 should work.
-Steve
|
January 21, 2019 Re: Modulo that 'wraps' the number? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Sunday, 20 January 2019 at 18:51:54 UTC, Steven Schveighoffer wrote:
> On 1/20/19 1:28 PM, faissaloo wrote:
>> In Python -1%3 == 2 however in D -1%3 == -1
>> Is there a standard library function or something that gives me the Python version of modulo?
>
> Hm... (n%3+3)%3 should work.
>
> -Steve
You only need the
(n % 3) + 3
|
January 21, 2019 Re: Modulo that 'wraps' the number? | ||||
---|---|---|---|---|
| ||||
Posted in reply to NaN | On Monday, 21 January 2019 at 04:52:53 UTC, NaN wrote:
> On Sunday, 20 January 2019 at 18:51:54 UTC, Steven Schveighoffer wrote:
>> On 1/20/19 1:28 PM, faissaloo wrote:
>>> In Python -1%3 == 2 however in D -1%3 == -1
>>> Is there a standard library function or something that gives me the Python version of modulo?
>>
>> Hm... (n%3+3)%3 should work.
>>
>> -Steve
>
> You only need the
>
> (n % 3) + 3
You need both:
(-3 % 3) + 3 == 3
((-3 % 3) + 3) % 3 == 0
|
January 21, 2019 Re: Modulo that 'wraps' the number? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On 1/21/19 2:33 AM, Paul Backus wrote:
> On Monday, 21 January 2019 at 04:52:53 UTC, NaN wrote:
>> On Sunday, 20 January 2019 at 18:51:54 UTC, Steven Schveighoffer wrote:
>>> On 1/20/19 1:28 PM, faissaloo wrote:
>>>> In Python -1%3 == 2 however in D -1%3 == -1
>>>> Is there a standard library function or something that gives me the Python version of modulo?
>>>
>>> Hm... (n%3+3)%3 should work.
>>>
>>> -Steve
>>
>> You only need the
>>
>> (n % 3) + 3
>
> You need both:
>
> (-3 % 3) + 3 == 3
> ((-3 % 3) + 3) % 3 == 0
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.
-Steve
|
January 21, 2019 Re: Modulo that 'wraps' the number? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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.
>
> -Steve
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.
Matheus.
|
January 21, 2019 Re: Modulo that 'wraps' the number? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Matheus | 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
|
January 21, 2019 Re: Modulo that 'wraps' the number? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 21 January 2019 at 18:39:27 UTC, Steven Schveighoffer wrote: > Not a Python user, just hoping to help answer questions :) Yes I know in fact I'm not the OP but from what I understood from his post, he want to replicate, but I may be wrong. If it's the case, this code may help him: //DMD64 D Compiler 2.072.2 import std.stdio; import std.math; int mod(int i,int j){ if(abs(j)==abs(i)||!j||!i||abs(j)==1){return 0;}; auto m = (i%j); return (!m||sgn(i)==sgn(j))?(m):(m+j); } void main(){ int j,i; for(j=1;j<9;++j){ for(i=1;i<9;++i){ writeln(-i, " % ", +j, " = ", (-i).mod(+j)); writeln(+i, " % ", -j, " = ", (+i).mod(-j)); } } } At least the code above gave me the same results as the Python version. Matheus. |
Copyright © 1999-2021 by the D Language Foundation