Skip to content

Commit fd9946e

Browse files
committed
Value: Add more methods for operations
1 parent e33c6ef commit fd9946e

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

src/scratch/value.h

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ class LIBSCRATCHCPP_EXPORT Value : public ValueVariant
166166
m_type = Type::Infinity;
167167
else if (m_type == Type::NegativeInfinity || v.m_type == Type::NegativeInfinity)
168168
m_type = Type::NegativeInfinity;
169+
return;
169170
}
170171
auto p1 = std::get_if<long>(this);
171172
auto p2 = std::get_if<long>(&v);
@@ -175,6 +176,69 @@ class LIBSCRATCHCPP_EXPORT Value : public ValueVariant
175176
*this = toDouble() + v.toDouble();
176177
}
177178

179+
/*! Subtracts the given value from the value. */
180+
inline void subtract(const Value &v)
181+
{
182+
if ((static_cast<int>(m_type) < 0) || (static_cast<int>(v.m_type) < 0)) {
183+
if ((m_type == Type::Infinity && v.m_type == Type::Infinity) || (m_type == Type::NegativeInfinity && v.m_type == Type::NegativeInfinity))
184+
m_type = Type::NaN;
185+
else if (m_type == Type::Infinity || v.m_type == Type::NegativeInfinity)
186+
m_type = Type::Infinity;
187+
else if (m_type == Type::NegativeInfinity || v.m_type == Type::Infinity)
188+
m_type = Type::NegativeInfinity;
189+
return;
190+
}
191+
auto p1 = std::get_if<long>(this);
192+
auto p2 = std::get_if<long>(&v);
193+
if (p1 && p2)
194+
*this = *p1 - *p2;
195+
else
196+
*this = toDouble() - v.toDouble();
197+
}
198+
199+
/*! Multiplies the given value with the value. */
200+
inline void multiply(const Value &v)
201+
{
202+
if ((static_cast<int>(m_type) < 0) || (static_cast<int>(v.m_type) < 0)) {
203+
if (m_type == Type::Infinity || m_type == Type::NegativeInfinity || v.m_type == Type::Infinity || v.m_type == Type::NegativeInfinity) {
204+
bool mode = (m_type == Type::Infinity || v.m_type == Type::Infinity);
205+
const Value &value = (m_type == Type::Infinity || m_type == Type::NegativeInfinity) ? v : *this;
206+
if (value > 0)
207+
m_type = mode ? Type::Infinity : Type::NegativeInfinity;
208+
else if (value < 0)
209+
m_type = mode ? Type::NegativeInfinity : Type::Infinity;
210+
else
211+
m_type = Type::NaN;
212+
return;
213+
}
214+
}
215+
auto p1 = std::get_if<long>(this);
216+
auto p2 = std::get_if<long>(&v);
217+
if (p1 && p2)
218+
*this = *p1 * *p2;
219+
else
220+
*this = toDouble() * v.toDouble();
221+
}
222+
223+
/*! Divides the value by the given value. */
224+
inline void divide(const Value &v)
225+
{
226+
if ((toDouble() == 0) && (v.toDouble() == 0)) {
227+
m_type = Type::NaN;
228+
return;
229+
} else if (v.toDouble() == 0) {
230+
if (v.m_type == Type::Infinity || v.m_type == Type::NegativeInfinity) {
231+
if (m_type == Type::Infinity || m_type == Type::NegativeInfinity)
232+
m_type = Type::NaN;
233+
else
234+
*this = 0;
235+
} else
236+
m_type = toDouble() > 0 ? Type::Infinity : Type::NegativeInfinity;
237+
return;
238+
}
239+
*this = toDouble() / v.toDouble();
240+
}
241+
178242
inline const Value &operator=(float v)
179243
{
180244
m_type = Type::Number;

0 commit comments

Comments
 (0)