PHP log1p() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Math

Definition

In PHP, log1p() returns the natural logarithm of 1 plus a number, written as log(1 + num).

It gives more accurate answers than log(1 + $x) when $x is very close to zero, which is useful in finance and science.

Syntax

Call log1p() with the values you need. Start with this line.

PHP
$result = log1p(0.05);
CallWhat it does
log1p(0)Returns 0, because log(1) is 0.
log1p(1)Returns about 0.6931, which is log(2).
log1p(0.05)Returns about 0.0488, the log of 1.05.

For normal-sized numbers, log1p($x) and log(1 + $x) give nearly the same result. log1p() matters when $x is tiny, because adding 1 first can lose precision. It is the partner of expm1().

Library declarations
PHP
log1p(float $num): float

Parameters

ParameterDescription
numRequired. The value to add to 1 before taking the natural logarithm. It must be greater than -1.

Return Value

Return ValueDescription
floatThe natural logarithm of 1 + num. Returns -INF for -1 and NAN for values below -1.

PHP Examples

Example 1 — Basic log1p() Values

Try log1p() with a few simple inputs and round the results for easy reading.

PHP
<?php
echo log1p(0), PHP_EOL;
echo round(log1p(1), 4), PHP_EOL;
echo round(log1p(0.5), 4), PHP_EOL;

How It Works

  1. log1p(0) is log(1), which is 0.
  2. log1p(1) is log(2), about 0.6931 after rounding.
  3. log1p(0.5) is log(1.5), about 0.4055 after rounding.
  4. round() keeps the output short and easy to read.

Example 2 — Values At or Below -1

The value 1 + num must be positive. Check what happens at the limit.

PHP
<?php
echo log1p(-1), PHP_EOL;
echo log1p(-2), PHP_EOL;

How It Works

  1. log1p(-1) is log(0), which goes toward negative infinity.
  2. PHP prints this as -INF.
  3. log1p(-2) is log(-1), which is not a real number.
  4. PHP prints this as NAN.

Example 3 — Compare log1p() and log()

For a normal growth rate both formulas agree. Use log1p() when the rate is extremely small.

PHP
<?php
$growth = 0.05;

echo "log1p: ", round(log1p($growth), 6), PHP_EOL;
echo "log:   ", round(log(1 + $growth), 6), PHP_EOL;

How It Works

  1. Set a growth rate of 5 percent as 0.05.
  2. log1p($growth) calculates log(1.05) directly.
  3. log(1 + $growth) does the same in two steps.
  4. Both match here, but log1p() is more accurate for tiny values.

Frequently Asked Questions

log1p() returns log(1 + num), the natural logarithm of one plus the number. It stays accurate even when the number is very close to zero.
When $x is tiny, adding 1 first can round away most of $x. log1p() avoids that step, so the answer keeps its precision.
The opposite is expm1(), which returns exp(num) - 1 with the same accuracy for tiny values.
Any number greater than -1. At -1 the result is -INF, and below -1 the result is NAN.
No. It uses the natural logarithm (base e). For base 10 use log10(1 + $x).

Next: max()

max() returns the highest value from a list of values or from an array.

max() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful