Skip to content

Commit b3891e8

Browse files
committed
Use lambda to simplify code
1 parent b5ee909 commit b3891e8

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

tinyexpr.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -397,25 +397,29 @@ namespace te_builtins
397397
static te_type te_sum(te_type val1, te_type val2, te_type val3, te_type val4, te_type val5,
398398
te_type val6, te_type val7)
399399
{
400-
return (!std::isfinite(val1) ? 0 : val1) + (!std::isfinite(val2) ? 0 : val2) +
401-
(!std::isfinite(val3) ? 0 : val3) + (!std::isfinite(val4) ? 0 : val4) +
402-
(!std::isfinite(val5) ? 0 : val5) + (!std::isfinite(val6) ? 0 : val6) +
403-
(!std::isfinite(val7) ? 0 : val7);
400+
const auto getSumMaybeNan = [](const auto val) { return (!std::isfinite(val) ? 0 : val); };
401+
402+
return getSumMaybeNan(val1) + getSumMaybeNan(val2) +
403+
getSumMaybeNan(val3)+ getSumMaybeNan(val4) +
404+
getSumMaybeNan(val5) + getSumMaybeNan(val6) +
405+
getSumMaybeNan(val7);
404406
}
405407

406408
[[nodiscard]]
407409
static te_type te_average(te_type val1, te_type val2, te_type val3, te_type val4, te_type val5,
408410
te_type val6, te_type val7)
409411
{
410-
const auto validN = (!std::isfinite(val1) ? 0 : 1) + (!std::isfinite(val2) ? 0 : 1) +
411-
(!std::isfinite(val3) ? 0 : 1) + (!std::isfinite(val4) ? 0 : 1) +
412-
(!std::isfinite(val5) ? 0 : 1) + (!std::isfinite(val6) ? 0 : 1) +
413-
(!std::isfinite(val7) ? 0 : 1);
412+
const auto isValidMaybeNan = [](const auto val) { return (!std::isfinite(val) ? 0 : 1); };
413+
414+
const auto validN = isValidMaybeNan(val1) + isValidMaybeNan(val2) +
415+
isValidMaybeNan(val3) + isValidMaybeNan(val4) +
416+
isValidMaybeNan(val5) + isValidMaybeNan(val6) +
417+
isValidMaybeNan(val7);
414418
const auto total = te_sum(val1, val2, val3, val4, val5, val6, val7);
415419
return te_divide(total, static_cast<te_type>(validN));
416420
}
417421

418-
/// @warning This version of round emulates Excel behavior of supporting
422+
/// @warning This version of round emulates Excel's behavior of supporting
419423
/// negative decimal places (e.g., ROUND(21.5, -1) = 20). Be aware
420424
/// of that if using this function outside of TinyExpr++.
421425
[[nodiscard]]

0 commit comments

Comments
 (0)