Definition and Usage
Here 1p stands for 1 plus. The log1p () function calculates natural (base-e) logarithm of a 1+number.
log1p(x)=log(1+x).
log1p is calculated in a way such that its value is accurate even for very small x such that 1+x is nearly equal to x
Syntax
log1p ( float $arg ) : float
Parameters
Sr.No | Parameter & Description |
---|---|
1 | arg The number whose 1p logarithm is to be calculated |
Return Values
PHP log1p() function returns base-1p logarithm of arg+1.
PHP Version
This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.
Example
Following example calculates log1p of 100
<?php $arg=100; echo "using log() to calculate log(1+". $arg.")=" . log(1+$arg) . "\n"; echo "log1p(" . $arg . ")=" . log1p($arg); ?>
Output
This will produce following result −
using log() to calculate log(1+100)=4.6151205168413 log1p(100)=4.6151205168413
Example
where normal log(0) returns -infinity, log1p(0) returns 0−
<?php $arg=0; echo "log(" . $arg . ")=" . log($arg) . "\n"; echo "log1p(" . $arg . ")=" . log1p($arg); ?>
Output
This will produce following result −
log(0)=-INF log1p(0)=0
Example
For very small number, log1p() is more accurate −
<?php $arg=0.000005; echo "log(" . $arg . ")=" . log($arg) . "\n"; echo "log1p(" . $arg . ")=" . log1p($arg); ?>
Output
This will produce following result −
log(5.0E-6)=-12.20607264553 log1p(5.0E-6)=4.9999875000744E-6
Example
Similarly sqrt(-1) results in NAN. Hence its log1p() also returns NAN −
<?php $arg=sqrt(-1); echo "log(" . $arg . ")=" . log($arg) . "\n"; echo "log1p(" . $arg . ")=" . log1p($arg); ?>
Output
This will produce following result −
log(NAN)=NAN log1p(NAN)=NAN