Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!princeton!caip!brl-adm!brl-smoke!gwyn From: gwyn@brl-smoke.ARPA (Doug Gwyn ) Newsgroups: net.lang.c Subject: non-built-in exponentiation (Re: What should be added to C) Message-ID: <1371@brl-smoke.ARPA> Date: Mon, 16-Jun-86 02:06:21 EDT Article-I.D.: brl-smok.1371 Posted: Mon Jun 16 02:06:21 1986 Date-Received: Sat, 21-Jun-86 07:44:02 EDT References: <1356@brl-smoke.ARPA> Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 43 Summary: Here is the exponentiation function I promised. /* LPow -- long exponentiation (no overflow detection) last edit: 86/06/16 D A Gwyn SCCS ID: @(#)lpow.c 1.1 */ long LPow( base, exponent ) /* returns base^exponent */ register long base; register long exponent; { register long result; /* result accumulator */ /* handle simple special cases separately: */ if ( base == 0 ) return 0; /* exp. < 0 should be EDOM */ else if ( base == 1 ) return 1; else if ( base == -1 ) return exponent % 2 == 0 ? 1 : -1; else if ( exponent < 0 ) return 0; /* general case with exponent >= 0: */ result = 1; for ( ; ; ) /* LOOP INVARIANT: result*base^exponent */ { if ( exponent % 2 != 0 ) result *= base; if ( (exponent /= 2) == 0 ) break; /* result now stable */ base *= base; } return result; }