定制 fintara/calculator-php 二次开发

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

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

fintara/calculator-php

Composer 安装命令:

composer require fintara/calculator-php

包简介

Calculator for arithmetic expressions

README 文档

README

PHP calculator which evaluates different arithmetic expressions:

2+5.9       = 7.9
3.5^(2-5)   = 0.02332361516035
5-(2*(1+2)) = -1

The included functions by default are sqrt(x) and log(base, arg), but there is also an option to add custom functions with any number of arguments.

Basic usage

use \Fintara\Tools\Calculator\Calculator;

$calculator = Calculator::create(); // use default tokenizer
echo $calculator->calculate('1+2*3/4'); // 2.5

Advanced usage

Custom functions

You can add custom functions:

  • name: name of the function, as it will be used in expressions. All lower case and _ (underscore) allowed.
  • implementation: how to evaluate the function.
$calculator->addFunction('cbrt', function($x) {
    return pow($x, 1/3);
});

echo $calculator->calculate('cbrt(27)'); // 3

Tokenizer

You can also use the tokenizer (or supply calculator with your own):

$tokenizer = new Tokenizer();
$tokens = $tokenizer->tokenize('1+2*3.5'); // [1, '+', 2, '*', 3.5]

In case the expression contains functions (e.g. sqrt(x)), the tokenizer needs to know all functions' names as second parameter.

$tokens = $tokenizer->tokenize('1+sqrt(4)', ['sqrt']); // [1, '+', 'sqrt', '(', 4, ')']

Note: The default tokenizer automatically adds * (multiplication) sign between a number and following function or a number and following parenthesis (if the sign is not found).

$tokens = $tokenizer->tokenize('2 (1 + 3)'); // [2, '*', '(', 1, '+', 3, ')']

Changelog

2.0.1

  • Added LICENSE

2.0.0

  • Breaking: operator mod is renamed to %.
  • Breaking: addFunction does not need number of arguments anymore (removed 3rd argument).
  • Breaking: adding function with existing name throws. You can use the helper replaceFunction.
  • Breaking: getReversePolishNotation is private.
  • Breaking: setExpression is removed. Use directly calculate($expression).
  • Breaking: ILexer and DefaultLexer are replaced with TokenizerInterface and Tokenizer respectively.

fintara/calculator-php 适用场景与选型建议

fintara/calculator-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31.47k 次下载、GitHub Stars 达 13, 最近一次更新时间为 2014 年 02 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 fintara/calculator-php 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 31.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 13
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-02-19