From: Timothy J. H. <ti...@cs...> - 2004-06-17 22:09:28
|
On Jun 17, 2004, at 8:46 AM, Jonathan A Rees wrote: >> From Scheme 48 (for heaven's sake don't even think about IP): > > (define (modulo x y) > (let ((r (remainder x y))) > (if (> y 0) > (if (< r 0) > (+ r y) > r) > (if (> r 0) > (+ r y) > r)))) Thanks!! > > Easily translated into Java. I used something similar in Java ... case MODULO: c=a1%b1; c= (((c==0)||((c>0)==(b1>0)))?c:c+b1); break; Your code says that if y and r have opposite signs then add y to r, otherwise leave r alone. Mine says the same, but tests first to see if r==0 which short circuits the tests. Your's is easier to understand as well! I'll try to see which is faster.... ---Tim--- > > As I remember it took several iterations to get this right. > > -Jonathan > > > ------------------------------------------------------- > This SF.Net email is sponsored by The 2004 JavaOne(SM) Conference > Learn from the experts at JavaOne(SM), Sun's Worldwide Java Developer > Conference, June 28 - July 1 at the Moscone Center in San Francisco, CA > REGISTER AND SAVE! http://java.sun.com/javaone/sf Priority Code > NWMGYKND > _______________________________________________ > Jscheme-user mailing list > Jsc...@li... > https://lists.sourceforge.net/lists/listinfo/jscheme-user |