log

(PHP 4, PHP 5, PHP 7, PHP 8)

logNatural logarithm

Description

log(float $num, float $base = M_E): float

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

Parameters

num

The value to calculate the logarithm for

base

The optional logarithmic base to use (defaults to 'e' and so to the natural logarithm).

Return Values

The logarithm of num to base, if given, or the natural logarithm.

See Also

  • log10() - Base-10 logarithm
  • exp() - Calculates the exponent of e
  • pow() - Exponential expression
  • error_log() - Send an error message to the defined error handling routines
add a note

User Contributed Notes 2 notes

up
-3
c0x at mail dot ru
19 years ago
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];
}
up
-5
olafurw [at] gmail.com
15 years ago
For those interested. Works with older than 4.3 versions.

<?php
function byteConvert($bytes)
{
$s = array('B', 'Kb', 'MB', 'GB', 'TB', 'PB');
$e = floor(log($bytes)/log(1024));

return
sprintf('%.2f '.$s[$e], ($bytes/pow(1024, floor($e))));
}
?>
To Top