diff options
author | Bruce Wells <brucekwells@gmail.com> | 2019-01-09 03:39:48 +0300 |
---|---|---|
committer | Bruce Wells <brucekwells@gmail.com> | 2019-01-09 03:39:48 +0300 |
commit | 9684cfd1d0e61a6bfcf1a46f322f99e7b6559df5 (patch) | |
tree | ca4b36c1383276cf50e81791b4b6179e4cacacd7 | |
parent | 76307e3f41b5582196e9620c687052f8bb8537cc (diff) | |
parent | 8e9c8508cbfaa944505f7d3a988bcfce1e4d7bba (diff) |
Merge branch 'master' of https://github.com/phpfui/MathExecutor
# Conflicts:
# src/NXP/Classes/Lexer.php
-rw-r--r-- | README.md | 17 | ||||
-rw-r--r-- | tests/MathTest.php | 14 |
2 files changed, 23 insertions, 8 deletions
@@ -3,18 +3,19 @@ A simple math expressions calculator ## Features: -* Built in support for +, -, *, / and power (^) operators +* Built in support for +, -, *, / and power (^) operators plus () * Support for user defined operators * Support for user defined functions -* Unlimited varable length -* String support, as function parameters or as evaluated by PHP +* Unlimited variable name lengths +* String support, as function parameters or as evaluated as a number by PHP * Exceptions on divide by zero, or treat as zero -* Unary Minus +* Unary Minus (e.g. -3) +* Pi ($pi) and Euler's number ($e) support to 11 decimal places ## Install via Composer: Stable branch ``` -composer require "nxp/math-executor" "dev-master" +composer require "nxp/math-executor" ``` Dev branch (currently unsupported) @@ -120,7 +121,7 @@ $pi = 3.14159265359 $e = 2.71828182846 ``` -You can add own variable to executor: +You can add your own variables to executor: ```php $executor->setVars([ @@ -131,7 +132,7 @@ $executor->setVars([ echo $executor->execute("$var1 + $var2"); ``` ## Division By Zero Support: -By default, the result of division by zero is zero and no error is generated. You have the option to thow a \NXP\Exception\DivisionByZeroException by by calling setDivisionByZeroException. +By default, the result of division by zero is zero and no error is generated. You have the option to thow a \NXP\Exception\DivisionByZeroException by calling setDivisionByZeroException. ```php $executor->setDivisionByZeroException(); @@ -146,7 +147,7 @@ try { Negative numbers are supported via the unary minus operator, but need to have a space before the minus sign. `-1+ -3` is legal, while '`-1+-3` will produce an error due to the way the parser works. Positive numbers are not explicitly supported as unsigned numbers are assumed positive. ## String Support: -Expressions can contain double or single quoted strings that are evaluated the same way as PHP. You can also pass strings to functions. +Expressions can contain double or single quoted strings that are evaluated the same way as PHP evalutes strings as numbers. You can also pass strings to functions. ```php echo $executor->execute("1 + '2.5' * '.5' + myFunction('category')"); diff --git a/tests/MathTest.php b/tests/MathTest.php index c5dd60d..9acdb63 100644 --- a/tests/MathTest.php +++ b/tests/MathTest.php @@ -69,7 +69,21 @@ class MathTest extends \PHPUnit_Framework_TestCase ['(2 + 2)*-2'], ['(2+-2)*2'], + ['1 + 2 * 3 / (min(1, 5) + 2 + 1)'], + ['1 + 2 * 3 / (min(1, 5) - 2 + 5)'], + ['1 + 2 * 3 / (min(1, 5) * 2 + 1)'], + ['1 + 2 * 3 / (min(1, 5) / 2 + 1)'], + ['1 + 2 * 3 / (min(1, 5) / 2 * 1)'], + ['1 + 2 * 3 / (min(1, 5) / 2 / 1)'], + ['1 + 2 * 3 / (3 + min(1, 5) + 2 + 1)'], + ['1 + 2 * 3 / (3 - min(1, 5) - 2 + 1)'], + ['1 + 2 * 3 / (3 * min(1, 5) * 2 + 1)'], + ['1 + 2 * 3 / (3 / min(1, 5) / 2 + 1)'], + + ['sin(10) * cos(50) / min(10, 20/2)'], + ['sin(10) * cos(50) / min(10, (20/2))'], + ['sin(10) * cos(50) / min(10, (max(10,20)/2))'], ['100500 * 3.5E5'], ['100500 * 3.5E-5'], |