Log1p computes the natural log of 1 + x without first rounding that sum to a double.
dotnet add package RecursiveAI.OneMathRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneMath; Console.WriteLine(Math64.Log1p(1e-17));
Unzip the example, then dotnet run.
1E-17The full program
Compare both expressions for tiny positive and negative values. The last two lines check what happens at and below -1.
using RecursiveAI.OneMath; foreach (double x in new[] { 1e-17, -1e-17 }) { Console.WriteLine(FormattableString.Invariant($"x = {x:R}")); Console.WriteLine(FormattableString.Invariant($"Math.Log(1 + x) = {Math.Log(1 + x):R}")); Console.WriteLine(FormattableString.Invariant($"Math64.Log1p(x) = {Math64.Log1p(x):R}")); } Console.WriteLine($"At x = -1: {double.IsNegativeInfinity(Math64.Log1p(-1))}"); Console.WriteLine($"Below x = -1: {double.IsNaN(Math64.Log1p(-2))}");
x = 1E-17 Math.Log(1 + x) = 0 Math64.Log1p(x) = 1E-17 x = -1E-17 Math.Log(1 + x) = 0 Math64.Log1p(x) = -1E-17 At x = -1: True Below x = -1: True
Math64.Log1p(-1) returns negative infinity; smaller inputs return NaN rather than throwing.
Variations
Use a float input and result. Math32.Log1p rounds to single precision.
using RecursiveAI.OneMath; Console.WriteLine(Math32.Log1p(1e-8f));
1E-08Related recipes
The short answer
Pass the small change itself to Math64.Log1p(x). It returns the natural logarithm of 1 + x. If you add x to 1 in a double first, you can lose the change before the logarithm sees it. Math32.Log1p is the corresponding method for float.
Yes, .NET has a built-in: double.LogP1 is part of generic math in .NET 7 and later. On .NET 10, Console.WriteLine(double.LogP1(1e-17)); prints 0.
What does log1p do?
log1p(x) means ln(1 + x); it isn’t a base-10 or base-2 logarithm. Use it when you start with a change relative to one. For example, if an amount grows by a fraction x, log1p(x) gives the logarithm of the growth factor. If you already have the number whose natural logarithm you want, use Math64.Log instead.
Why use log1p instead of log near zero?
For x = 1e-17 and x = -1e-17, the exact sums lie closer to 1 than to either neighboring double. 1 + x thus rounds to 1 in both cases, and Math.Log(1) correctly returns zero. Even if the sum doesn’t round all the way to one, the bits lost in the addition can be a large fraction of the small change you wanted to measure.
Log1p uses x directly. For the tiniest values the documented implementation returns x: the next term of the logarithm is too small to change how the result rounds. That explains the output above, but isn’t a rule to replace Log1p(x) with x for larger inputs. See OneMath’s elementary-function contract for rounding and special values.
What is log1p of zero?
It is zero because ln(1) = 0. For -1 < x < 0 it is negative, and for x > 0 it is positive. At x = -1 the sum reaches zero and the result is negative infinity; below -1 there’s no real result and the API returns NaN. If downstream code needs a finite result, check the input range before passing it along.