Skip to content

Commit 44b264a

Browse files
committed
Fix compare double and floats
1 parent d1696c2 commit 44b264a

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/CLR/Core/CLR_RT_HeapBlock.cpp

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,29 @@ bool CLR_RT_HeapBlock::ObjectsEqual(
13801380
return Compare_Values(pArgLeft, pArgRight, false) == 0;
13811381
break;
13821382

1383+
// edge cases, in .NET a NaN is equal to another NaN
1384+
// https://learn.microsoft.com/en-us/dotnet/fundamentals/runtime-libraries/system-double-equals?WT.mc_id=DT-MVP-5004179#nan
1385+
case DATATYPE_R4:
1386+
if (__isnanf(pArgLeft.NumericByRefConst().r4) && __isnanf(pArgRight.NumericByRefConst().r4))
1387+
{
1388+
return true;
1389+
}
1390+
else
1391+
{
1392+
return Compare_Values(pArgLeft, pArgRight, false) == 0;
1393+
}
1394+
break;
1395+
case DATATYPE_R8:
1396+
if (__isnand((double)pArgLeft.NumericByRefConst().r8) && __isnand((double)pArgRight.NumericByRefConst().r8))
1397+
{
1398+
return true;
1399+
}
1400+
else
1401+
{
1402+
return Compare_Values(pArgLeft, pArgRight, false) == 0;
1403+
}
1404+
break;
1405+
13831406
case DATATYPE_BYREF:
13841407
if (rightDataType == DATATYPE_OBJECT)
13851408
{
@@ -1699,9 +1722,9 @@ CLR_INT32 CLR_RT_HeapBlock::Compare_Values(const CLR_RT_HeapBlock &left, const C
16991722
case DATATYPE_R4:
17001723

17011724
// deal with special cases:
1702-
// return 0 if the numbers are unordered (either or both are NaN)
1725+
// return 1 if the numbers are unordered (either or both are NaN)
17031726
// this is post processed in interpreter so '1' will turn into '0'
1704-
if (__isnand(left.NumericByRefConst().r4) && __isnand(right.NumericByRefConst().r4))
1727+
if (__isnanf(left.NumericByRefConst().r4) || __isnanf(right.NumericByRefConst().r4))
17051728
{
17061729
return 1;
17071730
}
@@ -1732,7 +1755,7 @@ CLR_INT32 CLR_RT_HeapBlock::Compare_Values(const CLR_RT_HeapBlock &left, const C
17321755
case DATATYPE_R8:
17331756

17341757
// deal with special cases:
1735-
// return 0 if the numbers are unordered (either or both are NaN)
1758+
// return 1 if the numbers are unordered (either or both are NaN)
17361759
// this is post processed in interpreter so '1' will turn into '0'
17371760
if (__isnand((double)left.NumericByRefConst().r8) || __isnand((double)right.NumericByRefConst().r8))
17381761
{

0 commit comments

Comments
 (0)