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

Caligo <iteronvexor gmail.com> writes:
```--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&#39;t handle negative numbers properly.=A0 It&#39;s not a mod operato=
r, even though sometimes it&#39;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&#39;m hoping to see something like t=
he above mod function in Phobos someday.=A0 And perhapse a &#39;rem&#39; or=
&#39;remainder&#39; function that&#39;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&#39; because, just like the % for integrals, it doesn&#39;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&#39;m not so sure why we have &#39;<span class=3D"ddoc_psymbol=
symbol">remquo` in std.math when there is &#39;fmod`, though.<br>
<br>What do you guys think?<br></span>

--14dae9ccd4ecf67d0004b4940264--
```
Dec 20 2011
Don <nospam nospam.com> writes:
```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 're­main­der` 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
Stewart Gordon <smjg_1998 yahoo.com> writes:
```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.

http://www.digitalmars.com/d/archives/digitalmars/D/13125.html

Stewart.
```
Dec 21 2011
bearophile <bearophileHUGS lycos.com> writes:
```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
Don <nospam nospam.com> writes:
```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
Walter Bright <newshound2 digitalmars.com> writes:
```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]