Pyh.conf’25: a new PHP conference for the Russian-speaking community

Voting

: one plus six?
(Example: nine)

The Note You're Voting On

stonehew et g m a i l dut com
20 years ago
I hacked these taylor expansions up to make diagrams for some physics homework. I don't think you'll be wanting to do any real science with PHP... but what the hell, why not? I plan to implement either a spigot algorithm or something similar to generate pi in the near future.

<?
// arbitrary precision sin and cosine functions
// author tom boothby
// free for any use

function bcfact($n) {
$r = $n--;
while($n>1) $r=bcmul($r,$n--);
return $r;
}

function bcsin($a) {
$or= $a;
$r = bcsub($a,bcdiv(bcpow($a,3),6));
$i = 2;
while(bccomp($or,$r)) {
$or=$r;
switch($i%2) {
case 0: $r = bcadd($r,bcdiv(bcpow($a,$i*2+1),bcfact($i*2+1))); break;
default: $r = bcsub($r,bcdiv(bcpow($a,$i*2+1),bcfact($i*2+1))); break;
}
$i++;
}
return $r;
}

function bccos($a) {
$or= $a;
$r = bcsub(1,bcdiv(bcpow($a,2),2));
$i = 2;
while(bccomp($or,$r)) {
$or=$r;
switch($i%2) {
case 0: $r = bcadd($r,bcdiv(bcpow($a,$i*2),bcfact($i*2))); break;
default: $r = bcsub($r,bcdiv(bcpow($a,$i*2),bcfact($i*2))); break;
}
$i++;
}
return $r;
}

?>

<< Back to user notes page

To Top