Skip to content

Commit e1ad826

Browse files
committed
Fix conversion of NaN to bool
1 parent e6edce7 commit e1ad826

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/scratch/value_functions.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ extern "C"
238238
if (v->type == ValueType::Bool) {
239239
return v->boolValue;
240240
} else if (v->type == ValueType::Number) {
241-
return v->numberValue != 0;
241+
return v->numberValue != 0 && !std::isnan(v->numberValue);
242242
} else if (v->type == ValueType::String) {
243243
return value_stringToBool(v->stringValue);
244244
} else {

test/scratch_classes/value_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,15 @@ TEST(ValueTest, ToBool)
11521152
v = -2.54;
11531153
ASSERT_EQ(v.toBool(), true);
11541154

1155+
v = std::numeric_limits<double>::infinity();
1156+
ASSERT_EQ(v.toBool(), true);
1157+
1158+
v = -std::numeric_limits<double>::infinity();
1159+
ASSERT_EQ(v.toBool(), true);
1160+
1161+
v = std::numeric_limits<double>::quiet_NaN();
1162+
ASSERT_EQ(v.toBool(), false);
1163+
11551164
v = false;
11561165
ASSERT_EQ(v.toBool(), false);
11571166
v = true;

0 commit comments

Comments
 (0)