---
title: "Calculate a hypotenuse without overflow in C#"
url: https://recursiveai.org/onemath/cookbook/calculate-overflow-safe-hypotenuse/
description: "Calculate a hypotenuse in C# without intermediate square overflow or underflow. Run OneMath examples with large, tiny, infinite and NaN inputs."
version: "1.0.0"
---

# Calculate a hypotenuse without overflow 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-overflow-safe-hypotenuse/calculate-overflow-safe-hypotenuse.zip)

A hypotenuse is the length of a right triangle’s longest side. Use `Hypot` instead of squaring large or tiny sides yourself.

```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.Hypot(3e200, 4e200));
```

Prints:

```text
4.9999999999999995E+200
```

Unzip [the example](https://recursiveai.org/onemath/cookbook/calculate-overflow-safe-hypotenuse/calculate-overflow-safe-hypotenuse.zip), then `dotnet run`.

## The full program

```csharp
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:

```text
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

- [Deterministic powers and cube roots](https://recursiveai.org/onemath/cookbook/deterministic-powers-and-cube-roots/)
- [Test a point in a triangle robustly](https://recursiveai.org/onemath/cookbook/test-point-in-triangle-robustly/)

## 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`](https://learn.microsoft.com/en-us/dotnet/api/system.double.hypot?view=net-10.0). Choose `Math64.Hypot` when you need OneMath’s documented guarantee of correctly rounded results that are bit-identical across platforms.

OneMath’s [function contract](https://recursiveai.org/onemath/docs/functions/) 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](https://www.johndcook.com/blog/2010/06/02/whats-so-hard-about-finding-a-hypotenuse/), 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.
