mathematicator-core/numbers
Composer 安装命令:
composer require mathematicator-core/numbers
包简介
Safety operations with numbers.
关键字:
README 文档
README
Smart PHP Number Utilities
A PHP library to safely store and represent numbers and its equivalents in PHP.
Store lots of number types exactly (integers, decimals, fractions) and convert
them to each other. Expressions can be outputted as a human string (e.g. 1/2)
or LaTeX (e.g. \frac{1}{2}).
It is highly recommended to make sure you have enabled BCMath or GMP extension on your PHP server for much faster calculations.
Installation
composer require mathematicator-core/numbers
Features
- SmartNumber - Unified safe storage for all number types with
an arbitrary precision. It supports comparisons.
- Entity\Number Less magic universal arbitrary precision number storage with basic features.
- Fractions:
- Entity\Fraction - Storage for simple or compound fraction that can consist from numbers and other expressions.
- Entity\FractionNumbersOnly - Storage for simple or compound fraction that consists only from numbers and is directly computable.
- LaTeX support: (What is LaTeX?)
- MathLatexBuilder - Create valid LaTeX for mathematical expressions in simple way with a fluent interface.
- MathLatexToolkit - Statical library for LaTeX expressions (includes constants, functions, operators etc.)
- MathLatexSnippet - Storage for LaTeX syntax.
- Human string support:
- MathHumanStringBuilder - same interface as MathLatexBuilder, but produces human strings
- MathHumanStringToolkit - same interface as MathLatexToolkit,
but produces human strings (e.g.
1/2*(3+1))
- Set generators
- Primary numbers
- Even numbers
- Odd numbers
- Converters:
- Array to fraction and back
- Decimal to fraction
- Fraction to human string
- Fraction to LaTeX
- Int to Roman and back
- Calculation - simple arithmetic operations (brick/math decorator)
💡 TIP: You can use mathematicator-core/tokenizer for advance user input string tokenization or mathematicator-core/calculator for advance calculations.
Usage
use Brick\Math\RoundingMode; use Mathematicator\Numbers\SmartNumber; $smartNumber = SmartNumber::of('80.500'); echo $smartNumber->toBigDecimal(); // 80.500 echo $smartNumber->toFraction()->getNumerator(); // 161 echo $smartNumber->toFraction()->getDenominator(); // 2 echo Calculation::of($smartNumber)->multipliedBy(-4); // -322.000 echo Calculation::of($smartNumber)->multipliedBy(-4)->abs()->getResult()->toInt(); // 322 echo $smartNumber->toBigDecimal()->toScale(0, RoundingMode::HALF_UP); // 81 $smartNumber2 = SmartNumber::of('161/2'); echo $smartNumber2->toHumanString(); // 161/2 echo $smartNumber2->toHumanString()->plus(5)->equals('90.5'); // 161/2+10=90.5 echo $smartNumber2->toLatex(); // \frac{161}{2} echo $smartNumber2->toBigDecimal(); // 80.5
Recommended libraries
For safe operations with arbitrary length numbers we recommend to use:
- brick/math - Arbitrary precision arithmetic library for PHP with a simple interface.
- PHP BC Math extension - Native PHP extension for arbitrary precision computing.
Working with money
Use one of these libraries if you work with money in your application.
- brick/money - A money and currency library with an interface like brick/math.
- moneyphp/money - Widely adopted PHP implementation of Fowler's Money pattern.
Why float is not safe?
Float stores your number as an approximation with limited precision.
You should never trust float to the last digit. Do not use floats directly for checking equity if you rely on precision (not only monetary calculations).
Example:
$result = 0.1 + 0.2; echo $result; // output: 0.3 echo ($result == 0.3) ? 'true' : 'false'; // output: false
How is float stored in memory?
Read more about float on Wikipedia
Mathematicator Framework tools structure
The biggest advantage is that you can choose which layer best fits your needs and start build on the top of it, immediately, without the need to create everything by yourself. Our tools are tested for bugs and tuned for performance, so you can save a significant amount of your time, money, and effort.
Framework tend to be modular as much as possible, so you should be able to create an extension on each layer and its sublayers.
Mathematicator framework layers ordered from the most concrete one to the most abstract one:
| Search | Modular search engine layer that calls its sublayers and creates user interface. |
| Vizualizator |
Elegant graphic visualizer that can render to
SVG, PNG, JPG and Base64. Extensions: Mandelbrot set generator |
| Calculator |
Modular advance calculations layer.
Extensions: Integral Solver , Statistics |
| Engine | Core logic layer that maintains basic controllers, DAOs, translator, common exceptions, routing etc. |
| Tokenizer | Tokenizer that can convert string (user input / LaTeX) to numbers and operators. |
| Numbers | Fast & secure storage for numbers with arbitrary precision. It supports Human string and LaTeX output and basic conversions. |
Third-party packages:
⚠️ Not guaranteed!
| REST API | Install the whole pack as a REST API service on your server (Docker ready) or access it via public cloud REST API. |
Contribution
Please help to improve this documentation by sending a Pull request.
Tests
All new contributions should have its unit tests in /tests directory.
Before you send a PR, please, check all tests pass.
This package uses Nette Tester. You can run tests via command:
composer test
For benchmarking, we use phpbench. You can run benchmarks this way:
composer global require phpbench/phpbench @dev # only the first time
phpbench run
Before PR, please run complete code check via command:
composer cs:install # only the first time composer fix # otherwise pre-commit hook can fail
mathematicator-core/numbers 适用场景与选型建议
mathematicator-core/numbers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5.39k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 07 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「math」 「numbers」 「mathematicator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mathematicator-core/numbers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mathematicator-core/numbers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mathematicator-core/numbers 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LaTeX formula to PNG
Partition problem for balanced arrays splitting made easy.
Simple, chainable calculation library.
Easy to use arithmetic operations for decimals in PHP (using BCMath)
Rutils Symfony 2 bundle
Core logic for elegant graphic visualization. Render to SVG, PNG, JPG and Base64.
统计信息
- 总下载量: 5.39k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 24
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-07-25