log1p
(PHP 4 >= 4.0.7, PHP 5)
log1p — Returns log(1 + number), computed in a way that is accurate even when the value of number is close to zero
Описание
Эта функция является ЭКСПЕРИМЕНТАЛЬНОЙ. Поведение этой функции, ее имя и относящаяся к ней документация могут измениться в последующих версиях PHP без уведомления. Используйте эту функцию на свой страх и риск.
log1p() returns log(1 + number ) computed in a way that is accurante even when the value of number is close to zero. log() might only return log(1) in this case due to lack of precision.
Замечание: Для Windows-платформ эта функция не реализована.
Список параметров
- number
-
The argument to process
Возвращаемые значения
log(1 + number )
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Математические расширения
- Математические функции
- abs
- acos
- acosh
- asin
- asinh
- atan2
- atan
- atanh
- base_convert
- bindec
- ceil
- cos
- cosh
- decbin
- dechex
- decoct
- deg2rad
- exp
- expm1
- floor
- fmod
- getrandmax
- hexdec
- hypot
- intdiv
- is_finite
- is_infinite
- is_nan
- lcg_value
- log10
- log1p
- log
- max
- min
- mt_getrandmax
- mt_rand
- mt_srand
- octdec
- pi
- pow
- rad2deg
- rand
- round
- sin
- sinh
- sqrt
- srand
- tan
- tanh
Коментарии
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.