exp

(PHP 3, PHP 4, PHP 5)

exp -- 计算 e(自然对数的底)的指数

说明

float exp ( float arg )

返回 earg 次方值。

注: 用 'e' 作为自然对数的底 2.718282.

例子 1. exp() 范例

<?php
echo exp(12) . "\n";
echo
exp(5.7);
?>

上例将输出:

1.6275E+005
298.87

参见 log()pow()


add a note add a note User Contributed Notes
boards at gmail dot com
27-Apr-2006 01:18
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;
}
?>
info at teddycaddy dot com
16-Sep-2004 09:55
This only returns the first 51 digits after the decimal point.