Download the PHP package ibelousov/math-exec without Composer
On this page you can find all versions of the php package ibelousov/math-exec. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ibelousov/math-exec
More information about ibelousov/math-exec
Files in ibelousov/math-exec
Package math-exec
Short Description Evaluate expressions from strings using variable precision numbers
License MIT
Informations about the package math-exec
MathExec
MathExec is a php library for parsing and evaluating math expressions like this:
Or you could prepare an expression template and then execute it, with specified precision, when needed, by providing needed variables
or like this:
and this also is true:
It utilizes a Pratt parser, and uses ext-bcmath extension to evaluate operations. For example this expression
internally turns into this
it is very nice and convenient to write expressions naturally, without using bcadd, bcdiv, etc.
Installation
Use the package manager composer to install it
Usage
First of all import class Evaluator:
Supported things
(you could set inner precision, like this:
by default it is 40 signs after point )
// Multiplication
// Division
Functions
Also you can add your own functions like this:
Use with cautiousness
Inner representation of numbers
For example if you call this
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
Converting to native PHP number formats
Other convertions, like this
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:
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
All versions of math-exec with dependencies
ext-bcmath Version >=7.2