Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!bellcore!decvax!decwrl!pyramid!ut-sally!seismo!brl-smoke!gwyn From: gwyn@brl-smoke.ARPA (Doug Gwyn ) Newsgroups: net.lang.c Subject: Re: Integer division Message-ID: <923@brl-smoke.ARPA> Date: Fri, 14-Feb-86 03:54:44 EST Article-I.D.: brl-smok.923 Posted: Fri Feb 14 03:54:44 1986 Date-Received: Sun, 16-Feb-86 04:54:18 EST References: <11603@ucbvax.BERKELEY.EDU> <4917@alice.UUCP> <367@mcgill-vision.UUCP> <685@brl-smoke.ARPA> <246@hadron.UUCP> Reply-To: gwyn@brl.ARPA Organization: /usr/local/lib/news/organization Lines: 62 >... Since >the work involved was massive, I saved the operands, the quotient, >and the remainder. If subsequent calls had the same operands, I >just returned the already-computed values! This is a nice example of a "good idea". It paid off because long division was frequently accompanied by long remainder with the same operands, and the extra bookkeeping overhead per call was more than compensated by the average computational savings. Thanks, Joe. The obvious generalization can be applied in a variety of codes. A conceptually related example is: /* RnNorm -- random normal deviate with specified mean & std. dev. last edit: 86/01/04 D A Gwyn SCCS ID: @(#)rnnorm.c 1.1 Method: Polar method; see Knuth 3.4.1C(1) */ #include #include /* defines RnFlt() (uniform range) */ #include /* defines "bool", "true", "false" */ double RnNorm( mu, sigma ) /* return range (-inf,inf) */ double mu; /* desired mean */ double sigma; /* desired std. deviation */ { static bool saved = false; /* "have saved value" flag */ static double rndsave = 0.0; /* saved value iff `saved' */ double s, x, y; if ( saved ) { x = rndsave; /* already on hand */ saved = false; /* now used up */ } else { /* generate a pair of numbers */ do { x = RnFlt( -1.0, 1.0 ); y = RnFlt( -1.0, 1.0 ); s = x * x + y * y; } while ( s >= 1.0 /* 0.25 probability */ || s == 0.0 /* practically never */ ); rndsave = sqrt( -2.0 * log( s ) / s ); x *= rndsave; rndsave *= y; /* save for next call */ saved = true; } return mu + sigma * x; }