Search the web
Sign In
New User? Sign Up
wcalc · Wcalc Users
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Real people. Real stories. See how Yahoo! Groups impacts members worldwide.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
mod doesn't work wery well   Message List  
Reply | Forward Message #55 of 114 |
Re: [wcalc] mod doesn't work wery well

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




Thu Jun 29, 2006 11:03 pm

ingo_swt
Offline Offline
Send Email Send Email

Forward
Message #55 of 114 |
Expand Messages Author Sort by Date

I wanted the remainder of a division with big numbers, this is what I've done: $ time wcalc "2^56 - floor(2^56 / 57)*57" = 4 real 0m0.004s user 0m0.001s ...
Daniele
scrows@...
Send Email
Jun 28, 2006
10:15 am

... Heh, right. Counting up to 2^56 in 57-steps might take a while. ;-) ... Sure. Patch attached. Cheers, Ingo...
Ingo van Lil
ingo_swt
Offline Send Email
Jun 28, 2006
6:30 pm

... Hmm. What is currently in CVS is this: mpfr_set_ui(temp, 0, GMP_RNDN); mpfr_div(temp, first, second, GMP_RNDN); mpfr_rint(temp, temp, GMP_RNDZ); ...
Kyle Wheeler
m3m0ryh0l3
Offline Send Email
Jun 29, 2006
4:18 am

... What's the first line good for? ... -20 is not an option. The question is: When performing the integer division of -340 and 60, do you round the result by...
Ingo van Lil
ingo_swt
Offline Send Email
Jun 29, 2006
8:03 am

... Hmm, not much, come to think of it. ... Hmm, well, I tend to like not being restrictive, and I also like maintaining the existing behavior. In Wcalc 1.x,...
Kyle Wheeler
m3m0ryh0l3
Offline Send Email
Jun 29, 2006
9:35 pm

... 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...
Ingo van Lil
ingo_swt
Offline Send Email
Jun 29, 2006
11:05 pm

... Well, I'm not saying I'm in love with ADA, I'm just suggesting we preserve the existing behavior for % and do a better/different one with a new operator...
Kyle Wheeler
m3m0ryh0l3
Offline Send Email
Jun 30, 2006
1:23 am

... Well, rounding output towards zero would pick floor() or ceil() depending on the sign of output. I'm checking the sign of the first operand instead. So, if...
Ingo van Lil
ingo_swt
Offline Send Email
Jun 30, 2006
8:35 am

Perhaps I found another bug in the mod operator, I don't know if the result change with the last modifies to the way the mod works, so .. For `m % n' where `m'...
Daniele
scrows@...
Send Email
Jul 5, 2006
3:35 pm

... Nope, that's wrong. Looks like I need to get this fixed version out asap. ~Kyle -- It is easier to be critical than to be correct. -- Benjamin Disraeli...
Kyle Wheeler
m3m0ryh0l3
Offline Send Email
Jul 6, 2006
7:01 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help