---
title: "Calculate expm1 near zero in C#"
url: https://recursiveai.org/onemath/cookbook/calculate-exp-minus-one/
description: "Calculate exp(x) minus one near zero in C# with Math64.Expm1. See when subtracting one loses precision and how to keep small positive or negative results."
version: "1.0.0"
---

# Calculate expm1 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-exp-minus-one/calculate-exp-minus-one.zip)

`Expm1` computes `eˣ − 1` without first rounding `eˣ` 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.Expm1(1e-17));
```

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

```text
1E-17
```

## The full program

Compare both expressions for tiny inputs of either sign. The last two lines check zero and negative infinity.

```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.Exp(x) - 1 = {Math.Exp(x) - 1:R}"));
    Console.WriteLine(FormattableString.Invariant($"Math64.Expm1(x) = {Math64.Expm1(x):R}"));
}

Console.WriteLine($"At x = 0: {Math64.Expm1(0) == 0}");
Console.WriteLine($"At x = -infinity: {Math64.Expm1(double.NegativeInfinity) == -1}");
```

```text
x = 1E-17
Math.Exp(x) - 1 = 0
Math64.Expm1(x) = 1E-17
x = -1E-17
Math.Exp(x) - 1 = 0
Math64.Expm1(x) = -1E-17
At x = 0: True
At x = -infinity: True
```

If a positive input is large enough that the result overflows, `Expm1` returns positive infinity.

## Variations

**Use single precision.** Pass a `float` to `Math32.Expm1` for a `float` result.

```csharp
using RecursiveAI.OneMath;

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

```text
1E-08
```

## Related recipes

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

---

## The short answer

Call `Math64.Expm1(x)` when you need the amount by which `eˣ` differs from one. `Math.Exp(x) - 1` subtracts from an exponential that has already been rounded. When `x` is tiny, the rounded exponential may equal one even though the difference is large enough to represent on its own.

Yes, .NET has a built-in: [`double.ExpM1`](https://learn.microsoft.com/en-us/dotnet/api/system.double.expm1?view=net-10.0) is part of generic math in .NET 7 and later. On .NET 10, `Console.WriteLine(double.ExpM1(1e-17));` prints `0`.

## Why use expm1 instead of exp minus one?

When `x` is near zero, the exponential has the form `1 + x + x²/2 + …`. With `Math.Exp(x) - 1`, most of the precision is spent recording the leading `1`, then you subtract that part away. That’s cancellation: the error from rounding the exponential can be as large as the difference you wanted. For the two inputs above, the subtraction is exact, but the exponential it receives is already `1`.

A dedicated `Expm1` method computes the small difference directly near zero. For small enough `x`, OneMath returns `x` because the correction is too small to affect the rounded result. For other inputs it evaluates the function rather than treating `eˣ − 1` as equal to `x`. See [OneMath’s elementary-function notes](https://recursiveai.org/onemath/docs/functions/#methods-per-function), which also document signed zero and the rounding contract.

## How do you calculate compound interest with expm1?

Suppose `rate` is a fractional change per period, not a percentage. For a small rate, use `Log1p(rate)` to get the log of the growth factor, multiply by the number of periods and use `Expm1` to get the accumulated fractional change. This evaluates `(1 + rate)ⁿ − 1` without forming a growth factor that can round to one. The [POSIX expm1 reference](https://pubs.opengroup.org/onlinepubs/009604499/functions/expm1.html) describes this pairing for small interest rates.

```csharp
using RecursiveAI.OneMath;

const double rate = 1e-17;
const int periods = 100;
double logGrowth = periods * Math64.Log1p(rate);
Console.WriteLine(Math64.Expm1(logGrowth));
```

```text
1.0000000000000007E-15
```

The output is a change relative to the starting amount, not the final amount. Keeping it as a difference lets you use it later without rounding it away by adding it to one.
