Skip to content

Fix #198: Fix comparison of 0 with NaN #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions include/scratchcpp/value.h
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,12 @@ class LIBSCRATCHCPP_EXPORT Value
return doubleToString(d1) == v2.toString();
else
return stringsEqual(v1.toUtf16(), v2.toUtf16());
} else if (v1.isNumber() || v2.isNumber())
return v1.toDouble() == v2.toDouble();
else if (v1.isBool() || v2.isBool())
} else if (v1.isNumber() || v2.isNumber()) {
if (static_cast<int>(v1.m_type) < 0 || static_cast<int>(v2.m_type) < 0)
return false;
else
return v1.toDouble() == v2.toDouble();
} else if (v1.isBool() || v2.isBool())
return ((v1.m_type != Type::NaN && v2.m_type != Type::NaN) && (v1.toBool() == v2.toBool()));
else
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/blocks/operatorblocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ unsigned int OperatorBlocks::op_ln(VirtualMachine *vm)
const Value &v = *vm->getInput(0, 1);
if (v < 0)
vm->replaceReturnValue(Value(Value::SpecialValue::NaN), 1);
else if (v == 0)
else if (v == 0 || v.isNaN())
vm->replaceReturnValue(Value(Value::SpecialValue::NegativeInfinity), 1);
else if (!v.isInfinity())
vm->replaceReturnValue(std::log(v.toDouble()), 1);
Expand All @@ -269,7 +269,7 @@ unsigned int OperatorBlocks::op_log(VirtualMachine *vm)
const Value &v = *vm->getInput(0, 1);
if (v < 0)
vm->replaceReturnValue(Value(Value::SpecialValue::NaN), 1);
else if (v == 0)
else if (v == 0 || v.isNaN())
vm->replaceReturnValue(Value(Value::SpecialValue::NegativeInfinity), 1);
else if (!v.isInfinity())
vm->replaceReturnValue(std::log10(v.toDouble()), 1);
Expand Down
10 changes: 4 additions & 6 deletions test/scratch_classes/value_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1873,9 +1873,8 @@ TEST(ValueTest, EqualityOperators)
ASSERT_FALSE(v1 == v5);
ASSERT_TRUE(v1 != v5);

// TODO: Enable this after #198 is fixed
/*ASSERT_FALSE(v2 == v5);
ASSERT_TRUE(v2 != v5);*/
ASSERT_FALSE(v2 == v5);
ASSERT_TRUE(v2 != v5);
}

{
Expand Down Expand Up @@ -2042,8 +2041,7 @@ TEST(ValueTest, EqualityOperators)
ASSERT_FALSE(v3 == v7);
ASSERT_TRUE(v3 != v7);

// TODO: Enable this after #198 is fixed
/*ASSERT_FALSE(v4 == v7);
ASSERT_TRUE(v4 != v7);*/
ASSERT_FALSE(v4 == v7);
ASSERT_TRUE(v4 != v7);
}
}