hinyka/evalmath

当您需要评估来自不受信任来源的数学表达式时,请使用EvalMath。

dev-main / 2.x-dev 2023-08-24 19:35 UTC

This package is auto-updated.

Last update: 2024-09-24 22:07:48 UTC


README

安全评估数学表达式。

描述

EvalMath 允许安全地评估来自可能不受信任来源的数学表达式。
它支持内置和用户定义的函数和变量,为复杂的数学运算提供了灵活性。

用法

# Create a base instance (default precision is set to 6 digits after the decimal point)
$em = new EvalMath;

# Create a base instance with the precision parameter set to 2 digits after the decimal point
$em = new EvalMath(2);

# Basic evaluation:
$result = $em->evaluate('2+2');

# Supports: order of operation; parentheses; negation; built-in functions
$result = $em->evaluate('-8(5/2)^2*(1-sqrt(4))-8');

# Create your own variables
$em->evaluate('a = e^(ln(pi))');

# or functions
$em->evaluate('f(x,y) = x^2 + y^2 - 2x*y + 1');

# and then use them
$result = $em->evaluate('3*f(42,a)');

方法

致谢

这是由 Daniel Bojdo 创作的 EvalMath 的一个重写版本,原始版本基于 Miles Kaufmann 的 Miles KaufmannEvalMath 类。

许可证

本项目采用 BSD 3-Clause 许可证。许可证的全文可以在 LICENSE 文件中找到。