carriongrow/formula_parser

解析公式,支持延迟解决方案和用户变量的替换

v1.1.5 2022-02-25 13:33 UTC

This package is auto-updated.

Last update: 2024-09-25 19:06:51 UTC


README

解析公式,支持延迟解决方案和用户变量的替换

composer require carriongrow/formula_parser

安装

$ composer require carriongrow/formula_parser

描述

支持的基本函数 = '*', '/', '+', '-', '^' 函数操作 = 'cos', 'sin', 'exp', 'abs', 'log', 'sqrt', 'tan', 'pi'

cos sin tan - 不需要从度转换为弧度等效值

动态变量的可用符号:0-9 a-z _ .or [a-z.0-9_] 无关注册依赖!

示例

 $parser = new FormulaParser();
 $parser->setFormula('test1 * test2 * 2 (1 + 10) / 20'); // Parsing formulas once
 $counter = 1000;
 $result = [];
 while ($counter != 0) { // We use it as many times as we need
   $first = rand(1, getrandmax()) / getrandmax();
   $second = rand(1, getrandmax()) / getrandmax();
   $parser->setVariables(['test1' => $first, 'test2' => $second]); // Replacing user variables with values
   $result[] = $parser->calculate();
   $counter--;
 }

更多查看测试 - https://github.com/AnatolySnegovskiy/FormulaParser/blob/master/test/FormulaParserTest.php