Added new functions sgn(), int() and frac()

This commit is contained in:
Spixi 2016-05-21 16:32:07 +02:00
parent 68e5e7d7dc
commit 0365f31a4b
2 changed files with 52 additions and 0 deletions

View file

@ -275,6 +275,7 @@ Version 1.13.4+dev:
* pair() function that produces a key-value pair suitable for
passing to tomap() - this also means key-value pairs are now
serializable (relevant in FormulaAI)
* sgn(), int() and frac() functions for decimal numbers
* Bugfixes:
* Dice operator is now synced (where possible)
* Modulus (%) operator now works on decimal numbers

View file

@ -1318,7 +1318,55 @@ private:
}
};
class int_function : public function_expression {
public:
explicit int_function(const args_list& args)
: function_expression("int", args, 1, 1)
{}
private:
variant execute(const formula_callable& variables, formula_debugger *fdb) const {
variant decimal = args()[0]->evaluate(variables,fdb);
int d = decimal.as_int();
return variant( d );
}
};
class frac_function : public function_expression {
public:
explicit frac_function(const args_list& args)
: function_expression("frac", args, 1, 1)
{}
private:
variant execute(const formula_callable& variables, formula_debugger *fdb) const {
variant decimal = args()[0]->evaluate(variables,fdb);
int d = decimal.as_decimal();
d%=1000;
return variant( d, variant::DECIMAL_VARIANT );
}
};
class sgn_function : public function_expression {
public:
explicit sgn_function(const args_list& args)
: function_expression("sgn", args, 1, 1)
{}
private:
variant execute(const formula_callable& variables, formula_debugger *fdb) const {
variant decimal = args()[0]->evaluate(variables,fdb);
int d = decimal.as_decimal();
if( d != 0 ) {
d = d>0 ? 1 : -1;
}
return variant( d );
}
};
class as_decimal_function : public function_expression {
public:
@ -1541,6 +1589,9 @@ function_symbol_table& get_functions_map() {
FUNCTION(null);
FUNCTION(ceil);
FUNCTION(floor);
FUNCTION(int);
FUNCTION(frac);
FUNCTION(sgn);
FUNCTION(round);
FUNCTION(as_decimal);
FUNCTION(refcount);