325 lines
13 KiB
C++
325 lines
13 KiB
C++
#include "engine/functions/helpers.hpp"
|
|
#include "engine/functions/registry.hpp"
|
|
#include "engine/workbook.hpp"
|
|
|
|
#include <cmath>
|
|
#include <random>
|
|
#include <variant>
|
|
|
|
namespace htab {
|
|
|
|
using namespace htab::fn;
|
|
|
|
namespace {
|
|
|
|
constexpr double kPi = 3.14159265358979323846;
|
|
|
|
template <typename UnaryMathFn>
|
|
EvalResult unary_math(const std::vector<std::shared_ptr<Expr>>& args,
|
|
const EvalContext& ctx,
|
|
UnaryMathFn&& fn) {
|
|
if (args.size() != 1) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto num = eval_number_arg(args[0], ctx, err);
|
|
if (!num) return err_res(err);
|
|
return EvalResult::from_value(fn(*num));
|
|
}
|
|
|
|
EvalResult fn_abs(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& ctx) {
|
|
return unary_math(a, ctx, [](double x) { return Value::make_checked_number(std::fabs(x)); });
|
|
}
|
|
EvalResult fn_acos(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& ctx) {
|
|
return unary_math(a, ctx, [](double x) {
|
|
if (x < -1.0 || x > 1.0) return Value::make_error(ErrorCode::Num);
|
|
return Value::make_checked_number(std::acos(x));
|
|
});
|
|
}
|
|
EvalResult fn_asin(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& ctx) {
|
|
return unary_math(a, ctx, [](double x) {
|
|
if (x < -1.0 || x > 1.0) return Value::make_error(ErrorCode::Num);
|
|
return Value::make_checked_number(std::asin(x));
|
|
});
|
|
}
|
|
EvalResult fn_atan(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& ctx) {
|
|
return unary_math(a, ctx, [](double x) { return Value::make_checked_number(std::atan(x)); });
|
|
}
|
|
EvalResult fn_atan2(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 2) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto x = eval_number_arg(args[0], ctx, err);
|
|
if (!x) return err_res(err);
|
|
auto y = eval_number_arg(args[1], ctx, err);
|
|
if (!y) return err_res(err);
|
|
if (*x == 0.0 && *y == 0.0) return err_res(ErrorCode::Div0);
|
|
return EvalResult::from_value(Value::make_checked_number(std::atan2(*y, *x)));
|
|
}
|
|
EvalResult fn_cos(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& ctx) {
|
|
return unary_math(a, ctx, [](double x) { return Value::make_checked_number(std::cos(x)); });
|
|
}
|
|
EvalResult fn_degrees(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& ctx) {
|
|
return unary_math(a, ctx, [](double x) { return Value::make_checked_number(x * 180.0 / kPi); });
|
|
}
|
|
EvalResult fn_exp(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& ctx) {
|
|
return unary_math(a, ctx, [](double x) { return Value::make_checked_number(std::exp(x)); });
|
|
}
|
|
EvalResult fn_fact(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& ctx) {
|
|
return unary_math(a, ctx, [](double x) {
|
|
if (x < 0.0) return Value::make_error(ErrorCode::Num);
|
|
int n = static_cast<int>(std::trunc(x));
|
|
if (n > 170) return Value::make_error(ErrorCode::Num);
|
|
double res = 1.0;
|
|
for (int i = 2; i <= n; ++i) res *= static_cast<double>(i);
|
|
return Value::make_checked_number(res);
|
|
});
|
|
}
|
|
EvalResult fn_int(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& ctx) {
|
|
return unary_math(a, ctx, [](double x) { return Value::make_checked_number(std::floor(x)); });
|
|
}
|
|
EvalResult fn_ln(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& ctx) {
|
|
return unary_math(a, ctx, [](double x) {
|
|
if (x <= 0.0) return Value::make_error(ErrorCode::Num);
|
|
return Value::make_checked_number(std::log(x));
|
|
});
|
|
}
|
|
EvalResult fn_log(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.empty() || args.size() > 2) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto num = eval_number_arg(args[0], ctx, err);
|
|
if (!num) return err_res(err);
|
|
double base = 10.0;
|
|
if (args.size() == 2) {
|
|
auto b = eval_number_arg(args[1], ctx, err);
|
|
if (!b) return err_res(err);
|
|
base = *b;
|
|
}
|
|
if (*num <= 0.0 || base <= 0.0 || base == 1.0) return err_res(ErrorCode::Num);
|
|
return EvalResult::from_value(Value::make_checked_number(std::log(*num) / std::log(base)));
|
|
}
|
|
EvalResult fn_log10(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& ctx) {
|
|
return unary_math(a, ctx, [](double x) {
|
|
if (x <= 0.0) return Value::make_error(ErrorCode::Num);
|
|
return Value::make_checked_number(std::log10(x));
|
|
});
|
|
}
|
|
EvalResult fn_mod(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 2) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto n = eval_number_arg(args[0], ctx, err);
|
|
if (!n) return err_res(err);
|
|
auto d = eval_number_arg(args[1], ctx, err);
|
|
if (!d) return err_res(err);
|
|
if (*d == 0.0) return err_res(ErrorCode::Div0);
|
|
double res = *n - *d * std::floor(*n / *d);
|
|
return EvalResult::from_value(Value::make_checked_number(res));
|
|
}
|
|
EvalResult fn_pi(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext&) {
|
|
if (!args.empty()) return err_res(ErrorCode::Value);
|
|
return EvalResult::from_value(Value::make_number(kPi));
|
|
}
|
|
EvalResult fn_power(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 2) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto x = eval_number_arg(args[0], ctx, err);
|
|
if (!x) return err_res(err);
|
|
auto y = eval_number_arg(args[1], ctx, err);
|
|
if (!y) return err_res(err);
|
|
if (*x == 0.0 && *y < 0.0) return err_res(ErrorCode::Div0);
|
|
return EvalResult::from_value(Value::make_checked_number(std::pow(*x, *y)));
|
|
}
|
|
EvalResult fn_product(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.empty()) return err_res(ErrorCode::Value);
|
|
std::vector<double> nums;
|
|
if (auto err = collect_numeric_args(args, ctx, nums)) return err_res(*err);
|
|
if (nums.empty()) return EvalResult::from_value(Value::make_number(0.0));
|
|
double prod = 1.0;
|
|
for (double v : nums) prod *= v;
|
|
return EvalResult::from_value(Value::make_checked_number(prod));
|
|
}
|
|
EvalResult fn_radians(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& ctx) {
|
|
return unary_math(a, ctx, [](double x) { return Value::make_checked_number(x * kPi / 180.0); });
|
|
}
|
|
EvalResult fn_rand(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext&) {
|
|
if (!args.empty()) return err_res(ErrorCode::Value);
|
|
thread_local std::mt19937_64 rng{std::random_device{}()};
|
|
thread_local std::uniform_real_distribution<double> dist(0.0, 1.0);
|
|
return EvalResult::from_value(Value::make_number(dist(rng)));
|
|
}
|
|
EvalResult fn_round(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() != 2) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto num = eval_number_arg(args[0], ctx, err);
|
|
if (!num) return err_res(err);
|
|
auto dig = eval_number_arg(args[1], ctx, err);
|
|
if (!dig) return err_res(err);
|
|
int d = static_cast<int>(std::trunc(*dig));
|
|
double x = *num;
|
|
double res = 0.0;
|
|
if (d >= 0) {
|
|
double scale = std::pow(10.0, d);
|
|
double v = x * scale;
|
|
if (!std::isfinite(v)) return EvalResult::from_value(Value::make_checked_number(x));
|
|
double r = (v >= 0.0) ? std::floor(v + 0.5) : -std::floor(-v + 0.5);
|
|
res = r / scale;
|
|
} else {
|
|
double scale = std::pow(10.0, -d);
|
|
double v = x / scale;
|
|
double r = (v >= 0.0) ? std::floor(v + 0.5) : -std::floor(-v + 0.5);
|
|
res = r * scale;
|
|
}
|
|
return EvalResult::from_value(Value::make_checked_number(res));
|
|
}
|
|
EvalResult fn_sin(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& ctx) {
|
|
return unary_math(a, ctx, [](double x) { return Value::make_checked_number(std::sin(x)); });
|
|
}
|
|
EvalResult fn_sqrt(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& ctx) {
|
|
return unary_math(a, ctx, [](double x) {
|
|
if (x < 0.0) return Value::make_error(ErrorCode::Num);
|
|
return Value::make_checked_number(std::sqrt(x));
|
|
});
|
|
}
|
|
EvalResult fn_sum(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.empty()) return err_res(ErrorCode::Value);
|
|
std::vector<double> nums;
|
|
if (auto err = collect_numeric_args(args, ctx, nums)) return err_res(*err);
|
|
double total = 0.0;
|
|
for (double v : nums) total += v;
|
|
return EvalResult::from_value(Value::make_checked_number(total));
|
|
}
|
|
EvalResult fn_sumif(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() < 2 || args.size() > 3) return err_res(ErrorCode::Value);
|
|
EvalResult cr_res = evaluate_ast(args[0].get(), ctx);
|
|
auto cr_var = result_to_array2d(cr_res, *ctx.wb);
|
|
if (std::holds_alternative<ErrorCode>(cr_var)) return err_res(std::get<ErrorCode>(cr_var));
|
|
const Array2D& cr = std::get<Array2D>(cr_var);
|
|
|
|
Value crit = deref_to_value(evaluate_ast(args[1].get(), ctx), *ctx.wb);
|
|
if (crit.is_error()) return EvalResult::from_value(crit);
|
|
|
|
Array2D sr;
|
|
if (args.size() == 3) {
|
|
EvalResult sr_res = evaluate_ast(args[2].get(), ctx);
|
|
if (sr_res.is_ref && sr_res.ranges.size() == 1) {
|
|
CellRange base = sr_res.ranges[0];
|
|
sr = Array2D(cr.rows, cr.cols);
|
|
for (int r = 0; r < cr.rows; ++r) {
|
|
for (int c = 0; c < cr.cols; ++c) {
|
|
sr.at(r, c) = ctx.wb->get_scalar_value(base.sheet, base.r1 + r, base.c1 + c);
|
|
}
|
|
}
|
|
} else {
|
|
auto sr_var = result_to_array2d(sr_res, *ctx.wb);
|
|
if (std::holds_alternative<ErrorCode>(sr_var)) return err_res(std::get<ErrorCode>(sr_var));
|
|
sr = std::move(std::get<Array2D>(sr_var));
|
|
if (sr.rows != cr.rows || sr.cols != cr.cols) return err_res(ErrorCode::Value);
|
|
}
|
|
} else {
|
|
sr = cr;
|
|
}
|
|
|
|
double total = 0.0;
|
|
for (int r = 0; r < cr.rows; ++r) {
|
|
for (int c = 0; c < cr.cols; ++c) {
|
|
if (matches_criteria(cr.at(r, c), crit)) {
|
|
const Value& sv = sr.at(r, c);
|
|
if (sv.is_number()) total += sv.as_number();
|
|
}
|
|
}
|
|
}
|
|
return EvalResult::from_value(Value::make_checked_number(total));
|
|
}
|
|
EvalResult fn_sumifs(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.size() < 3 || (args.size() - 1) % 2 != 0) return err_res(ErrorCode::Value);
|
|
auto sr_var = result_to_array2d(evaluate_ast(args[0].get(), ctx), *ctx.wb);
|
|
if (std::holds_alternative<ErrorCode>(sr_var)) return err_res(std::get<ErrorCode>(sr_var));
|
|
const Array2D& sr = std::get<Array2D>(sr_var);
|
|
|
|
struct Pair { Array2D range; Value crit; };
|
|
std::vector<Pair> pairs;
|
|
for (size_t i = 1; i < args.size(); i += 2) {
|
|
auto cr_var = result_to_array2d(evaluate_ast(args[i].get(), ctx), *ctx.wb);
|
|
if (std::holds_alternative<ErrorCode>(cr_var)) return err_res(std::get<ErrorCode>(cr_var));
|
|
Array2D cr = std::move(std::get<Array2D>(cr_var));
|
|
if (cr.rows != sr.rows || cr.cols != sr.cols) return err_res(ErrorCode::Value);
|
|
Value crit = deref_to_value(evaluate_ast(args[i + 1].get(), ctx), *ctx.wb);
|
|
if (crit.is_error()) return EvalResult::from_value(crit);
|
|
pairs.push_back({std::move(cr), std::move(crit)});
|
|
}
|
|
|
|
double total = 0.0;
|
|
for (int r = 0; r < sr.rows; ++r) {
|
|
for (int c = 0; c < sr.cols; ++c) {
|
|
bool ok = true;
|
|
for (const auto& p : pairs) {
|
|
if (!matches_criteria(p.range.at(r, c), p.crit)) {
|
|
ok = false;
|
|
break;
|
|
}
|
|
}
|
|
if (ok && sr.at(r, c).is_number()) {
|
|
total += sr.at(r, c).as_number();
|
|
}
|
|
}
|
|
}
|
|
return EvalResult::from_value(Value::make_checked_number(total));
|
|
}
|
|
EvalResult fn_tan(const std::vector<std::shared_ptr<Expr>>& a, const EvalContext& ctx) {
|
|
return unary_math(a, ctx, [](double x) { return Value::make_checked_number(std::tan(x)); });
|
|
}
|
|
EvalResult fn_trunc(const std::vector<std::shared_ptr<Expr>>& args, const EvalContext& ctx) {
|
|
if (args.empty() || args.size() > 2) return err_res(ErrorCode::Value);
|
|
ErrorCode err = ErrorCode::Value;
|
|
auto num = eval_number_arg(args[0], ctx, err);
|
|
if (!num) return err_res(err);
|
|
int d = 0;
|
|
if (args.size() == 2) {
|
|
auto dig = eval_number_arg(args[1], ctx, err);
|
|
if (!dig) return err_res(err);
|
|
d = static_cast<int>(std::trunc(*dig));
|
|
}
|
|
double x = *num;
|
|
double res = 0.0;
|
|
if (d >= 0) {
|
|
double scale = std::pow(10.0, d);
|
|
double v = x * scale;
|
|
res = std::isfinite(v) ? (std::trunc(v) / scale) : x;
|
|
} else {
|
|
double scale = std::pow(10.0, -d);
|
|
res = std::trunc(x / scale) * scale;
|
|
}
|
|
return EvalResult::from_value(Value::make_checked_number(res));
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void register_math_trig_functions(FunctionRegistry& reg) {
|
|
reg.register_fn("ABS", fn_abs);
|
|
reg.register_fn("ACOS", fn_acos);
|
|
reg.register_fn("ASIN", fn_asin);
|
|
reg.register_fn("ATAN", fn_atan);
|
|
reg.register_fn("ATAN2", fn_atan2);
|
|
reg.register_fn("COS", fn_cos);
|
|
reg.register_fn("DEGREES", fn_degrees);
|
|
reg.register_fn("EXP", fn_exp);
|
|
reg.register_fn("FACT", fn_fact);
|
|
reg.register_fn("INT", fn_int);
|
|
reg.register_fn("LN", fn_ln);
|
|
reg.register_fn("LOG", fn_log);
|
|
reg.register_fn("LOG10", fn_log10);
|
|
reg.register_fn("MOD", fn_mod);
|
|
reg.register_fn("PI", fn_pi);
|
|
reg.register_fn("POWER", fn_power);
|
|
reg.register_fn("PRODUCT", fn_product);
|
|
reg.register_fn("RADIANS", fn_radians);
|
|
reg.register_fn("RAND", fn_rand);
|
|
reg.register_fn("ROUND", fn_round);
|
|
reg.register_fn("SIN", fn_sin);
|
|
reg.register_fn("SQRT", fn_sqrt);
|
|
reg.register_fn("SUM", fn_sum);
|
|
reg.register_fn("SUMIF", fn_sumif);
|
|
reg.register_fn("SUMIFS", fn_sumifs);
|
|
reg.register_fn("TAN", fn_tan);
|
|
reg.register_fn("TRUNC", fn_trunc);
|
|
}
|
|
|
|
} // namespace htab
|