Recursive AI

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.

Expm1 computes eˣ − 1 without first rounding eˣ to a double.

Terminal
dotnet add package RecursiveAI.OneMath

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneMath;
 
Console.WriteLine(Math64.Expm1(1e-17));

Unzip the example, 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.

C#
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.

C#
using RecursiveAI.OneMath;
 
Console.WriteLine(Math32.Expm1(1e-8f));
Text
1E-08

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 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, 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 describes this pairing for small interest rates.

C#
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.

All onemath recipes