---
title: "Calculate a grid path’s fixed-point cost in C#"
url: https://recursiveai.org/onepath/cookbook/price-grid-path-in-fixed-point/
description: "Calculate the fixed-point cost of a grid path in C#. Price adjacent steps in Q31.32 and see why skipping a cell makes OnePath refuse the path."
version: "1.2.0"
---

# Calculate a grid path’s fixed-point cost in C#

**OnePath** is a free, open-source pathfinding library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePath) · [GitHub](https://github.com/recursive-ai-oss/onepath-csharp) · [Docs](https://recursiveai.org/onepath/docs/) · [Download example (.zip)](https://recursiveai.org/onepath/cookbook/price-grid-path-in-fixed-point/price-grid-path-in-fixed-point.zip)

`TryGetFixedCost` prices a grid route in integer ticks. One Q31.32 tick is 1/2³² of a cost unit. OnePath returns the same integer cost on every platform for the same path and area costs, so a replay can compare costs recorded on another machine.

```shell
dotnet add package RecursiveAI.OnePath
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OnePath;

// '.' is an open cell: 2 rows × 3 columns.
var grid = Grid.FromText("...\n...");
Cell[] cells =
    [new(0, 0), new(1, 0), new(2, 1)];
if (grid.TryGetFixedCost(cells, out var cost))
    Console.WriteLine($"Q31.32: {cost}");
else
    Console.WriteLine("Path refused.");
```

Unzip [the example](https://recursiveai.org/onepath/cookbook/price-grid-path-in-fixed-point/price-grid-path-in-fixed-point.zip), then `dotnet run`.

```text
Q31.32: 10368968296
```

The grid has two open rows of three cells. The path goes one tile right, then one tile diagonally down and right.

## The full program

The download also tries passing the same endpoints with the middle cell removed. `TryGetFixedCost` returns `false` because a step from `(0, 0)` straight to `(2, 1)` isn’t adjacent. A step with an endpoint on a wall or outside the grid is refused too.

```csharp
using RecursiveAI.OnePath;

var grid = Grid.FromText("...\n...");
Cell[] cells = [new(0, 0), new(1, 0), new(2, 1)];
if (grid.TryGetFixedCost(cells, out long cost))
    Console.WriteLine($"Q31.32: {cost}");
else
    Console.WriteLine("Path refused.");

Cell[] gap = [cells[0], cells[^1]];
Console.WriteLine(grid.TryGetFixedCost(gap, out _)
    ? "Gap accepted." : "Gap refused.");
```

```text
Q31.32: 10368968296
Gap refused.
```

## Variations

**Price a straight-only route.** Replace the `cells` declaration at the top with these three cells; leave the cost call in place.

```csharp
Cell[] cells =
    [new(0, 0), new(1, 0), new(2, 0)];
```

```text
Q31.32: 8589934592
```

## Related recipes

- [Find a weighted grid path](https://recursiveai.org/onepath/cookbook/route-around-expensive-grid-terrain/)
- [Find any-angle grid paths](https://recursiveai.org/onepath/cookbook/any-angle-grid-pathfinding/)

## The short answer

Pass the ordered cells to `Grid.TryGetFixedCost` and keep the returned `long` if the call succeeds. In Q31.32, one whole unit is `4294967296` integer ticks. The sample’s straight step costs one unit and its diagonal step costs about 1.4142 units. Use the raw integer when comparing costs; if you display approximate decimal units, divide by `4294967296.0` rather than truncating with integer division.

## How do you use fixed-point math for a path cost in C#?

Let OnePath price each step, then compare or sum the integer results. This is different from summing the doubles in a route and rounding the final number once. The [grid docs](https://recursiveai.org/onepath/docs/grids/) specify that each step is “multiplied by 2^32 (exact) and rounded half to even, and the integers are added.” The returned integer is exact for that rounding rule, not an exact representation of an irrational distance. If you add results from multiple calls yourself, check for overflow there too.

Pass the cells of a route with each intervening tile present. An any-angle route can have a straight segment that spans several grid cells, and a list containing only its corners doesn’t specify which adjacent steps to price. This method doesn’t insert those cells or check whether your search options would allow every move; for instance, it has no corner-cutting setting. Find a route with the movement rules you want first, then price its adjacent steps.

## How is diagonal distance priced on a weighted grid?

OnePath averages the area multipliers on the two ends of a step. With weights of 1 and 3, a straight step costs 2 before scaling. For a diagonal, the library also multiplies by `1.4142135623730951`. That’s why the two routes in this recipe, each with two steps, have different costs even though every tile has the same weight. A route’s tile count isn’t its price.

The method reads the grid’s current area multipliers and doesn’t price a `StepCost` hook. If you change the area table after finding a route, you’re pricing it on the new table. Keep the raw cost with the route if you need the old figure; don’t expect to recover a custom search cost from the cells alone.
