www.digitalmars.com         C & C++   DMDScript  

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

reply Caligo <iteronvexor gmail.com> writes:
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?
Dec 20 2011
next sibling parent 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
prev sibling next sibling parent 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.
<snip> And a floor-divide along the same lines. See also http://www.digitalmars.com/d/archives/digitalmars/D/13125.html Stewart.
Dec 21 2011
prev sibling parent reply 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
parent reply 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
parent 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] add EAX,EDX pop ECX ret 4
Dec 25 2011