Skip to content

Commit 3ee3065

Browse files
committed
Value: Fix special values in divide operator
1 parent bea9911 commit 3ee3065

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/scratch/value.h

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,17 @@ class LIBSCRATCHCPP_EXPORT Value : public ValueVariant
228228
m_type = Type::NaN;
229229
return;
230230
} else if (v.toDouble() == 0) {
231-
if (v.m_type == Type::Infinity || v.m_type == Type::NegativeInfinity) {
232-
if (m_type == Type::Infinity || m_type == Type::NegativeInfinity)
233-
m_type = Type::NaN;
234-
else
235-
*this = 0;
236-
} else
237-
m_type = toDouble() > 0 ? Type::Infinity : Type::NegativeInfinity;
231+
m_type = *this > 0 ? Type::Infinity : Type::NegativeInfinity;
232+
return;
233+
} else if ((m_type == Type::Infinity || m_type == Type::NegativeInfinity) && (v.m_type == Type::Infinity || v.m_type == Type::NegativeInfinity)) {
234+
m_type = Type::NaN;
235+
return;
236+
} else if (m_type == Type::Infinity || m_type == Type::NegativeInfinity) {
237+
if (v.toDouble() < 0)
238+
m_type = m_type == Type::Infinity ? Type::NegativeInfinity : Type::Infinity;
239+
return;
240+
} else if (v.m_type == Type::Infinity || v.m_type == Type::NegativeInfinity) {
241+
*this = 0;
238242
return;
239243
}
240244
*this = toDouble() / v.toDouble();
@@ -484,14 +488,17 @@ class LIBSCRATCHCPP_EXPORT Value : public ValueVariant
484488
{
485489
if ((v1 == 0) && (v2 == 0))
486490
return Value(SpecialValue::NaN);
487-
else if (v2 == 0) {
488-
if (v2.m_type == Type::Infinity || v2.m_type == Type::NegativeInfinity) {
489-
if (v1.m_type == Type::Infinity || v1.m_type == Type::NegativeInfinity)
490-
return Value(SpecialValue::NaN);
491-
else
492-
return 0;
493-
} else
494-
return Value(v1 > 0 ? SpecialValue::Infinity : SpecialValue::NegativeInfinity);
491+
else if (v2.toDouble() == 0)
492+
return v1 > 0 ? Value(SpecialValue::Infinity) : Value(SpecialValue::NegativeInfinity);
493+
else if ((v1.m_type == Type::Infinity || v1.m_type == Type::NegativeInfinity) && (v2.m_type == Type::Infinity || v2.m_type == Type::NegativeInfinity)) {
494+
return Value(SpecialValue::NaN);
495+
} else if (v1.m_type == Type::Infinity || v1.m_type == Type::NegativeInfinity) {
496+
if (v2.toDouble() < 0)
497+
return v1.m_type == Type::Infinity ? Value(SpecialValue::NegativeInfinity) : Value(SpecialValue::Infinity);
498+
else
499+
return v1.m_type == Type::Infinity ? Value(SpecialValue::Infinity) : Value(SpecialValue::NegativeInfinity);
500+
} else if (v2.m_type == Type::Infinity || v2.m_type == Type::NegativeInfinity) {
501+
return 0;
495502
}
496503
return v1.toDouble() / v2.toDouble();
497504
}

0 commit comments

Comments
 (0)