<?php
#--------------------------------------------------------
# How many digits does an integer have?
#--------------------------------------------------------
function digit_count($n, $base=10) {
if($n == 0) return 1;
if($base == 10) {
# using the built-in log10(x)
# might be more accurate than log(x)/log(10).
return 1 + floor(log10(abs($n)));
}else{
# here logB(x) = log(x)/log(B) will have to do.
return 1 + floor(log(abs($n))/ log($base));
}
}
# Example: How many decimal digits for 2 to the power 24?
echo digit_count(pow(2, 24));
# Example: How many bits to write 1 billion in binary, last century?
if($country_code == 'US') echo digit_count(pow(10, 9), 2);
if($country_code == 'UK') echo digit_count(pow(10, 12), 2);
#--------------------------------------------------------
# Using log to format columns.
#--------------------------------------------------------
# Suppose we have a dynamically generated list of integers,
# and want to present them as a table. The use of log10 in
# our digit_count helps calculate the proper format string.
function print_list_of_ints($ints, $line_width=40) {
# Apply our digit_count to the max int among ints.
$field_width = 2 + digit_count(max($ints));
# Create format string for printf.
$format = "%${field_width}d";
$ints_per_line = floor($line_width/$field_width);
$border = str_repeat("-", $ints_per_line * $field_width);
echo "\n$border\n";
foreach($ints as $count => $int) {
if( $count and ($count % $ints_per_line == 0)) echo "\n";
printf($format, $int);
}
echo "\n$border\n";
}
# To generate an example, here is a basic function
# returning a list of (pseudo) random numbers.
function rands($how_many) {
for($i=0; $i < $how_many; $i++) $rands[] = rand();
return $rands;
}
# Example: A list of random ints dynamically formatted into columns.
print_list_of_ints(rands(11));
/* Sample output. Numbers and fonts vary. Visualize monospace!
------------------------------------
1093146637 244503173 1346204527
638304372 140216732 1054707210
573915416 1728677954 2038899669
534854768 12674586
------------------------------------
*/
?>
log
(PHP 4, PHP 5)
log — 自然対数
説明
float log
( float $arg
[, float $base
] )
オプションの base パラメータを指定した場合は log() は logbase arg を返します。それ以外の場合は log() は arg の自然対数を返します。
パラメータ
- arg
-
対数を計算する値。
- base
-
オプションで指定する、底 (デフォルトは 'e' で、これは自然対数となります)。
返り値
base を指定した場合はそれを底とする arg の対数、指定しない場合は自然対数を返します。
変更履歴
| バージョン | 説明 |
|---|---|
| 4.3.0 以降 | オプションのパラメータ base が使用可能となりました。 これ以前のバージョンでも b を底とする n の対数を計算することが可能です。しかし、数学の等式 logb(n) = log(n)/log(b) を使用する必要があります。ここで、log は自然対数です。 |
log
Ulf Wostner
06-Aug-2006 09:56
06-Aug-2006 09:56
mcmeijer at yahoo dot com
04-Feb-2005 12:22
04-Feb-2005 12: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 07:08
19-Sep-2004 07: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
21-Jun-2004 05:06
21-Jun-2004 05: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
07-Feb-2003 05:02
07-Feb-2003 05: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.