Oh Dear

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Jul 12 14:20:05 PDT 2009


Michiel Helvensteijn wrote:
> Michiel Helvensteijn wrote:
> 
>> Walter, surely this test shouldn't take more than a minute for you (or
>> anyone else with a D compiler installed).
> 
> Ok, ok. I'll do it. Here's the results:
> 
> ----------------------------------
> import std.stdio;
> 
> int main() {
>         writefln(8/3);
>         writefln(8/(-3));
>         writefln((-8)/3);
>         writefln((-8)/(-3));
>         writefln(8%3);
>         writefln(8%(-3));
>         writefln((-8)%3);
>         writefln((-8)%(-3));
> 
>         return 0;
> }
> ----------------------------------
> 
> outputs the following
> 
> ----------
> 2
> -2
> -2
> 2
> 2
> 2
> -2
> -2
> ----------
> 
> So DMD uses truncated division. The quotient rounds towards zero and the
> remainder has the same sign as the dividend. I've updated issue 3165 with
> this information.
> 
> Hope that helps.


Thanks, Michiel. Here's what I have in TDPL. Is it 100% in sync with you?

=======================================
The  multiplicative expressions  are multiplication  (\ccbox{a  * b}),
division  (\ccbox{a  / b}),  and  remainder  (\ccbox{a  \% b}).   They
operate on numeric types only. The  result type is same as the type of
\ccbox{true ? a : b}.

If~@b@  is  zero in  \ccbox{a  /  b} or  \ccbox{a  \%  b}, a  hardware
exception   is  thrown.    The  sign\footnote{Sign,   not  signedness,
    i.e.,~the sign of  the actual value.}  of \ccbox{a  \% b} is always
the same as  the sign of~@a at .  That is, \ccbox{a \%  b} is the closest
number to  zero of the same sign  of~@a@ that must be  added to~@a@ to
make it divisible by~@b at .  For  example, \ccbox{-5 \% 2} and \ccbox{-5
    \% -2} both yield~\cc{-1}.

\dee\ also defines modulus for  floating-point numbers in the same way
as the  IEEE~754  standard.  When  at least one  of @a@  and @b@  is a
floating-point value in \cc{a \%  b}, the result is the floating-point
number @r@ satisfying the relation \cc{a = b * n + r}, where @n@ is an
integer\footnote{``integer'' in  the mathematical sense  here} and @r@
is a positive number less than @b@'s absolute value.
=======================================


Thanks,

Andrei



More information about the Digitalmars-d mailing list