定制 ibelousov/math-exec 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ibelousov/math-exec

Composer 安装命令:

composer require ibelousov/math-exec

包简介

Evaluate expressions from strings using variable precision numbers

README 文档

README

MathExec is a php library for parsing and evaluating math expressions like this:

echo Evaluator::mathExec("5 ^ 2 + 36 / 2 - 2 * 0.5");

Or you could prepare an expression template and then execute it, with specified precision, when needed, by providing needed variables

$evaluator = Evaluator::mathPrepare("a + 5 / 3 * b - c");

foreach([[1,2,3,4],[4,5,6,4],[7,8,9,0]] as $values) {
    $evaluator->exec(['a' => $values[0], 'b' => $values[1], 'c' => $values[2]], $values[3]);
}

or like this:

$a = 1.5E+2;
$b = 18;

return Evaluator::mathExec("$a * 4 + 30 * 2 + $b / 3");

and this also is true:

var_dump(Evaluator::mathExec("floor((0.7+0.1)*10)") == 8);
// boolean true

var_dump([floor((0.7+0.1)*10) == 8]);
// boolean false

It utilizes a Pratt parser, and uses ext-bcmath extension to evaluate operations. For example this expression

$a = 4;
$b = 4; 
$c = 4;

Evaluator::mathExec("$a + $b / $c", 40);

internally turns into this

bcadd($a, bcdiv($b, $c, 40), 40);

it is very nice and convenient to write expressions naturally, without using bcadd, bcdiv, etc.

Installation

Use the package manager composer to install it

composer install ibelousov/math-exec

Usage

First of all import class Evaluator:

use \Ibelousov\MathExec\Evaluator\Evaluator;

Supported things

// Root  
Evaluator::mathExec("\\2"); 
// "1.1892071150027210667174999705604759152929"

(you could set inner precision, like this:

Evaluator::mathExec("\\2", 1000); 

// "1.1892071150027210667174999705604759152929720924638174130190022
// 2471946666822691715987078134453813767371603739477476921318606372
// 6361789847756785360862538017775070151511403557092273162342868889
// 9241754460719087105038499725591050098371044920154845735674580904
// 8399409309000349779590803848965884300504119871700937907982098462
// 5235373981281740818113780828552014842210060958932412445931035057
// 5191963029413832634742802798244080228008217292720586153666393704
// 0023820730854565306744771485988873345762718678381165470458727612
// 7111269988678434930175861424970170054131455143891998743766762178
// 5161783177987307048236318734734842180537156986842636482761056228
// 4779958628963329392816878747586560347379199645940075615444371574
// 1890303986971294306248625351734129153597531121544674615908647760
// 6517445957055930979119465756398917686972170262497475333629918606
// 5311570834936807698049481706074376847467855865282550141846497924
// 8909951563378299859508764353239662147789654791045418693466186139
// 614521856391702634160435422985610854932687"

by default it is 40 signs after point )

// Multiplication

Evaluator::mathExec("2 * 2", 0); 
//  "4"

// Division

Evaluator::mathExec("2 / 2", 0); 
// "1"
// Power
Evaluator::mathExec("2 ^ 3", 0); 
// "8" (left and right should be whole numbers)
// Modul
Evaluator::mathExec("7 % 2", 0); 
// "1"
// Whole division
Evaluator::mathExec("3.1415 // 2", 0); 
// "1"
// Associativity
Evaluator::mathExec("2 + 2 * 2", 0);  
// "6"
// Parenthesis
Evaluator::mathExec("(2 + 2) * 2", 0); 
// "8"
// Float to string convertion number formats
$a = 0.1415E-10;
$b = 0.1415E-10;
Evaluator::mathExec("$a * $b"); 
// "0.0000000000000000000002002225000000000000"
// comparison
Evaluator::mathExec("4 ^ 128 > 4 ^ 64"); 
// "1" 
Evaluator::mathExec("4 ^ 128 < 4 ^ 64"); 
// "0" 
Evaluator::mathExec("4 ^ 64 + 1 >= 4 ^ 64"); 
// "1"
Evaluator::mathExec("4 ^ 64 <= 4 ^ 64"); 
// "1"
Evaluator::mathExec("4 == 4"); 
// "1"
Evaluator::mathExec("4 != 4"); 
// "0"

Functions

Evaluator::mathExec("floor(3.1415)"); 
// "3"
Evaluator::mathExec("ceil(3.1415)");
// "4"
Evaluator::mathExec("format(ceil(3.1415) + floor(3.1415), 2)");
// "7.00" 

Also you can add your own functions like this:

\Ibelousov\MathExec\Evaluator\BuiltinCollection::addBuiltin('inc', function($args) {
    $number_parts = explode('.', $args[0]->value);
    $precision = isset($number_parts[1]) ? strlen($number_parts[1]) : 0;

    return new \Ibelousov\MathExec\Evaluator\NumberObj(bcadd($args[0]->value, '1', $precision));
});

echo Evaluator::mathExec('inc(inc(2))',40);

Use with cautiousness

Inner representation of numbers

For example if you call this

Evaluator::mathExec("4/4 == 6/5", 0);

it evaluates to 1, because inner representation in this case is 0, and when you divide 6/5 you get 1 and not 1.2

For accurate numbers comparison you should set precision properly. In case shown above, you should do this, to properly compare numbers

Evaluator::mathExec("4/4 == 6/5", 1);

Converting to native PHP number formats

(int)Evaluator::mathExec((string)PHP_INT_MAX); 
// Evaluates to PHP_INT_MAX number
(int)Evaluator::mathExec((string)PHP_INT_MIN);
// Evaluates to PHP_INT_MIN number
(float)Evaluator::mathExec('\\2 + \\2', 30);
// cuts result 2.8284271247461900976033774484193961571392 to 2.8284271247462  
(float)Evaluator::mathExec((string)PHP_FLOAT_MIN);
// Evaluates to PHP_FLOAT_MIN number
(float)Evaluator::mathExec('1.7976931348623157E+308');
// Evaluates to PHP_FLOAT_MAX. Today i have no time to figure out, why is
// (float)\Ibelousov\MathExec\Evaluator\Evaluator::mathExec((string)PHP_FLOAT_MAX)
// doesnt return correct value, but it is a fact

Other convertions, like this

(int)Evaluator::mathExec((string)PHP_INT_MIN . ' - 1');

are unpredictable

To evaluate and then compare numbers with high precision and/or big numbers, you should prefer storing values in strings instead of converting them into float or int.

REPL

You can use REPL to test expression evaluation:

$ ./vendor/ibelousov/math-exec/src/REPL.php
>> 4+4*4
20
Executed in 0.0025420188903809s.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

ibelousov/math-exec 适用场景与选型建议

ibelousov/math-exec 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「parser」 「math」 「expressions」 「bcmath」 「convenient」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ibelousov/math-exec 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ibelousov/math-exec 我们能提供哪些服务?
定制开发 / 二次开发

基于 ibelousov/math-exec 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 7
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-04-04