You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
max gives incorrect results for floats when not optimised #5647
FSharp.Core.Operators.max may return the incorrect result when one of it operands is nan.
Repro steps
letm a b = max a b
letx= m 7. nan
lety= 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:
letm 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.
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):
letm a b = max a b
letx= m 7. nan // now returns: nan ✓lety= 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.
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).
Activity
dsyme commentedon Sep 17, 2018
The problem is the fallback generic implementation of
maxhere is incorrect for NaN values. This implementation is used when types are unknown:I suppose we do something like this in the worst case:
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.
Adding tests for showing dotnet#5647, difference in max/min in debug …
github-actions commentedon Mar 16, 2026
🤖 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. nanreturned7.(notnan) when unoptimized, whilemax 7. nanreturnednan.Test result (unoptimized FSI):
The fix appears to have been applied via SRTP specializations in
prim-types.fs— bothGenericMaximumandOperators.maxnow havewhen 'T : float = Math.Max(...)andwhen 'T : float32 = Math.Max(...)clauses, ensuringSystem.Math.Maxis used for float types regardless of optimization level.Maintainers may want to close this issue if the fix has been fully verified.
dsyme commentedon Mar 17, 2026
@T-Gro Yay for Repo Assist
auduchinok commentedon Mar 23, 2026
@dsyme Should a test case be added?
T-Gro commentedon Mar 24, 2026
Related test:
fsharp/tests/FSharp.Core.UnitTests/FSharp.Core/PrimTypes.fs
Lines 79 to 95 in d04f46f
The
MaxMinNantest verifies thatmaxandminreturn NaN consistently for bothDoubleandSinglewhen NaN is involved, covering both argument orderings (max nan 1.0andmax 1.0 nan).