略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: log1p

2024-12-21

log1p

(PHP 4 >= 4.1.0, PHP 5, PHP 7, PHP 8)

log1p 返回 log(1 + number),甚至当 number 的值接近零也能计算出准确结果

说明

log1p(float $number): float

log1p() 返回 log(1 + number),甚至当 number 的值接近零也能计算出准确结果。 log() might only return log(1) in this case due to lack of precision.

参数

number

要处理的参数

返回值

log(1 + number)

更新日志

版本 说明
5.3.0 此函数在所有平台上均可用

参见

  • expm1() - 返回 exp(number) - 1,甚至当 number 的值接近零也能计算出准确结果
  • log() - 自然对数
  • log10() - 以 10 为底的对数
add a noteadd a note

User Contributed Notes 1 note

up
3
Anonymous
19 years ago
Note that the benefit of this function for small argument values is lost if PHP is compiled against a C library that that not have builtin support for the log1p() function.

In this case, log1p() will be compiled by using log() instead, and the precision of the result will be identical to log(1), i.e. it will always be 0 for small numbers.
Sample log1p(1.0e-20):
- returns 0.0 if log1p() is approximated by using log()
- returns something very near from 1.0e-20, if log1p() is supported by the underlying C library.

One way to support log1p() correctly on any platform, so that the magnitude of the expected result is respected:

function log1p($x) {
return ($x>-1.0e-8 && $x<1.0e-8) ? ($x - $x*$x/2) : log(1+$x);
}

If you want better precision, you may use a better limited development, for small positive or negative values of x:

log(1+x) = x - x^2/2 + x^3/3 - ... + (-1)^(n-1)*x^n/n + ...

(This serial sum converges only for values of x in [0 ... 1] inclusive, and the ^ operator in the above formula means the exponentiation operator, not the PHP xor operation)

Note that log1p() is undefined for arguments lower than or equal to -1, and that the implied base of the log function is the Neperian "e" constant.

官方地址:https://www.php.net/manual/en/function.log1p.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3