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.
log1p
(PHP 4 >= 4.0.7, PHP 5)
log1p — 値がゼロに近い時にでも精度を保つ方法で計算した log(1 + number) を返す
説明
float log1p
( float $number
)
警告
この関数は、 実験的 なものです。この関数の動作・ 名前・その他ドキュメントに書かれている事項は、予告なく、将来的な PHP のリリースにおいて変更される可能性があります。 この関数は自己責任で使用してください。
log1p() は、 log(1 + number ) の値を返します。 number がゼロに近い場合でも正確な値が 計算できる方法を使用します。 log() は、このような場合には 精度の問題で log(1) の値を返してしまいます。
注意: この関数は Windows 環境にはまだ実装されていません。
パラメータ
- number
-
処理する引数。
返り値
log(1 + number ) を返します。
log1p
11-Sep-2002 06:29