Skip to content

Commit 5b2c63d

Browse files
authored
Merge pull request #236 from scratchcpp/fix_zero_and_nan_comparison
Fix #198: Fix comparison of 0 with NaN
2 parents 653236e + cb4b2fe commit 5b2c63d

File tree

3 files changed

+12
-11
lines changed

3 files changed

+12
-11
lines changed

include/scratchcpp/value.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -553,9 +553,12 @@ class LIBSCRATCHCPP_EXPORT Value
553553
return doubleToString(d1) == v2.toString();
554554
else
555555
return stringsEqual(v1.toUtf16(), v2.toUtf16());
556-
} else if (v1.isNumber() || v2.isNumber())
557-
return v1.toDouble() == v2.toDouble();
558-
else if (v1.isBool() || v2.isBool())
556+
} else if (v1.isNumber() || v2.isNumber()) {
557+
if (static_cast<int>(v1.m_type) < 0 || static_cast<int>(v2.m_type) < 0)
558+
return false;
559+
else
560+
return v1.toDouble() == v2.toDouble();
561+
} else if (v1.isBool() || v2.isBool())
559562
return ((v1.m_type != Type::NaN && v2.m_type != Type::NaN) && (v1.toBool() == v2.toBool()));
560563
else
561564
return false;

src/blocks/operatorblocks.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ unsigned int OperatorBlocks::op_ln(VirtualMachine *vm)
257257
const Value &v = *vm->getInput(0, 1);
258258
if (v < 0)
259259
vm->replaceReturnValue(Value(Value::SpecialValue::NaN), 1);
260-
else if (v == 0)
260+
else if (v == 0 || v.isNaN())
261261
vm->replaceReturnValue(Value(Value::SpecialValue::NegativeInfinity), 1);
262262
else if (!v.isInfinity())
263263
vm->replaceReturnValue(std::log(v.toDouble()), 1);
@@ -269,7 +269,7 @@ unsigned int OperatorBlocks::op_log(VirtualMachine *vm)
269269
const Value &v = *vm->getInput(0, 1);
270270
if (v < 0)
271271
vm->replaceReturnValue(Value(Value::SpecialValue::NaN), 1);
272-
else if (v == 0)
272+
else if (v == 0 || v.isNaN())
273273
vm->replaceReturnValue(Value(Value::SpecialValue::NegativeInfinity), 1);
274274
else if (!v.isInfinity())
275275
vm->replaceReturnValue(std::log10(v.toDouble()), 1);

test/scratch_classes/value_test.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,9 +1873,8 @@ TEST(ValueTest, EqualityOperators)
18731873
ASSERT_FALSE(v1 == v5);
18741874
ASSERT_TRUE(v1 != v5);
18751875

1876-
// TODO: Enable this after #198 is fixed
1877-
/*ASSERT_FALSE(v2 == v5);
1878-
ASSERT_TRUE(v2 != v5);*/
1876+
ASSERT_FALSE(v2 == v5);
1877+
ASSERT_TRUE(v2 != v5);
18791878
}
18801879

18811880
{
@@ -2042,8 +2041,7 @@ TEST(ValueTest, EqualityOperators)
20422041
ASSERT_FALSE(v3 == v7);
20432042
ASSERT_TRUE(v3 != v7);
20442043

2045-
// TODO: Enable this after #198 is fixed
2046-
/*ASSERT_FALSE(v4 == v7);
2047-
ASSERT_TRUE(v4 != v7);*/
2044+
ASSERT_FALSE(v4 == v7);
2045+
ASSERT_TRUE(v4 != v7);
20482046
}
20492047
}

0 commit comments

Comments
 (0)