Skip to content

Commit

Permalink
Formula engine: fix abs() and modulus not working on decimals
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Mar 18, 2016
1 parent fe05f51 commit c1ca72f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/formula_function.cpp
Expand Up @@ -165,8 +165,14 @@ class abs_function : public function_expression {

private:
variant execute(const formula_callable& variables, formula_debugger *fdb) const {
const int n = args()[0]->evaluate(variables,fdb).as_int();
return variant(n >= 0 ? n : -n);
const variant input = args()[0]->evaluate(variables,fdb);
if(input.is_decimal()) {
const int n = input.as_decimal();
return variant(n >= 0 ? n : -n, variant::DECIMAL_VARIANT);
} else {
const int n = input.as_int();
return variant(n >= 0 ? n : -n);
}
}
};

Expand Down
22 changes: 16 additions & 6 deletions src/variant.cpp
Expand Up @@ -696,13 +696,23 @@ variant variant::operator/(const variant& v) const

variant variant::operator%(const variant& v) const
{
const int numerator = as_int();
const int denominator = v.as_int();
if(denominator == 0) {
throw type_error((formatter() << "divide by zero error").str());
}
if(type_ == TYPE_DECIMAL || v.type_ == TYPE_DECIMAL) {
const int numerator = as_decimal();
const int denominator = v.as_decimal();
if(denominator == 0) {
throw type_error((formatter() << "divide by zero error").str());
}

return variant(numerator%denominator, DECIMAL_VARIANT);
} else {
const int numerator = as_int();
const int denominator = v.as_int();
if(denominator == 0) {
throw type_error((formatter() << "divide by zero error").str());
}

return variant(numerator%denominator);
return variant(numerator%denominator);
}
}


Expand Down

0 comments on commit c1ca72f

Please sign in to comment.