log

(PHP 3, PHP 4, PHP 5)

log -- Natural logarithm

Description

float log ( float arg [, float base] )

If the optional base parameter is specified, log() returns logbase arg, otherwise log() returns the natural logarithm of arg.

Замечание: The base parameter became available with PHP 4.3.0.

As always you can calculate the logarithm in base b of a number n, but using the mathematical identity: logb(n) = log(n)/log(b), where log is the neperian (or natural) logarithm.

See also exp().



log
mcmeijer at yahoo dot com
03-Feb-2005 07:22
$val = 1000000
$val2 = floor(log($val,10)) gives a value of 5 for $val2 and not 6 as expected.
$val2 = floor(log10($val)) gives the correct value.
c0x at mail dot ru
19-Sep-2004 03:08
more general version, works fine on negative, very big ($value > 1E+18) and very small ($value < 1E-18) numbers.

function expn($value, $prec = 3, $base = 1000, $prefix = '') {
   $e = array('a', 'f', 'p', 'n', 'u', 'm', '', 'k', 'M', 'G', 'T', 'P', 'E');
   $p = min(max(floor(log(abs($value), $base)), -6), 6);
   return round((float)$value / pow($base, $p), $prec) . $prefx . $e[$p + 6];
}
admin at worldtakeover dot tk
20-Jun-2004 01:06
In regards to the note about log in base 10 and the round() function. You need to use floor() instead of round() to find out the order of magnitude. That way, you don't have to worry about subtracting 0.5 or whatever.
mightye (at) mightye.org
06-Feb-2003 12:02
A minor warning:

in PHP < 4.3.0, in order to get the log base 10 of a number, you have to do:
$log10 = log($n)/log(10);

If you want a whole number (to identify the order of magnitude), and you typecast $log10 to (int), you may not get what you expect:
(int)(log(1000)/log(10)) = 2
(log(1000)/log(10)) = 3 (float with no displayed decimal places)

The mathematical error in this causes the typecast to round the result down, even though the error runs out to so many decimal places that it is not displayed, and the float value looks like a whole number.  Instead you may need to do:
round(log($n)/log(10)-0.5,0);

This will give you the order of magnitude of your number.

Presumably in PHP 4.3.0+, a similar result may occur.

<log1pmax>
 Last updated: Tue, 15 Nov 2005