Skip to content

TensorPrimitives.MinNumber(ReadOnlySpan<T>) incorrectly propagates NaN #133346

Description

@Sebtous

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:

1
NaN

Expected behavior

TensorPrimitives.MinNumber(values) should return:

1

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions