---
title: "Compare squared distances in C#"
url: https://recursiveai.org/onemath/cookbook/compare-distance-without-square-root/
description: "Compare a squared 3D distance with a squared radius in C#. Use OneMath to check whether a point is in range without taking a square root."
version: "1.0.0"
---

# Compare squared distances 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/compare-distance-without-square-root/compare-distance-without-square-root.zip)

For a range check, compare squared distance with squared radius.

```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;
var d2 = Vector3D.DistanceSquared(
    new(0, 0, 0), new(3, 4, 0));
double radius = 5;
var inRange = d2 <= radius * radius;
Console.WriteLine(inRange);
```

Unzip [the example](https://recursiveai.org/onemath/cookbook/compare-distance-without-square-root/compare-distance-without-square-root.zip), then `dotnet run`.

```text
True
```

The point is on the boundary: both squares are 25. Comparing squared distance with the squared radius skips a square root for each point and gives the same yes-or-no answer for inputs like these.

## The full program

```csharp
using RecursiveAI.OneMath;

Vector3D center = new(0, 0, 0);
const double radius = 5;
double radiusSquared = radius * radius;

foreach (Vector3D point in new Vector3D[]
         { new(3, 3, 0), new(3, 4, 0), new(3, 5, 0) })
{
    double squared = Vector3D.DistanceSquared(center, point);
    Console.WriteLine($"{squared}: {squared <= radiusSquared}");
}
```

`dotnet run` prints:

```text
18: True
25: True
34: False
```

Reject negative or nonfinite radii before squaring; a negative radius would otherwise give the same threshold as its positive counterpart.

## Variations

Pick the nearer of two candidates by comparing their squared distances to the same origin:

```csharp
using RecursiveAI.OneMath;

var origin = new Vector3D(0, 0, 0);
var first = new Vector3D(3, 4, 0);
var second = new Vector3D(2, 2, 0);
double firstSquared = Vector3D.DistanceSquared(origin, first);
double secondSquared = Vector3D.DistanceSquared(origin, second);
Console.WriteLine(secondSquared.CompareTo(firstSquared));
```

```text
-1
```

## Related recipes

- [Vector and quaternion math](https://recursiveai.org/onemath/cookbook/vector-and-quaternion-math/)
- [Calculate an overflow-safe hypotenuse](https://recursiveai.org/onemath/cookbook/calculate-overflow-safe-hypotenuse/)

## The short answer

Use `Vector3D.DistanceSquared(origin, point) <= radius * radius` with a finite, nonnegative radius and modest finite coordinates. Both sides of the comparison are in squared units. The inclusive comparison counts points on the boundary; use `<` when you want the interior only.

## What is the difference between squared Euclidean distance and Euclidean distance?

Subtract the two positions component by component, then add the squares of the X, Y, and Z differences. That sum is the squared Euclidean distance. Taking its square root gives the ordinary Euclidean distance, in the same units as the coordinates. [OneMath’s `Vector3D` source](https://github.com/recursive-ai-oss/onemath-csharp/blob/1986f32ecb9f5f3001c0121484f104c489720334/src/OneMath/Types/Vector3D.cs#L92-L103) shows the two methods and where they differ.

The point `(3, 4, 0)` is five units from the origin because 3² + 4² is 25. Comparing 25 with 5 would mix squared units and ordinary units and incorrectly reject that point. Square the radius as well. In real arithmetic, squaring nonnegative values preserves their order, so the range test answers the same geometric question without computing the distance itself. The [point-versus-sphere explanation on MDN](https://developer.mozilla.org/en-US/docs/Games/Techniques/3D_collision_detection#point_vs._sphere) gives the same squared-threshold construction.

## How do I compare distances from one point to several others in C#?

Use the same center and coordinate units for all candidates. If you’re testing a fixed radius, compute the squared threshold once, as the full program does. The outputs correspond to a point inside, a point on the boundary, and a point outside. If you’re looking for the nearest of several points instead, compare squared distances to each other as in the variation. It prints `-1` because the second candidate’s squared distance is smaller. Decide separately what to do when the values tie. Use `Distance` if the result needs to be a length rather than a ranking or a yes/no answer.

The operations are floating-point, not exact real arithmetic. Very large differences can overflow when squared, and very small ones can underflow; two distinct lengths can also round to the same comparison value. This recipe assumes inputs in a range where the intermediate squares and their sum are usable. It isn’t an exact boundary classifier for points separated from the threshold by less than the rounding error.
