Could we have mod in std.math?

Walter Bright newshound2 at digitalmars.com
Sun Dec 25 23:28:45 PST 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


More information about the Digitalmars-d mailing list