note 65589 added to function.bcpow
| From: | RobertLozyniak at osu1 dot php dot net | Date: | Thu, 04 May 2006 04:13:15 +0000 |
| Subject: | note 65589 added to function.bcpow | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-110494@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 66.163.161.117
Probable Submitter: 172.162.148.147
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.bcpow.php
Edit -- http://master.php.net/note/edit/65589
Del: integrated -- http://master.php.net/note/delete/65589/integrated
Del: useless -- http://master.php.net/note/delete/65589/useless
Del: bad code -- http://master.php.net/note/delete/65589/bad+code
Del: spam -- http://master.php.net/note/delete/65589/spam
Del: non-english -- http://master.php.net/note/delete/65589/non-english
Del: in docs -- http://master.php.net/note/delete/65589/in+docs
Del: other reasons-- http://master.php.net/note/delete/65589
Reject -- http://master.php.net/note/reject/65589
Search -- http://master.php.net/manage/user-notes.php