A hypotenuse is the length of a right triangle’s longest side. Use Hypot instead of squaring large or tiny sides yourself.
dotnet add package RecursiveAI.OneMathRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneMath; Console.WriteLine( Math64.Hypot(3e200, 4e200));
Prints:
4.9999999999999995E+200Unzip the example, then dotnet run.
The full program
using RecursiveAI.OneMath; Console.WriteLine(Math64.Hypot(3e200, 4e200)); Console.WriteLine(Math64.Hypot(3e-200, 4e-200)); Console.WriteLine(Math64.Hypot(-3, 4)); Console.WriteLine(Math64.Hypot(double.PositiveInfinity, double.NaN)); Console.WriteLine(Math64.Hypot(double.NaN, 4)); Console.WriteLine(Math64.Hypot(double.MaxValue, double.MaxValue)); Console.WriteLine(Math32.Hypot(3f, 4f));
dotnet run prints:
4.9999999999999995E+200 5E-200 5 ∞ NaN ∞ 5
The large and tiny cases remain finite and nonzero. A result too large to represent becomes positive infinity; NaN inputs normally produce NaN, but infinity takes precedence.
Related recipes
The short answer
Call Math64.Hypot(x, y) to evaluate the square root of the sum of two squares without first rounding each square as a standalone double. In the example, sides 3e200 and 4e200 produce a finite length. Squaring either side directly would overflow before a square root could bring the magnitude back down.
.NET also provides double.Hypot. Choose Math64.Hypot when you need OneMath’s documented guarantee of correctly rounded results that are bit-identical across platforms.
OneMath’s function contract specifies correctly rounded results for every input, including gradual underflow and overflow to infinity. “Without overflow” here means avoiding unnecessary intermediate overflow. It does not mean that a double can hold a length beyond its representable range.
How do I calculate a hypotenuse without overflow in C#?
Use Hypot directly on the two side lengths. Do not form their squares before calling it: the function needs the original components. Passing already-overflowed squares loses the information it needs to calculate a finite answer.
The problem is the order of operations, not the Pythagorean identity. As John D. Cook explains, an intermediate square can overflow even though the final length fits. Rescaling is one way to avoid that intermediate problem. A function’s rounding contract is a separate issue from merely keeping intermediate values finite; OneMath documents both its rounded result and its special-value behavior.
The printed large result is 4.9999999999999995E+200, not a hand-rounded 5E+200. Decimal literals are parsed into binary doubles, and the function rounds the exact hypotenuse of those represented inputs. Preserve that distinction when comparing output. If the result is for display, format it at the boundary of your application; do not confuse shorter decimal output with a different internal value.
What is overflow versus underflow?
Overflow is a magnitude too large for the numeric format. Underflow concerns results at its tiny end, where precision reduces and sufficiently small values round to zero. Both can damage a square-and-add calculation before the square root runs.
The full program exercises tiny sides 3e-200 and 4e-200. Their hypotenuse prints 5E-200; explicitly squaring such inputs would instead lose the squares to zero in double arithmetic. Use the same Hypot call for large and tiny inputs rather than choosing a formula based on a guessed size threshold.
The exceptional cases also matter. The program shows that negative components are accepted: Hypot(-3, 4) is 5. Hypot(NaN, 4) returns NaN, while Hypot(PositiveInfinity, NaN) returns positive infinity, as the docs explicitly specify. Two double.MaxValue components also produce infinity because the length itself cannot fit. Check finiteness in your application if you require a finite geometric length; a function returning a documented special value is not an exception.