浏览代码

Merge pull request #5005 from Kelimion/fix-5004

Fix #5004
Jeroen van Rijn 5 月之前
父节点
当前提交
e651803045
共有 2 个文件被更改,包括 37 次插入0 次删除
  1. 4 0
      src/exact_value.cpp
  2. 33 0
      tests/internal/test_5004_nan_constant_comparison.odin

+ 4 - 0
src/exact_value.cpp

@@ -954,6 +954,10 @@ gb_internal bool compare_exact_values(TokenKind op, ExactValue x, ExactValue y)
 	case ExactValue_Float: {
 		f64 a = x.value_float;
 		f64 b = y.value_float;
+		if (isnan(a) || isnan(b)) {
+			return false; // Fixes #5004
+		}
+
 		switch (op) {
 		case Token_CmpEq: return cmp_f64(a, b) == 0;
 		case Token_NotEq: return cmp_f64(a, b) != 0;

+ 33 - 0
tests/internal/test_5004_nan_constant_comparison.odin

@@ -0,0 +1,33 @@
+package test_internal
+
+import "core:testing"
+
+@(test)
+compare_constant_nans_f32 :: proc(t: ^testing.T) {
+	NaN  :: f32(0h7fc0_0000)
+	NaN2 :: f32(0h7fc0_0001)
+
+	testing.expect_value(t, NaN == NaN,  false)
+	testing.expect_value(t, NaN == NaN2, false)
+	testing.expect_value(t, NaN != NaN,  false)
+	testing.expect_value(t, NaN != NaN2, false)
+	testing.expect_value(t, NaN <  NaN,  false)
+	testing.expect_value(t, NaN <= NaN,  false)
+	testing.expect_value(t, NaN >  NaN,  false)
+	testing.expect_value(t, NaN >= NaN,  false)
+}
+
+@(test)
+compare_constant_nans_f64 :: proc(t: ^testing.T) {
+	NaN  :: f64(0h7fff_0000_0000_0000)
+	NaN2 :: f64(0h7fff_0000_0000_0001)
+
+	testing.expect_value(t, NaN == NaN,  false)
+	testing.expect_value(t, NaN == NaN2, false)
+	testing.expect_value(t, NaN != NaN,  false)
+	testing.expect_value(t, NaN != NaN2, false)
+	testing.expect_value(t, NaN <  NaN,  false)
+	testing.expect_value(t, NaN <= NaN,  false)
+	testing.expect_value(t, NaN >  NaN,  false)
+	testing.expect_value(t, NaN >= NaN,  false)
+}