hinyka / evalmath
Use the EvalMath when you want to evaluate mathematical expressions from untrusted sources.
dev-main / 2.x-dev
2023-08-24 19:35 UTC
Requires
- php: >=8.0
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: ^1.0
- phpstan/phpstan: >=1.10.0
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: ^3.7.2
This package is auto-updated.
Last update: 2024-12-24 22:40:26 UTC
README
Safely evaluate math expressions.
Description
The EvalMath empowers safe evaluation of mathematical expressions originating from potentially untrusted sources.
It supports built-in and user-defined functions and variables, offering flexibility for complex mathematical operations.
Usage
# Create a base instance (default precision is set to 6 digits after the decimal point) $em = new EvalMath; # Create a base instance with the precision parameter set to 2 digits after the decimal point $em = new EvalMath(2); # Basic evaluation: $result = $em->evaluate('2+2'); # Supports: order of operation; parentheses; negation; built-in functions $result = $em->evaluate('-8(5/2)^2*(1-sqrt(4))-8'); # Create your own variables $em->evaluate('a = e^(ln(pi))'); # or functions $em->evaluate('f(x,y) = x^2 + y^2 - 2x*y + 1'); # and then use them $result = $em->evaluate('3*f(42,a)');
Methods
Credits
This is a heavily refactored version of EvalMath, originally by Daniel Bojdo, which is based on Miles Kaufmann's EvalMath class.
License
This project is licensed under the BSD 3-Clause License. The full text of the license can be found in the LICENSE file.