Skip to content

Commit c36984b

Browse files
committed
Optimize value_mod()
1 parent 3f651dc commit c36984b

File tree

1 file changed

+3
-11
lines changed

1 file changed

+3
-11
lines changed

src/scratch/value_functions.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -354,17 +354,9 @@ extern "C"
354354
/*! Calculates the modulo the given values and writes the result to dst. */
355355
void value_mod(const libscratchcpp::ValueData *v1, const libscratchcpp::ValueData *v2, ValueData *dst)
356356
{
357-
double a = value_toDouble(v1);
358-
double b = value_toDouble(v2);
359-
360-
if ((b == 0) || std::isinf(a))
361-
value_assign_double(dst, std::numeric_limits<double>::quiet_NaN());
362-
else if (std::isinf(b))
363-
value_assign_double(dst, value_toDouble(v1));
364-
else if (value_isNegative(v1) || value_isNegative(v2))
365-
value_assign_double(dst, fmod(value_toDouble(v2) + fmod(value_toDouble(v1), -value_toDouble(v2)), value_toDouble(v2)));
366-
else
367-
value_assign_double(dst, fmod(value_toDouble(v1), value_toDouble(v2)));
357+
const double a = value_toDouble(v1);
358+
const double b = value_toDouble(v2);
359+
value_assign_double(dst, fmod(a, b) / b < 0.0 ? fmod(a, b) + b : fmod(a, b));
368360
}
369361

370362
/* comparison */

0 commit comments

Comments
 (0)