digitalmars.D  Could we have mod in std.math?
 Caligo (29/29) Dec 20 2011 1.
 Don (6/35) Dec 20 2011 Those names and semantics all come from C. We can't change them without

Stewart Gordon
(7/12)
Dec 21 2011
 bearophile (7/8) Dec 24 2011 I'd like that, for both int/long/BigInts. And I'd like some functions fo...
 Don (2/10) Dec 25 2011
 Walter Bright (19/30) Dec 25 2011 Consider the following program:
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
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.<snip> And a floordivide 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 builtin) to quickly compute (x ^^ y) % z. Bye, bearophile
Dec 24 2011
On 24.12.2011 22:12, bearophile wrote:Caligo:These should both exist for int and long as well.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 builtin) 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: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 4Caligo:These should both exist for int and long as well.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
Dec 25 2011