Calculates the natural logarithm (base e) of 1 + x.
LOG1P(x)
x is the number input, and should be greater than or equal to -1.
Example:
If x contains 12:
LOG1P(12)
returns 2.564949357
If x contains -2:
LOG1P(-2)
returns #NUM!
x:
Result:
An application of the log1p function is in calculating the accurate percentage change of a stock price. When a stock price changes from $100.00 to $100.01, the percentage change is calculated as:
To get back the new value from the original value and the percentage change, we can use the formula:
New Value=Original Value×(1+percentage change)
So, 100.01=100×(1+0.0001)
This formula works well, but what if we want to calculate the natural logarithm of the ratio of the new value to the original value, which is a common calculation in financial modeling, especially for continuously compounded returns?
The formula for the continuously compounded return is:
Now, let's see how log1p helps. The log1p function is defined as ln(1+x). In our case, the percentage change is x=0.0001. So, we can rewrite the formula as:
This is where the advantage of log1p becomes apparent. Due to the limitations of floating-point arithmetic in computers, when x is a very small number, the calculation of 1+x can lose precision. For example, if we were working with a very small change, like 1×10−16, adding it to 1 might result in 1 because of the way floating-point numbers are stored. The log1p function is specifically designed to calculate ln(1+x) accurately for very small values of x. It achieves this by using a different algorithm that avoids the intermediate addition of 1, thus preserving the precision of the small value x.
Here's a table comparing the direct calculation of ln(1+x) with log1p(x) for small values of x:
x (Percentage Change) | 1+x | LN(1+x) | LOG1P(x) | Precision Difference | ||
|---|---|---|---|---|---|---|
A | B | C | D | E | ||
1 | 1×10−5 | 1.00001 | 0.000009999950000008 | 0.000009999950000008 | 0 | |
2 | 1×10−10 | 1.0000000001 | 0.000000000099999999 | 0.000000000099999999 | 0 | |
3 | 1×10−16 | 1.0000000000000000 | 0.000000000000000000 | 0.000000000000000100 | Large | |
4 | 1×10−20 | 1.0000000000000000 | 0.000000000000000000 | 0.000000000000000001 | Large |
As you can see, for extremely small values of x (e.g., 1×10−16 or 1×10−20), the direct calculation of ln(1+x) fails completely and returns 0 due to precision loss. The log1p function, however, provides a highly accurate result, which is crucial in scientific and financial computations where these small changes can be significant.
PRODUCT & FEATURES
RESOURCES
Terms | Privacy | Spam Policy
© 2026 Zapof