Description
TensorPrimitives.MinNumber(ReadOnlySpan<T>) returns NaN when any element of the span is NaN, even when other elements are numbers.
This appears inconsistent with MinNumber semantics, where a numeric operand should be preferred over NaN.
Reproduction Steps
using System;
using System.Numerics.Tensors;
float[] values = [1f, float.NaN, 2f];
Console.WriteLine(float.MinNumber(1f, float.NaN));
Console.WriteLine(TensorPrimitives.MinNumber(values));
Output:
Expected behavior
TensorPrimitives.MinNumber(values) should return:
A NaN should be ignored when another numeric value is available, consistent with float.MinNumber / T.MinNumber.
If all elements are NaN, returning NaN would be expected.
Actual behavior
TensorPrimitives.MinNumber(values) returns NaN as soon as the reduction encounters a NaN.
Regression?
Unknown
Known Workarounds
Perform the reduction manually using T.MinNumber:
T result = values[0];
for (int i = 1; i < values.Length; i++)
{
result = T.MinNumber(result, values[i]);
}
Configuration
- .NET 10
- Appears to be independent of OS and architecture.
Other information
The scalar reduction overload currently delegates to:
MinMaxCore<T, MinNumberOperator<T>>(x)
However, MinMaxCore contains explicit T.IsNaN(...) checks that return the encountered NaN immediately. This prevents MinNumberOperator<T>, which delegates to T.MinNumber(x, y), from applying MinNumber semantics.
MaxNumber(ReadOnlySpan<T>) appears to use the same reduction mechanism and may be affected by the equivalent issue.
Description
TensorPrimitives.MinNumber(ReadOnlySpan<T>)returnsNaNwhen any element of the span isNaN, even when other elements are numbers.This appears inconsistent with
MinNumbersemantics, where a numeric operand should be preferred overNaN.Reproduction Steps
Output:
Expected behavior
TensorPrimitives.MinNumber(values)should return:A
NaNshould be ignored when another numeric value is available, consistent withfloat.MinNumber/T.MinNumber.If all elements are
NaN, returningNaNwould be expected.Actual behavior
TensorPrimitives.MinNumber(values)returnsNaNas soon as the reduction encounters aNaN.Regression?
Unknown
Known Workarounds
Perform the reduction manually using
T.MinNumber:Configuration
Other information
The scalar reduction overload currently delegates to:
However,
MinMaxCorecontains explicitT.IsNaN(...)checks that return the encounteredNaNimmediately. This preventsMinNumberOperator<T>, which delegates toT.MinNumber(x, y), from applyingMinNumbersemantics.MaxNumber(ReadOnlySpan<T>)appears to use the same reduction mechanism and may be affected by the equivalent issue.