andileco/eval-math

EvalMath

3.0.1 2022-07-21 21:04 UTC

This package is auto-updated.

Last update: 2024-09-22 02:03:43 UTC


README

EvalMath 的 Composer/Packagist 版本,由 Miles Kaufman 编写,版权所有 (C) 2005 Miles Kaufmann http://www.twmagic.com/ NAME

EvalMath - safely evaluate math expressions

描述

Use the EvalMath class when you want to evaluate mathematical expressions 
from untrusted sources.  You can define your own variables and functions,
which are stored in the object.  Try it, it's fun!

概要

`$m = new EvalMath;`

`// basic evaluation:`
`$result = $m->evaluate('2+2');`

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

`// create your own variables`
`$m->evaluate('a = e^(ln(pi))');`

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

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

`// use methods (calc functions)
`$m->evaluate('1+max(2,3)') // => 4`
`$m->evaluate('if(1=2, 2+2, 5+5') // => 10

方法

`$m->evaluate($expr)`
    Evaluates the expression and returns the result.  If an error occurs,
    prints a warning and returns false.  If $expr is a function assignment,
    returns true on success.

`$m->e($expr)`
    A synonym for $m->evaluate().

`$m->vars()`
    Returns an associative array of all user-defined variables and values.
    
`$m->funcs()`
    Returns an array of all user-defined functions.

计算方法(计算函数)

  • max(n...,m) 返回给定参数中的最大值
  • min(n...,m) 返回给定参数中的最小值
  • if(expr, true_value, false_value) (具有 iif 同义词)根据 expr 的评估结果返回 true_valuefalse_value
  • round(n,m) 返回具有 m 精度的 n 值

创建您自己的自定义计算方法(计算函数)

You can create custom classes, that implements custom calc methods.

待办事项

  • 改进文档
  • 添加自定义操作符的能力
  • 更多测试

致谢和版权

此软件集成了以下库和补丁: