Skip to content

Commit 2bae9cc

Browse files
committed
Fix NaN to double conversion
1 parent d37f312 commit 2bae9cc

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

src/scratch/value_functions.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,9 @@ extern "C"
222222
/*! Returns the double representation of the given value. */
223223
double value_toDouble(const libscratchcpp::ValueData *v)
224224
{
225-
if (v->type == ValueType::Number)
226-
return v->numberValue;
227-
else if (v->type == ValueType::Bool)
225+
if (v->type == ValueType::Number) {
226+
return std::isnan(v->numberValue) ? 0 : v->numberValue;
227+
} else if (v->type == ValueType::Bool)
228228
return v->boolValue;
229229
else if (v->type == ValueType::String)
230230
return value_stringToDouble(v->stringValue);

test/scratch_classes/value_test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,8 @@ TEST(ValueTest, ToDouble)
981981
ASSERT_TRUE(v.toDouble() < 0 && std::isinf(v.toDouble()));
982982
v = "NaN";
983983
ASSERT_EQ(v.toDouble(), 0.0);
984+
v = std::numeric_limits<double>::quiet_NaN();
985+
ASSERT_EQ(v.toDouble(), 0.0);
984986

985987
v = "something";
986988
ASSERT_EQ(v.toDouble(), 0.0);

0 commit comments

Comments
 (0)