Convert float32 tensor storage to float16 or bfloat16, then widen it to inspect the rounded values.
dotnet add package RecursiveAI.OneModelRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneModel.Tensors; using var x = Tensor.FromSpan<float>( [1.001f, 70000f], Shape.Create(2)); using var half = x.Cast(DType.F16); using var brain = x.Cast(DType.BF16); Console.WriteLine($"{half.DType}, {brain.DType}");
F16, BF16Unzip the example, then dotnet run.
The full program
The input is [1.001f, 70000f]. The program widens both results to float32 for printing. Infinity is printed as ∞ in this run. Narrowing overflow becomes infinity rather than throwing.
using RecursiveAI.OneModel.Tensors; using var tensor = Tensor.FromSpan<float>([1.001f, 70000f], Shape.Create(2)); using var half = tensor.Cast(DType.F16); using var brain = tensor.Cast(DType.BF16); using var halfBack = half.Cast(DType.F32); using var brainBack = brain.Cast(DType.F32); Console.WriteLine("float16: " + string.Join(", ", halfBack.ToArray<float>())); Console.WriteLine("bfloat16: " + string.Join(", ", brainBack.ToArray<float>()));
float16: 1.0009766, ∞ bfloat16: 1, 70144
Variations
To see the value at the largest finite binary16 magnitude, replace the full program’s input with this line; all other lines stay the same:
using var tensor = Tensor.FromSpan<float>([65504f, -65504f], Shape.Create(2));float16: 65504, -65504 bfloat16: 65536, -65536
Related recipes
The short answer
Use Tensor.Cast(DType.F16) for binary16 storage or Tensor.Cast(DType.BF16) for bfloat16 storage. Keep the returned tensors alive while using them and dispose them afterward. To inspect the numbers using ToArray<float>(), cast the stored tensor back to DType.F32 first. ToArray<T>() reads the tensor’s actual element type; it is not an implicit numeric conversion.
The example deliberately separates storage from calculation. It takes two float32 values, narrows each to both formats, and widens the results. The numeric-conversion reference defines nearest-even rounding for both narrowing operations. Widening preserves the stored value exactly; it cannot recover digits already discarded.
What is float16 and bfloat16?
Both store a floating-point number in two bytes. They divide those bits differently. Binary16 has more significand precision but less exponent range. Bfloat16 has the exponent width of float32, with fewer significand bits. The format comparison by Nick Higham gives the bit allocations and explains this tradeoff.
In the run above, the input near one becomes 1.0009766 after the binary16 round trip, while bfloat16 rounds it to 1. Neither format promises that an arbitrary float32 number will survive unchanged. Values already representable in the destination can survive, while nearby values may map to the same stored number.
A cast does not mutate the source into another type. Retain the returned value and use that value downstream. Each distinct converted tensor in the example has its own using declaration. This also makes the round trip explicit: the arrays printed are from the widened copies, not from the original input.
What is the bfloat16 range?
The OneModel precision guide describes bfloat16 as keeping the binary32 range with an eight-bit significand, while binary16 ends at 65504. This means comparable exponent range, not identical representable values at every boundary. The conversion rules still allow overflow to infinity.
The concrete distinction here is 70000: it exceeds binary16’s finite range and becomes infinity, but bfloat16 stores the rounded finite value 70144. Use inputs representative of your own tensors before choosing storage. An absence of infinity is not a guarantee of sufficient precision, and converting back to float32 does not undo the change.
Is bfloat16 better than float16?
The snippet at the top creates both 16-bit storage types; it does not request half-precision kernels.
Choose from the values you must preserve. Binary16 is useful when its finite range is sufficient and the additional significand precision matters. Bfloat16 is useful when the wider exponent range matters more. The boundary variation shows that a value exactly representable in binary16 can still round in bfloat16.
Storage type alone does not establish the arithmetic used by an operator. OneModel’s precision guide says that its general half-storage execution path widens inputs, runs kernels in binary32, then narrows outputs. This recipe converts tensors only; it neither configures compact graph weights nor claims that changing the storage enables half-precision computation.