Compute hyperbolic cosine with the same result bits across machines.
dotnet add package RecursiveAI.OneMathRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneMath; Console.WriteLine(Math64.Cosh(1));
Unzip the example, then dotnet run.
Prints 1.5430806348152437 with an English locale.
The full program
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})")); }
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:
using RecursiveAI.OneMath; Console.WriteLine(Math32.Cosh(1f));
This prints 1.5430807 with an English locale.
Related recipes
The short answer
For the same input bits, Math64.Cosh returns the same result bits on every supported machine and runtime; Math.Cosh doesn’t promise that. Use Math64.Cosh(x) for a double, or Math32.Cosh(x) for a float. OneMath’s function contract 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. 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.