System.Half
doesn't generate correct ordered comparison results when both sides are negative
#39770
Labels
Milestone
Description
The comparison methods / operators do not return the correct comparison result when both numbers are negative. For example,
-1 <= NegativeInfinity
returnstrue
, when it is obvious that -1 is greater thanNegativeInfinity
.Configuration
The code likely isn't specific to a certain H/W or OS. There's only software implementations of those comparison methods.
Regression?
This bug existed from the initial prototype code in dotnet/corefxlab.
Other information
The current implementation,
return (short)(left._value) <= (short)(right._value);
is incorrect when both sides are negative.Consider the following:
1 01111 0000000000
1 11111 0000000000
0 01111 0000000000
0 11111 0000000000
This implementation is correct for cases where both sides are positive, but when both sides are negative we get an inverted result. This likely is the same in all 4 ordered comparison operators and
CompareTo
method.The solution would be to invert the result when
leftIsNegative
is true. (XOR withleftIsNegative
?)runtime/src/libraries/System.Private.CoreLib/src/System/Half.cs
Lines 127 to 145 in 0e23ef4
P.S. Apparently we don't have any test that compares two negative values....
The text was updated successfully, but these errors were encountered: