---
title: "Calculate deterministic cosh in C#"
url: https://recursiveai.org/onemath/cookbook/calculate-deterministic-hyperbolic-cosine/
description: "Calculate deterministic hyperbolic cosine (cosh) with OneMath in C#. Get a correctly rounded double or float and see what happens near zero and overflow."
version: "1.0.0"
---

# Calculate deterministic cosh 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-deterministic-hyperbolic-cosine/calculate-deterministic-hyperbolic-cosine.zip)

Compute hyperbolic cosine with the same result bits across machines.

```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.Cosh(1));
```

Unzip [the example](https://recursiveai.org/onemath/cookbook/calculate-deterministic-hyperbolic-cosine/calculate-deterministic-hyperbolic-cosine.zip), then `dotnet run`.

Prints `1.5430806348152437` with an English locale.

## The full program

```csharp
using RecursiveAI.OneMath;

foreach (double x in new[] { 1, -1, 0, 1e-20, 710, 711, double.NaN })
{
    double value = Math64.Cosh(x);
    long bits = BitConverter.DoubleToInt64Bits(value);
    Console.WriteLine(FormattableString.Invariant(
        $"cosh({x:R}) = {value:R} (0x{bits:X16})"));
}
```

```text
cosh(1) = 1.5430806348152437 (0x3FF8B07551D9F550)
cosh(-1) = 1.5430806348152437 (0x3FF8B07551D9F550)
cosh(0) = 1 (0x3FF0000000000000)
cosh(1E-20) = 1 (0x3FF0000000000000)
cosh(710) = 1.1169973830808555E+308 (0x7FE3E21A464507F9)
cosh(711) = Infinity (0x7FF0000000000000)
cosh(NaN) = NaN (0xFFF8000000000000)
```

The program prints the result in decimal and as hexadecimal bits. Overflow returns positive infinity rather than throwing, and a `NaN` input returns `NaN`.

## Variations

Use `Math32.Cosh` to round directly to a `float`:

```csharp
using RecursiveAI.OneMath;

Console.WriteLine(Math32.Cosh(1f));
```

This prints `1.5430807` with an English locale.

## Related recipes

- [Calculate deterministic hyperbolic sine](https://recursiveai.org/onemath/cookbook/calculate-deterministic-hyperbolic-sine/)
- [Calculate deterministic powers and cube roots](https://recursiveai.org/onemath/cookbook/deterministic-powers-and-cube-roots/)

---

## The short answer

For the same input bits, `Math64.Cosh` returns the same result bits on every supported machine and runtime; [`Math.Cosh`](https://learn.microsoft.com/en-us/dotnet/api/system.math.cosh?view=net-10.0#remarks) doesn’t promise that. Use `Math64.Cosh(x)` for a `double`, or `Math32.Cosh(x)` for a `float`. OneMath’s [function contract](https://recursiveai.org/onemath/docs/functions/) specifies IEEE 754 rounding to the nearest representable value, with ties rounded to even. For a replay or simulation check, compare the bits of inputs as well as results: identical displayed decimals don’t necessarily mean identical inputs.

## What is the difference between cosine and hyperbolic cosine?

Cosine repeats and stays between −1 and 1. Hyperbolic cosine doesn’t repeat: it’s the average of two exponentials, [(eˣ + e⁻ˣ) / 2](https://www.mathcentre.ac.uk/resources/workbooks/mathcentre/hyperbolicfunctions.pdf). Switching the sign of `x` swaps the two terms without changing the result. That’s why `1` and `−1` give exactly the same bits in the example. The curve rises on both sides of `x = 0` rather than oscillating.

The real-valued formula also explains a trap in implementing it with separate floating-point exponentials. At `x = 710`, the positive exponential doesn’t fit in a `double`, but half of it does. If the exponential overflows before you divide it, the division can’t recover the finite answer. The example shows `Math64.Cosh(710)` returning a finite result and `Math64.Cosh(711)` returning positive infinity. Those two rows are examples, not the exact overflow boundary.

## Can cosh ever be zero?

Not for a real input: both exponentials are positive, and the smallest value of the average is 1, at zero. A *rounded* result of 1 doesn’t prove the input was zero. The function reference says inputs with magnitude below 2⁻²⁷ round to 1; the `1e-20` row shows this for a nonzero value. If you need to distinguish a tiny nonzero input from zero, inspect the input rather than testing whether its hyperbolic cosine equals 1.
