PHP Doku:: Exponentialfunktion - function.exp.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzMathematische ErweiterungenMathematische FunktionenMathematische Funktionenexp

Ein Service von Reinhard Neidl - Webprogrammierung.

Mathematische Funktionen

<<deg2rad

expm1>>

exp

(PHP 4, PHP 5)

expExponentialfunktion

Beschreibung

float exp ( float $arg )

Berechnet e hoch arg.

Hinweis:

'e' ist die Basis des natürlichen Logarithmus (ca. 2,718282).

Parameter-Liste

arg

Der zu verarbeitende Exponent

Rückgabewerte

'e' hoch arg

Beispiele

Beispiel #1 exp() Beispiel

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

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

1.6275E+005
298.87

Siehe auch


5 BenutzerBeiträge:
- Beiträge aktualisieren...
zooly at globmi dot com
14.12.2010 16:12
PHP does not have the following math function in any extensions:

frexp() - Extract Mantissa and Exponent of the Floating-Point Value

I've digged many C source codes, and found the simplest implementation as follows:

<?php

function frexp ( $float ) {

 
$exponent = ( floor(log($float, 2)) + 1 );
 
$mantissa = ( $float * pow(2, -$exponent) );

  return(
    array(
$mantissa, $exponent)
  );

}

print_r(frexp(0.0345));
print_r(frexp(21.539));

?>

Array
(
    [0] => 0.552
    [1] => -4
)
Array
(
    [0] => 0.67309375
    [1] => 5
)

I have compared the results using a lot of floats against C's frexp function - they are the same.

Note that C and PHP uses different float precisions, for example "4619.3" gives:

C: 0.56387939453125, 13
PHP: 0.563879394531, 13

/Assuming default configurations./
Nitrogen
24.09.2009 2:54
Just a note about using the submitted codes below..
Their functions have an optional $precision parameter; however, it's not being used properly..

BCMath functions by default do not use decimal precision unless specified by BCScale($precision); or using the extra parameter in the used BC functions.

For example, a blank PHP file with their code.. executing BCExp('5.7'); returns "47" instead of the correct answer of "298.86740096706..."

So for optimum accuracy, I'd suggest setting BCScale to a healthy length before running their codes.
konrad
25.01.2007 0:13
working version (checked) of below code is

<?php
 
// see bccomp for this code (signed and unsigned zero!)
 
function bccomp_zero($amount) {
    return
bccomp($amount, (@$amount{0}=="-"?'-':'').'0.0');
  }

 
// arbitrary precision function (x^n)/(n)!
 
function bcpowfact($x, $n) {
    if (
bccomp_zero($n) == 0) return '1';
    if (
bccomp($n, '1') == 0) return $x;
   
$a = $x; // 1st 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, $digits) {
   
$sum = $prev_sum = '0.0';
   
$error = '0.'.str_repeat('0', $digits-1).'1'; // 0.1*10^-k
   
$n = '0.0';
    do {
     
$prev_sum = $sum;
     
$sum = bcadd($sum, bcpowfact($x, $n));
     
$n = bcadd($n, '1'); // bc idiom for $n++
   
} while (bccomp(bcsub($sum, $prev_sum), $error) == 1);
    return
$sum;
  }
?>
boards at gmail dot com
26.04.2006 19: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.09.2004 15:55
This only returns the first 51 digits after the decimal point.



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)