Skip to content

max gives incorrect results for floats when not optimised #5647

Closed

Description

@simoncowen88

FSharp.Core.Operators.max may return the incorrect result when one of it operands is nan.

Repro steps

let m a b = max a b

let x = m 7. nan
let y = max 7. nan

Without optimisations, x = 7. and y = nan.
With optimisations, x = nan and y = nan.

This leads to inconsitent results when code is executed in Debug vs Release.
Moreover, combined with #5646 this can lead to the code being incorrect even with optimisations enabled, with the following example exhibiting this:

let m A = fun b -> max A b

This appears to be caused by a difference between the behaviour of Microsoft.FSharp.Core.LanguagePrimitives/HashCompare::GenericLessThanIntrinsic and System.Math::Max, with the bug arising when the code cannot be optimised to use the latter.

Expected behavior

max should give consistent results whether optimisation are enabled or not.

Actual behavior

max can give different results (for floats when one operand is nan) depending on whether optimisations are enabled.

Known workarounds

None known.

Related information

  • Windows 10
  • .NET Framework 4.6.1
  • F# Compiler version 14.0.23020.0

Activity

  1. dsyme commented on Sep 17, 2018

    @dsyme
    Contributor

    The problem is the fallback generic implementation of max here is incorrect for NaN values. This implementation is used when types are unknown:

                [<CompiledName("Max")>]
                let inline max (e1: ^T) (e2: ^T) = 
                    (if e1 < e2 then e2 else e1)
                    when ^T : float         = (System.Math.Max : float * float -> float)(retype<_,float> e1, retype<_,float> e2)
                    when ^T : float32       = (System.Math.Max : float32 * float32 -> float32)(retype<_,float32> e1, retype<_,float32> e2)
    

    I suppose we do something like this in the worst case:

    let c = compare e1 e2 
    if (test-for-negative c, e.g.  c &&& 0x80000000 = 0x80000000) then e2 else e1
    

    or some better version of that. However we should add type-specialized versions for all of the non-floating point primitive types.

    Likewise for min.

  2. added this to the 16.0 milestone on Sep 18, 2018
  3. added
    Impact-High
    Impact-Medium
    and removed
    Impact-High
    on Oct 1, 2018
  4. modified the milestones: 16.0, 16.1 on Feb 21, 2019
  5. modified the milestones: 16.1, 16.2 on Apr 23, 2019
  6. modified the milestones: 16.2, Backlog on Apr 30, 2019
  7. added
    Area-Library
    and removed on Apr 1, 2022
  8. added a commit that references this issue on Jul 24, 2022
  9. github-actions commented on Mar 16, 2026

    @github-actions
    Contributor

    🤖 This is an automated response from Repo Assist.

    I tested this issue against the current codebase and it appears to be fixed. The original bug reported that let m a b = max a b; m 7. nan returned 7. (not nan) when unoptimized, while max 7. nan returned nan.

    Test result (unoptimized FSI):

    let m a b = max a b
    let x = m 7. nan   // now returns: nan ✓
    let y = max 7. nan  // returns: nan ✓
    // Both consistent: true ✓

    The fix appears to have been applied via SRTP specializations in prim-types.fs — both GenericMaximum and Operators.max now have when 'T : float = Math.Max(...) and when 'T : float32 = Math.Max(...) clauses, ensuring System.Math.Max is used for float types regardless of optimization level.

    Maintainers may want to close this issue if the fix has been fully verified.

    Generated by Repo Assist · ◷

    To install this agentic workflow, run

    gh aw add githubnext/agentics/workflows/repo-assist.md@9135cdfde26838a01779aa966628308404ec1f02
    
  10. dsyme commented on Mar 17, 2026

    @dsyme
    Contributor

    @T-Gro Yay for Repo Assist

  11. auduchinok commented on Mar 23, 2026

    @auduchinok
    Member

    @dsyme Should a test case be added?

  12. T-Gro commented on Mar 24, 2026

    @T-Gro
    Member

    Related test:

    [<Fact>]
    member this.MaxMinNan() =
    Assert.True(Double.IsNaN(max nan 1.0))
    Assert.True(Double.IsNaN(max 1.0 nan))
    Assert.True(Double.IsNaN(max nan nan))
    Assert.True(Single.IsNaN(max Single.NaN 1.0f))
    Assert.True(Single.IsNaN(max 1.0f Single.NaN))
    Assert.True(Single.IsNaN(max Single.NaN Single.NaN))
    Assert.True(Double.IsNaN(min nan 1.0))
    Assert.True(Double.IsNaN(min 1.0 nan))
    Assert.True(Double.IsNaN(min nan nan))
    Assert.True(Single.IsNaN(min Single.NaN 1.0f))
    Assert.True(Single.IsNaN(min 1.0f Single.NaN))
    Assert.True(Single.IsNaN(min Single.NaN Single.NaN))

    The MaxMinNan test verifies that max and min return NaN consistently for both Double and Single when NaN is involved, covering both argument orderings (max nan 1.0 and max 1.0 nan).

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

    AI-thinks-issue-fixedArea-LibraryBugImpact-Medium

    Type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions