Recursive AI

Calculate deterministic tanh in C#

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

Hyperbolic tangent (tanh) maps a signed input to a smooth response between −1 and 1.

Terminal
dotnet add package RecursiveAI.OneMath

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

C#
using RecursiveAI.OneMath;
double response = Math64.Tanh(0.5);
Console.WriteLine(FormattableString.Invariant($"{response:R}"));

Unzip the example, then dotnet run.

Prints:

Text
0.46211715726000974

The full program

C#
using RecursiveAI.OneMath;
 
foreach (double input in new[] { -22d, -2d, -0.5, 0, 0.5, 2, 22, double.NaN })
{
    double response = Math64.Tanh(input);
    Console.WriteLine(FormattableString.Invariant($"tanh({input}) = {response:R}"));
}
Text
tanh(-22) = -1
tanh(-2) = -0.9640275800758169
tanh(-0.5) = -0.46211715726000974
tanh(0) = 0
tanh(0.5) = 0.46211715726000974
tanh(2) = 0.9640275800758169
tanh(22) = 1
tanh(NaN) = NaN

The program uses invariant formatting so the decimal separator is the same on every run.

NaN stays NaN; if your input must represent a finite measurement, validate it before computing the response.

Variations

Keep the response in single precision. Use Math32.Tanh when the input and output are float:

C#
using System.Globalization;
using RecursiveAI.OneMath;
 
float response = Math32.Tanh(0.5f);
Console.WriteLine(response.ToString(
    CultureInfo.InvariantCulture));

This prints 0.46211717.

The short answer

Use Math64.Tanh(x) for double or Math32.Tanh(x) for float. The response preserves the sign, maps zero to zero, and approaches −1 or 1 as the input magnitude grows. According to the OneMath function contract, the same input bits produce the same result bits on every platform for the precision you choose.

How is tanh calculated?

The mathematical formula is (exp(2x) − 1) / (exp(2x) + 1). It explains the shape: the numerator is zero when x is zero, and for a large positive x the numerator and denominator become nearly equal. Negating x negates the response. Unlike the ordinary tangent, tanh has no poles and doesn’t repeat at intervals.

That formula is an identity, not a recommendation to evaluate it literally in C#. For small inputs, subtracting 1 from a rounded exponential can throw away much of the result; for large positive inputs, the exponential can overflow even though the response is bounded. OneMath’s function reference describes the expm1 form it uses in its higher-precision stage: it computes the exponential minus one without first rounding the exponential to a double and then subtracting. The public call returns a correctly rounded result, not the product of rounding each piece of the formula separately.

Does tanh ever reach 1?

For finite real inputs, the exact mathematical value is strictly between −1 and 1. In floating point, it can round to either endpoint. The program prints 1 for Math64.Tanh(22) and -1 for Math64.Tanh(-22); these are rounded results, not a decimal formatting trick.

Use the bounded result as a response, not as an invertible encoding of the input. Once two inputs map to the same rounded result, the original value can’t be recovered from the response. That also matters if the next formula requires strictly less than 1 in magnitude: handle the endpoints explicitly rather than assuming a finite input rules them out.

What does a deterministic tanh calculation mean?

Correct rounding means choosing the nearest representable value to the exact mathematical result, with ties resolved toward the even significand. Determinism here is about the bits returned for a given input, not how many decimal digits your console shows. If two callers compute different inputs before calling tanh, one call can’t make those inputs identical. Keep the input precision and preceding calculation consistent when you use the response in a replay or compare saved values.

All onemath recipes