## digitalmars.D - Could we have mod in std.math?

- Caligo <iteronvexor gmail.com> Dec 20 2011
- Don <nospam nospam.com> Dec 20 2011
- Stewart Gordon <smjg_1998 yahoo.com> Dec 21 2011
- bearophile <bearophileHUGS lycos.com> Dec 24 2011
- Don <nospam nospam.com> Dec 25 2011
- Walter Bright <newshound2 digitalmars.com> Dec 25 2011

--14dae9ccd4ecf67d0004b4940264 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 1. The % operator, just like in C/C++, calculates the remainder, but it doesn't handle negative numbers properly. It's not a mod operator, even though sometimes it's called that. assert(-6 % 20 =3D=3D -6); assert( 6 % -20 =3D=3D 6); assert(-6 % -20 =3D=3D -6); I use my own mod function whenever I need to handle negative numbers. It looks like this: pure T mod(T)(T n, T d) if(isIntegral!(T)){ T r =3D n % d; return sgn(r) =3D=3D -(sgn(d)) ? r + d : r; } assert(mod(-6, 20) =3D=3D 14); assert(mod( 6, -20) =3D=3D -14); assert(mod(-6, -20) =3D=3D -6); I'm hoping to see something like the above mod function in Phobos someday. And perhapse a 'rem' or 'remainder' function that's a wrapper for the % operator, just to stay consistent. 2. With the above, the math.fmod then would have to be renamed to 'frem' because, just like the % for integrals, it doesn't handle negative numbers properly only calculates the remainder. assert(fmod(-6, 20) =3D=3D -6); assert(fmod( 6, -20) =3D=3D 6); assert(fmod(-6, -20) =3D=3D -6); I'm not so sure why we have 're=ADmain=ADder` and `remquo` in std.math when there is 'fmod`, though. What do you guys think? --14dae9ccd4ecf67d0004b4940264 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable 1.<br>The % operator, just like in C/C++, calculates the remainder, but it = doesn't handle negative numbers properly.=A0 It's not a mod operato= r, even though sometimes it's called that.<br><br>=A0 assert(-6 %=A0 20= =3D=3D -6);<br> =A0 assert( 6 % -20 =3D=3D=A0 6);<br>=A0 assert(-6 % -20 =3D=3D -6);<br><br=I use my own mod function whenever I need to handle negative numbers.=A0 I=

=A0 T r =3D n % d;<br> =A0 return sgn(r) =3D=3D -(sgn(d)) ? r + d : r;<br>}<br><br>=A0 assert(mod(= -6,=A0 20) =3D=3D=A0 14);<br>=A0 assert(mod( 6, -20) =3D=3D -14);<br>=A0 as= sert(mod(-6, -20) =3D=3D -6);<br><br>I'm hoping to see something like t= he above mod function in Phobos someday.=A0 And perhapse a 'rem' or= 'remainder' function that's a wrapper for the % operator, just= to stay consistent.<br> <br>2.<br>With the above, the math.fmod then would have to be renamed to &#= 39;frem' because, just like the % for integrals, it doesn't handle = negative numbers properly only calculates the remainder.<br><br>=A0 assert(= fmod(-6,=A0 20) =3D=3D -6);<br> =A0 assert(fmod( 6, -20) =3D=3D=A0 6);<br>=A0 assert(fmod(-6, -20) =3D=3D -= 6);<br><br>I'm not so sure why we have '<span class=3D"ddoc_psymbol= ">re=ADmain=ADder` and `</span><a name=3D"remquo"></a><span class=3D"ddoc_p= symbol">remquo` in std.math when there is 'fmod`, though.<br> <br>What do you guys think?<br></span> --14dae9ccd4ecf67d0004b4940264--

Dec 20 2011

On 21.12.2011 07:08, Caligo wrote:1. The % operator, just like in C/C++, calculates the remainder, but it doesn't handle negative numbers properly. It's not a mod operator, even though sometimes it's called that. assert(-6 % 20 == -6); assert( 6 % -20 == 6); assert(-6 % -20 == -6); I use my own mod function whenever I need to handle negative numbers. It looks like this: pure T mod(T)(T n, T d) if(isIntegral!(T)){ T r = n % d; return sgn(r) == -(sgn(d)) ? r + d : r; } assert(mod(-6, 20) == 14); assert(mod( 6, -20) == -14); assert(mod(-6, -20) == -6); I'm hoping to see something like the above mod function in Phobos someday. And perhapse a 'rem' or 'remainder' function that's a wrapper for the % operator, just to stay consistent. 2. With the above, the math.fmod then would have to be renamed to 'frem' because, just like the % for integrals, it doesn't handle negative numbers properly only calculates the remainder. assert(fmod(-6, 20) == -6); assert(fmod( 6, -20) == 6); assert(fmod(-6, -20) == -6); I'm not so sure why we have 'remainder` and `remquo` in std.math when there is 'fmod`, though. What do you guys think?

Those names and semantics all come from C. We can't change them without an extremely good reason. Note that things are a bit more complicated than you describe -- there is also the IEEE754 remainder function, which is really wierd. A mod() function might not be a bad idea.

Dec 20 2011

On 21/12/2011 06:08, Caligo wrote: <snip>assert(mod(-6, 20) == 14); assert(mod( 6, -20) == -14); assert(mod(-6, -20) == -6); I'm hoping to see something like the above mod function in Phobos someday. And perhapse a 'rem' or 'remainder' function that's a wrapper for the % operator, just to stay consistent.

And a floor-divide along the same lines. See also http://www.digitalmars.com/d/archives/digitalmars/D/13125.html Stewart.

Dec 21 2011

Caligo:I'm hoping to see something like the above mod function in Phobos someday.

I'd like that, for both int/long/BigInts. And I'd like some functions for BigInts: - divmod: returns both % and div and %, but it's more efficient if you need both values; - gcd - pow: with a third optional argument (like in Python pow built-in) to quickly compute (x ^^ y) % z. Bye, bearophile

Dec 24 2011

On 24.12.2011 22:12, bearophile wrote:Caligo:I'm hoping to see something like the above mod function in Phobos someday.

I'd like that, for both int/long/BigInts. And I'd like some functions for BigInts: - divmod: returns both % and div and %, but it's more efficient if you need both values; - gcd

These should both exist for int and long as well.- pow: with a third optional argument (like in Python pow built-in) to quickly compute (x ^^ y) % z. Bye, bearophile

Dec 25 2011

On 12/25/2011 10:57 PM, Don wrote:On 24.12.2011 22:12, bearophile wrote:Caligo:I'm hoping to see something like the above mod function in Phobos someday.

I'd like that, for both int/long/BigInts. And I'd like some functions for BigInts: - divmod: returns both % and div and %, but it's more efficient if you need both values; - gcd

These should both exist for int and long as well.

Consider the following program: int div(int a, int b) { auto x = a / b; auto y = a % b; return x + y; } Compile with: dmd -c test -O Disassemble with obj2asm: _D4test3divFiiZi comdat push EAX mov EAX,8[ESP] cdq idiv [ESP] add EAX,EDX pop ECX ret 4

Dec 25 2011