[PHP 4, PHP 5, PHP 7, PHP 8]
log — Natural logarithm
Description
log[float $num
, float $base
= M_E
]: float
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
c0x at mail dot ru ¶
17 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];
}
olafurw [at] gmail.com ¶
13 years ago
For those interested. Works with older than 4.3 versions.
ClaudiuS ¶
9 years ago
If you just need to check if N is a perfect power of Base, log[] is SLOW compared to a WHILE construct that will be 2x faster!
Tested on range: 1 ... 20.000.000 => while[] is 2.105 times faster
mcmeijer at yahoo dot com ¶
17 years ago
$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.
rhinopete at batpudding dot com ¶
3 years ago
Seems like unit prefixes should have a standard PHP function. Maybe in the future.
I found this page while looking for a quick unit prefix function. The one by olafurw was voted down, I think because it had unchecked array indexes and /0s. So here it is fixed and readable.
-Should work down to PHP 4.
-Not meant for fractions or negatives, so anything less than 1 returns 0.
-Not very effective on really really large numbers, but it's easy to add more prefixes.
-Doesn't handle non numeric arguments. PHP 7+ can do this: function binaryprefix[ int $units, $unit = '' ]
// returns reduced $units with a binary prefix
// ex. [ 110974120, 'B' ] == 105.8MiB
// ex. [ 2^100, 'B' ] == 1048576.0YiB
// ex. [ 0.12314, 'B' ] == 0B
function binaryprefix[ $units, $unit = '' ]
{
$prefix = array['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'];
$exponent = min[ floor[ log[ max[ 1, $units ], 1024 ] ], count[ $prefix ] - 1 ];
if [ $units < 1024 ]
return sprintf[ '%d%s%s', max[ 1, $units + 1 ] - 1, $prefix[$exponent], $unit ];
else
return sprintf[ '%.1f%s%s', $units / pow[1024, $exponent], $prefix[$exponent], $unit ];
}
//en.wikipedia.org/wiki/Binary_prefix#Adoption_by_IEC.2C_NIST_and_ISO
Also more colloquially:
$prefix = array['', 'k', 'm', 'g', 't', 'p', 'e', 'z', 'y'];
admin at worldtakeover dot tk ¶
18 years ago
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.
dingus_76 at hotmail dot com ¶
14 years ago
well i been pulling my hair out trying to get log to work with big numbers and i ended up writing a bclog function so to save everyone else the stress here it is
Ulf Wostner ¶
16 years ago