note 65589 deleted from function.bcpow by derick

From: Date: Fri, 11 May 2007 14:55:45 +0000
Subject: note 65589 deleted from function.bcpow by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-126411@lists.php.net to get a copy of this message
Note Submitter: Robert Lozyniak ---- I hate every living thing that ever lived, except for Ms. A.Sh.O. and maybe also Ms. C.D.F. I hate the person who posted the silly joke below my post, and I hate myself immeasurably more. That having been said: This function takes arbitrary integers as exponents: <?php function bcpowislame($base,$exp,$scale) { // I take arbitrary integers as exponents // Remember, I take ONLY integers as exponents! // bcpow is lame; I am a substitute $wexp=bcadd($exp,'0',0); if (bccomp($exp,$wexp)) return false; $wbase=$base; $wscale=$scale+strlen($wexp)+3; // the 3 is arbitrary if ($wexp{0}=='-') {$wbase=bcdiv('1',$wbase,$wscale); $wexp=bcsub('0',$wexp,0);} if ($wexp=='0') return bcadd('1','0',$scale); $scr='1'; $scrm=$wbase; $loop=true; while ($loop) { if (bcmod($wexp,'2')=='1') $scr=bcmul($scr,$scrm,$wscale); if (bccomp($wexp,'1')==0) $loop=false; else {$scrm=bcmul($scrm,$scrm,$wscale); $wexp=bcdiv($wexp,'2',0);}} return bcadd('0',$scr,$scale); } ?> I also hate my interpreter for not letting this work on negative exponents. This seems to work great on positive exponents though.

« previous php.notes (#126411) next »