Page MenuHomeFeedback Tracker

NaN float comparison false positive and false negative
Reviewed, NormalPublic

Description

After testing not a number (NaN) floats I found out that their boolean implicit conversion and or comparator operations are maybe not covered or bugged. Comparisons of nan to itself are correct, but to NaN vs real numbers fail as seen below.

float nan = "nan".ToFloat();
Print(nan);
Print("nan == nan: " + (nan == nan));
Print("nan != nan: " + (nan != nan));
Print("nan == 0: " + (nan == 0));
Print("nan != 0: " + (nan != 0));
Print("nan == 1337: " + (nan == 1337));
Print("nan != 1337: " + (nan != 1337));
Print("nan == -42: " + (nan == -42));
Print("nan != -42: " + (nan != -42));
SCRIPT       : float nan = nan
SCRIPT       : nan == nan: 1
SCRIPT       : nan != nan: 0
SCRIPT       : nan == 0: 1 //should be false
SCRIPT       : nan != 0: 0 // should be true
SCRIPT       : nan == 1337: 1 // should be false
SCRIPT       : nan != 1337: 0 //should be true
SCRIPT       : nan == -42: 1 //should be false
SCRIPT       : nan != -42: 0 // should be true

Details

Severity
Minor
Resolution
Open
Reproducibility
Always
Operating System
Windows 11 x64
Category
General

Event Timeline

Arkensor created this task.Apr 13 2023, 4:38 PM
Geez changed the task status from New to Assigned.Apr 14 2023, 10:46 AM
Geez changed the task status from Assigned to Reviewed.Apr 14 2023, 3:13 PM
Geez added a subscriber: Geez.

Hello Arkensor.
According to the devs:

Bhavior of NAN in script is not defined, so result is expected (and can be different on different platforms).

Same results are in "C++ MSVC with /fp:fast" which we use for Windows. By MSVC /fp:fast documentataion:

https://learn.microsoft.com/en-us/cpp/build/reference/fp-specify-floating-point-behavior?view=msvc-170&viewFallbackFrom=vs-2019

"Special values (NaN, +infinity, -infinity, -0.0) may not be propagated or behave strictly according to the IEEE-754 standard."

That is a fair point, I guess it is very niche, but perhaps worth 2 sentences in the data type biki page to explain that "NaN" exists but should not be used because of these issues.
The ticket can be closed. Thank you, Geez.