PHP log1p() Function


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.NoParameter & Description
1arg
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

 Live Demo

Following example calculates log1p of 100

<?php
   $arg=100;
   echo "using log() to calculate log(1+". $arg.")=" . log(1+$arg) . "
";    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

 Live Demo

where normal log(0) returns -infinity, log1p(0) returns 0−

<?php
   $arg=0;
   echo "log(" . $arg . ")=" . log($arg) . "
";    echo "log1p(" . $arg . ")=" . log1p($arg); ?>

Output

This will produce following result −

log(0)=-INF
log1p(0)=0

Example

 Live Demo

For very small number, log1p() is more accurate −

<?php
   $arg=0.000005;
   echo "log(" . $arg . ")=" . log($arg) . "
";    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

 Live Demo

Similarly sqrt(-1) results in NAN. Hence its log1p() also returns NAN −

<?php
   $arg=sqrt(-1);
   echo "log(" . $arg . ")=" . log($arg) . "
";    echo "log1p(" . $arg . ")=" . log1p($arg); ?>

Output

This will produce following result −

log(NAN)=NAN
log1p(NAN)=NAN

Updated on: 29-Jun-2020

78 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements