aboutsummaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorBruce Wells <bruce.wells@simparel.com>2018-10-30 23:16:01 +0300
committerBruce Wells <bruce.wells@simparel.com>2018-10-31 16:35:40 +0300
commitf0d4562b9ef3bbc64cb8353f0597e639420ede6b (patch)
tree86f215d0c7187843e863e7a980ae81943e7d4a5f /README.md
parent334dd26e3c78a36002aed34aa26aa526b11c5dfb (diff)
Division By Zero Exception support
Updated the documentation. Unit tests for strings. DivisionByZeroException support.
Diffstat (limited to 'README.md')
-rw-r--r--README.md57
1 files changed, 39 insertions, 18 deletions
diff --git a/README.md b/README.md
index 2fdb5c1..f5bf318 100644
--- a/README.md
+++ b/README.md
@@ -1,34 +1,37 @@
-[![Stories in Ready](https://badge.waffle.io/NeonXP/MathExecutor.png?label=ready&title=Ready)](https://waffle.io/NeonXP/MathExecutor)
-# MathExecutor
+# MathExecutor [![Stories in Ready](https://badge.waffle.io/NeonXP/MathExecutor.png?label=ready&title=Ready)](https://waffle.io/NeonXP/MathExecutor) [![Build Status](https://travis-ci.org/NeonXP/MathExecutor.png?branch=master)](https://travis-ci.org/NeonXP/MathExecutor)
-[![Build Status](https://travis-ci.org/NeonXP/MathExecutor.png?branch=master)](https://travis-ci.org/NeonXP/MathExecutor)
+A simple math expressions calculator
-Simple math expressions calculator
-
-## Install via Composer
+## Features:
+* Built in support for +, -, *, / and power (^) operators
+* Support for user defined operators
+* Support for user defined functions
+* Unlimited varable length
+* String support, as function parameters or as evaluated by PHP
+* Exceptions on divide by zero, or treat as zero
+* Unary Minus
+## Install via Composer:
Stable branch
```
composer require "nxp/math-executor" "dev-master"
```
-Dev branch
+Dev branch (currently unsupported)
```
composer require "nxp/math-executor" "dev-dev"
```
## Sample usage:
-
```php
require "vendor/autoload.php";
-$calculator = new \NXP\MathExecutor();
+$executor = new \NXP\MathExecutor();
-print $calculator->execute("1 + 2 * (2 - (4+10))^2 + sin(10)");
+echo $executor->execute("1 + 2 * (2 - (4+10))^2 + sin(10)");
```
## Functions:
-
Default functions:
* sin
* cos
@@ -48,13 +51,10 @@ $executor->addFunction('abs', function($arg) {
```
## Operators:
-
Default operators: `+ - * / ^`
Add custom operator to executor:
-MyNamespace/ModulusToken.php:
-
```php
<?php
namespace MyNamespace;
@@ -113,7 +113,6 @@ $executor->addOperator('MyNamespace\ModulusToken');
```
## Variables:
-
Default variables:
```
@@ -124,9 +123,31 @@ $e = 2.71828182846
You can add own variable to executor:
```php
-$executor->setVars(array(
+$executor->setVars([
'var1' => 0.15,
'var2' => 0.22
-));
+]);
+
+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.
-$executor->execute("$var1 + $var2");
+```php
+$executor->setDivisionByZeroException();
+try {
+ echo $executor->execute('1/0');
+} catch (\NXP\Exception\DivisionByZeroException $e) {
+ echo $e->getMessage();
+}
+```
+
+## Unary Minus Operator:
+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.
+
+```php
+echo $executor->execute("1 + '2.5' * '.5' + myFunction('category')");
+```