---
title: "Calculate log1p near zero in C#"
url: https://recursiveai.org/onemath/cookbook/calculate-log-one-plus-x/
description: "Calculate the natural log of 1 + x in C# with Math64.Log1p. See why Math.Log(1 + x) returns zero for tiny inputs and how Log1p preserves the change."
version: "1.0.0"
---

# Calculate log1p near zero in C#

**OneMath** is a free, open-source deterministic math library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneMath) · [GitHub](https://github.com/recursive-ai-oss/onemath-csharp) · [Docs](https://recursiveai.org/onemath/docs/) · [Download example (.zip)](https://recursiveai.org/onemath/cookbook/calculate-log-one-plus-x/calculate-log-one-plus-x.zip)

`Log1p` computes the natural log of `1 + x` without first rounding that sum to a `double`.

```shell
dotnet add package RecursiveAI.OneMath
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneMath;

Console.WriteLine(Math64.Log1p(1e-17));
```

Unzip [the example](https://recursiveai.org/onemath/cookbook/calculate-log-one-plus-x/calculate-log-one-plus-x.zip), then `dotnet run`.

```text
1E-17
```

## The full program

Compare both expressions for tiny positive and negative values. The last two lines check what happens at and below `-1`.

```csharp
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))}");
```

```text
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.

```csharp
using RecursiveAI.OneMath;

Console.WriteLine(Math32.Log1p(1e-8f));
```

```text
1E-08
```

## Related recipes

- [Calculate expm1 near zero in C#](https://recursiveai.org/onemath/cookbook/calculate-exp-minus-one/)
- [Deterministic exp and log functions in C#](https://recursiveai.org/onemath/cookbook/deterministic-exp-and-log/)

---

## 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`](https://learn.microsoft.com/en-us/dotnet/api/system.double.logp1?view=net-10.0) 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](https://recursiveai.org/onemath/docs/functions/#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.
