Kyle Wheeler <kyle-wcalc-yahoo@...> schrieb:
> Hmm, well, I tend to like not being restrictive, and I also like
> maintaining the existing behavior. In Wcalc 1.x, this was implemented
> as the C % operator, so I'd like to keep it that way. On the other
> hand, I can see a valid need for something more interesting, like the
> "rem" operator in Ada. Perhaps I should add your method as the
> implementation of that operator...?
Actually, if I'm reading the ADA reference manual correctly, the REM
operator does exactly what your algorithm does (A REM B keeps the sign
of A) while the MOD operator does the same as mine (A MOD B keeps the
sign of B; I was mistaken when I claimed my algorithm would always pick
the positive result).
There's one drawback to the C behaviour of % (and the ADA REM
operator): The sign of the divisor isn't regarded at all, i.e. A % B ==
A % -B. We could use the alternative set of results in case B was
negative, and the users could still easily reproduce the current
behaviour using A % abs(B).
Proposed algorithm:
mpfr_div(output, first, second, GMP_RNDN);
if (MPFR_SIGN(first) >= 0)
mpfr_floor(output, output);
else
mpfr_ceil(output, output);
mpfr_mul(output, output, second, GMP_RNDN);
mpfr_sub(output, first, output, GMP_RNDN);
Cheers,
Ingo