Modulo that 'wraps' the number?

Matheus stop at spam.com
Mon Jan 21 21:39:37 UTC 2019


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.


More information about the Digitalmars-d-learn mailing list