---
title: "Deterministic powers and cube roots in C#"
url: https://recursiveai.org/onemath/cookbook/deterministic-powers-and-cube-roots/
description: "Evaluate deterministic powers and real cube roots in C# with OneMath. Includes negative inputs, float results and a runnable bit-pattern check."
version: "1.0.0"
---

# Deterministic powers and cube roots 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/deterministic-powers-and-cube-roots/deterministic-powers-and-cube-roots.zip)

Use powers and real cube roots when identical floating-point inputs must produce identical result bits across platforms.

```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.Pow(2, 10));
Console.WriteLine(Math64.Cbrt(-27));
```

Prints:

```text
1024
-3
```

Unzip [the example](https://recursiveai.org/onemath/cookbook/deterministic-powers-and-cube-roots/deterministic-powers-and-cube-roots.zip), then `dotnet run`.

## The full program

```csharp
using RecursiveAI.OneMath;

Console.WriteLine(Math64.Pow(2, 10));
Console.WriteLine(Math64.Cbrt(-27));
double root = Math64.Cbrt(-2);
Console.WriteLine($"cbrt(-2) bits: {BitConverter.DoubleToUInt64Bits(root):X16}");
Console.WriteLine($"Matches expected: {BitConverter.DoubleToUInt64Bits(root) == 0xBFF428A2F98D728BUL}");
Console.WriteLine($"Negative fractional power: {Math64.Pow(-8, 1.0 / 3)}");
Console.WriteLine($"Float cube root: {Math32.Cbrt(-27f)}");
```

`dotnet run` prints:

```text
1024
-3
cbrt(-2) bits: BFF428A2F98D728B
Matches expected: True
Negative fractional power: NaN
Float cube root: -3
```

Domain errors return `NaN`, not an exception.

## Related recipes

- [Calculate an overflow-safe hypotenuse](https://recursiveai.org/onemath/cookbook/calculate-overflow-safe-hypotenuse/)
- [Test a point in a triangle robustly](https://recursiveai.org/onemath/cookbook/test-point-in-triangle-robustly/)

## The short answer

Use `Math64.Pow(x, y)` for a double-precision power and `Math64.Cbrt(x)` for a real cube root. Use `Math32` for a `float` result. The [elementary-function contract](https://recursiveai.org/onemath/docs/functions/) specifies correctly rounded results: round-to-nearest, ties-to-even, with gradual underflow and overflow to infinity. It states that results are bit-identical on every platform.

That contract concerns the actual floating-point arguments. It does not make a decimal measurement exact, or recover digits already lost while computing an argument. Keep input parsing and preceding arithmetic consistent too. The downloadable program checks a result’s binary representation so you can compare the actual value rather than the way a terminal formats it.

## How do I calculate the cube root of a negative number in C#?

Call `Cbrt` directly. The cube root of −27 is −3 because multiplying −3 by itself three times gives −27. This is a real result, not a complex-number calculation. The snippet at the top prints exactly `-3`.

Do not substitute a fractional power for a real cube-root operation on negative inputs. In the full program, `Math64.Pow(-8, 1.0 / 3)` returns `NaN`. The exponent supplied by that expression is a floating-point approximation, not the exact rational number one third. The real-valued power function’s negative-base domain and the real cube-root operation are different contracts.

If negative inputs are valid in your data, choose `Cbrt` before doing the calculation rather than trying a power and treating its `NaN` as a missing measurement. A dedicated cube root also states the operation you intend without encoding it as a rounded exponent.

## How can I make floating-point powers deterministic in .NET?

Use the same function and the same input bits throughout the calculation. In OneMath, `Math64.Pow(2, 10)` returns `1024`. The guarantee comes from the library’s documented function contract, not from this one exact-integer example. Correct rounding means selecting the representable floating-point value nearest the exact mathematical result; ties select the even significand.

The program’s noninteger example computes the cube root of −2 and compares its bits with `BFF428A2F98D728B`. `BitConverter.DoubleToUInt64Bits` exposes those bits as an integer, and `X16` prints the integer in hexadecimal. This avoids confusing display formatting with numerical identity. Different cultures can format the same value differently while the bits stay unchanged.

For float data, use `Math32` instead of casting a computed `Math64` result down to float. The docs specify that float inputs convert exactly to double for the internal calculation and only the final rounding differs. This makes the requested result precision explicit. Domain errors return canonical `double.NaN` or `float.NaN`; they do not require a try/catch around each power.
