log

(PHP 4, PHP 5, PHP 7)

logНатуральный логарифм

Описание

float log ( float $arg [, float $base = M_E ] )

Если указан необязательный параметр base, log() возвращает logbase от arg, иначе log() возвращает натуральный логарифм числа arg.

Список параметров

arg

Число, для которого необходимо вычислить логарифм

base

Необязательное основание логарифма (по умолчанию основание 'e', что соответствует натуральному логарифму).

Возвращаемые значения

Логарифм числа arg по основанию base, если указано, или натуральный логарифм.

Смотрите также

  • log10() - Десятичный логарифм
  • exp() - Вычисляет число e в степени
  • pow() - Возведение в степень

Коментарии

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.
2004-06-20 16:06:31
http://php5.kiev.ua/manual/ru/function.log.html
Автор:
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];
}
2004-09-19 06:08:12
http://php5.kiev.ua/manual/ru/function.log.html
$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.
2005-02-03 09:22:39
http://php5.kiev.ua/manual/ru/function.log.html
Автор:
<?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 floor(log10(abs($n)));
  }else{
   
# here  logB(x) = log(x)/log(B) will have to do.
   
return floor(log(abs($n))/ log($base));
  }
}

# Example:  How many decimal digits for 2 to the power 24?
echo digit_count(pow(224));

# Example: How many bits to write 1 billion in binary, last century?

if($country_code  == 'US') echo digit_count(pow(109), 2);
if(
$country_code == 'UK') echo digit_count(pow(1012), 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 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
------------------------------------

*/

?>
2006-08-06 08:56:58
http://php5.kiev.ua/manual/ru/function.log.html
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

<?php
function bclog($X,$base=10,$decimalplace=12){
   
$integer_value=0;
    while(
$X 1){
       
$integer_value $integer_value ;
       
$X bcmul($X base);
    }
    while(
$X >= $base){
       
$integer_value $integer_value 1;
       
$X bcdiv($X $base );
    }
   
$decimal_fraction 0.0;
   
$partial 1.0;
   
# Replace X with X to the 10th power
   
$X bcpow($X 10);
    while(
$decimalplace 0){
       
$partial bcdiv($partial 10);
       
$digit=0;
        while(
$X >= $base){
             
$digit $digit 1;
             
$X bcdiv($X $base);
        }
       
$decimal_fraction bcadd($decimal_fraction bcmul($digit $partial));
       
# Replace X with X to the 10th power
       
$X bcpow($X 10);
       
$decimalplace $decimalplace ;
    }
    return 
$integer_value $decimal_fraction ;
}
?>
2008-05-15 22:41:45
http://php5.kiev.ua/manual/ru/function.log.html
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(1024floor($e))));
    }
?>
2008-11-04 06:56:55
http://php5.kiev.ua/manual/ru/function.log.html
Автор:
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

<?php 

$number
='fill in your number here'
$base='fill in requested base here';

//use when the power is needed
$pow=0;do { $number/=$base;$pow++; } while ($number>1);
if (
$number==1) print $base.'^'.$pow;
else print 
'False';

//use when just a check is needed
do $number/=$base; while ($number>1);
if (
$number==1) print 'True';
else print 
'False';
?>
2013-02-21 21:34:38
http://php5.kiev.ua/manual/ru/function.log.html
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 );
}

https://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');
2018-11-05 03:28:06
http://php5.kiev.ua/manual/ru/function.log.html

    Поддержать сайт на родительском проекте КГБ