---
title: "Deterministic exp and log functions in C#"
url: https://recursiveai.org/onemath/cookbook/deterministic-exp-and-log/
description: "Evaluate deterministic exponential and logarithmic functions in C# with OneMath. See output bits, tiny inputs, domain errors and subnormal results."
version: "1.0.0"
---

# Deterministic exp and log functions 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/deterministic-exp-and-log/deterministic-exp-and-log.zip)

Use exponential and logarithmic functions when the same floating-point inputs must produce the same output bits across platforms.

```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.Exp(1));
Console.WriteLine(Math64.Log(2));
```

Prints e¹ and the natural logarithm of 2.

```text
2.718281828459045
0.6931471805599453
```

Unzip [the example](https://recursiveai.org/onemath/cookbook/deterministic-exp-and-log/deterministic-exp-and-log.zip), then `dotnet run`.

## Full program

```csharp
using RecursiveAI.OneMath;
using System.Globalization;

void Show(string label, double value)
{
    Console.WriteLine($"{label}: {value.ToString("R", CultureInfo.InvariantCulture)} 0x{BitConverter.DoubleToUInt64Bits(value):X16}");
}
Show("Exp(1)", Math64.Exp(1));
Show("Log(2)", Math64.Log(2));
Show("Exp2(-1074)", Math64.Exp2(-1074));
Show("Exp2(-1075)", Math64.Exp2(-1075));
Show("Log(0)", Math64.Log(0));
Show("Log(-1)", Math64.Log(-1));
Show("Expm1(-0)", Math64.Expm1(-0.0));
Show("Log1p(1e-20)", Math64.Log1p(1e-20));
ulong expectedExp = 0x4005BF0A8B145769;
ulong expectedLog = 0x3FE62E42FEFA39EF;
bool match = true;
for (int i = 0; i < 10; i++)
    match &= BitConverter.DoubleToUInt64Bits(Math64.Exp(1)) == expectedExp
        && BitConverter.DoubleToUInt64Bits(Math64.Log(2)) == expectedLog;
Console.WriteLine($"Repeated bits match: {match}");
```

```text
Exp(1): 2.718281828459045 0x4005BF0A8B145769
Log(2): 0.6931471805599453 0x3FE62E42FEFA39EF
Exp2(-1074): 5E-324 0x0000000000000001
Exp2(-1075): 0 0x0000000000000000
Log(0): -Infinity 0xFFF0000000000000
Log(-1): NaN 0xFFF8000000000000
Expm1(-0): -0 0x8000000000000000
Log1p(1e-20): 1E-20 0x3BC79CA10C924223
Repeated bits match: True
```

Domain errors return NaN, not exceptions; overflow returns infinity and underflow rounds into subnormals or zero.

## Variations

### Base two, base ten and small changes

```csharp
using RecursiveAI.OneMath;

Console.WriteLine(Math64.Exp2(3));
Console.WriteLine(Math64.Log2(8));
Console.WriteLine(Math64.Log10(100));
Console.WriteLine(Math64.Expm1(1e-20));
Console.WriteLine(Math64.Log1p(1e-20));
Console.WriteLine(Math32.Exp(1));
```

```text
8
3
2
1E-20
1E-20
2.7182817
```

## Related recipes

- [Invert a matrix](https://recursiveai.org/onemath/cookbook/invert-matrix/)
- [Compute inertia tensors](https://recursiveai.org/onemath/cookbook/inertia-tensors/)
- [Deterministic trigonometry](https://recursiveai.org/onemath/cookbook/deterministic-trigonometry/)

---

## The short answer

Call `Math64.Exp` and `Math64.Log` from `RecursiveAI.OneMath` for deterministic double results, or use `Math32` for float results. `Exp(x)` evaluates eˣ and `Log(x)` is the natural logarithm. The example prints both numbers and their IEEE 754 binary64 bit patterns, then repeats the calls against known bits.

The [published contract](https://recursiveai.org/onemath/docs/functions/#contract) says results are correctly rounded to nearest, ties to even, with gradual underflow and overflow to infinity. It also specifies canonical NaNs. This is a per-function contract; it does not make a larger expression insensitive to changes in its evaluation order.

## How do I calculate exp and log in C# deterministically?

Use the `Math64` calls in the snippet at the top and keep the input type fixed. A double call and a float call have different output formats and therefore different rounded values. The variation prints `2.7182817` for `Math32.Exp(1)`, while the double example prints `2.718281828459045`.

For ordinary framework math, Microsoft’s [Math.Log remarks](https://learn.microsoft.com/en-us/dotnet/api/system.math.log?view=net-10.0#remarks) say that the underlying C runtime can produce different exact results or valid ranges across operating systems or architectures. OneMath instead documents a platform-independent result. Its function implementation uses floating-point arithmetic, integer arithmetic and an exact fallback rather than delegating function results to the platform math library.

The full program converts each result with `BitConverter.DoubleToUInt64Bits`. This distinguishes values that a short decimal display could make look identical, including positive and negative zero. It compares repeated results for this run; it is not itself a cross-platform test. The guarantee comes from the library’s contract, not from repeating a call on one machine.

## What is the difference between log, log2 and log10 in C#?

`Math64.Log` uses base e, `Log2` uses base two, and `Log10` uses base ten. Their exponential counterparts are `Exp` and `Exp2`. In the variation, `Exp2(3)` returns 8, `Log2(8)` returns 3 and `Log10(100)` returns 2.

Choose the function that expresses the operation directly. Replacing a base-two call with a quotient of two rounded logarithms changes the sequence of operations. Each function’s rounding contract applies to that function’s mathematical result, not to an algebraic rearrangement assembled from several calls.

The same distinction matters near zero. `Expm1(x)` evaluates eˣ − 1 directly, and `Log1p(x)` evaluates ln(1 + x) directly. Both return `1E-20` for the tiny positive input in the variation. Use them when the small change is the quantity you need, rather than forming an intermediate sum or difference that can round away that change.

## What happens to exp and log at zero or outside their domain?

The full program shows `Log(0)` returning negative infinity and `Log(-1)` returning NaN. Domain errors return NaN rather than throwing an exception. Every NaN result uses the canonical .NET NaN bit pattern, regardless of the input payload. `Expm1(-0)` preserves the negative zero sign.

Underflow is gradual, not an unconditional flush to zero. `Exp2(-1074)` produces the smallest positive double, whose bit pattern is `0x0000000000000001`. At the halfway boundary, `Exp2(-1075)` rounds to zero under ties-to-even. Very large exponential results overflow to positive infinity.

The [implementation documentation](https://recursiveai.org/onemath/docs/functions/#how-a-result-is-produced-three-stages-and-a-rounding-test) describes an exact fallback for ambiguous, subnormal and overflow-edge results. It also states that the earlier stages’ error bounds are validated, not proved. Use that published scope rather than treating the implementation as a formal proof of the whole algorithm.
