note 65196 added to function.exp

From: Date: Wed, 26 Apr 2006 17:18:14 +0000
Subject: note 65196 added to function.exp
Groups: php.notes 
Request: Send a blank email to php-notes+get-109774@lists.php.net to get a copy of this message
Note regarding the mathematical function exp(x): To continue accuracy of the exponential function to an infinite amount of decimal places, one would use the power series definition for exp(x). (in LaTeX form:) e^x = \sum_{n=0}^{\infty} \frac{x^n}{n!} So, to do that in PHP (using BC math): <?php // arbitrary precision function (x^n)/(n)! function bcpowfact($x, $n) { if (bccomp($n, '0') == 0) return '1.0'; if (bccomp($n, '1') == 1) return $x; $a = $x; // nth step: a *= x / 1 $i = $n; while (bccomp($i, '1') == 1) { // ith step: a *= x / i $a = bcmul($a, bcdiv($x, $i)); $i = bcsub($i, '1'); // bc idiom for $i-- } return $a; } // arbitrary precision exp() function function bcexp($x, $decimal_places) { $sum = $prev_sum = '0.0'; $error = bcdiv(bcpow('10', '-'.$decimal_places), 10); // 0.1*10^-k $n = '0'; do { $prev_sum = $sum; $sum = bcadd($sum, bcpowfact($x, $n)); } while (bccomp(bcsub($sum, $prev_sum), $error) == 1); return $sum; } ?> ---- Server IP: 64.71.164.2 Probable Submitter: 71.228.25.94 ---- 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.exp.php Edit -- http://master.php.net/note/edit/65196 Del: integrated -- http://master.php.net/note/delete/65196/integrated Del: useless -- http://master.php.net/note/delete/65196/useless Del: bad code -- http://master.php.net/note/delete/65196/bad+code Del: spam -- http://master.php.net/note/delete/65196/spam Del: non-english -- http://master.php.net/note/delete/65196/non-english Del: in docs -- http://master.php.net/note/delete/65196/in+docs Del: other reasons-- http://master.php.net/note/delete/65196 Reject -- http://master.php.net/note/reject/65196 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#109774) next »