From 4d6731854cf71b16330ac24ec6bca083af28a417 Mon Sep 17 00:00:00 2001 From: motionSeed Dev Team Date: Sun, 19 Feb 2017 15:56:42 +0000 Subject: [PATCH 1/3] - Add support for Math functions - with single arg --- .gitignore | 8 +++- .../Expressions/MathFunction.php | 41 +++++++++++++++++++ lib/PHPMathParser/TerminalExpression.php | 2 + test.php | 23 ++++++++++- 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 lib/PHPMathParser/Expressions/MathFunction.php diff --git a/.gitignore b/.gitignore index 5657f6e..0683cd6 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,7 @@ -vendor \ No newline at end of file +vendor + +# Netbeans +/nbproject/private/ +project.properties +project.xml +/.gitignore \ No newline at end of file diff --git a/lib/PHPMathParser/Expressions/MathFunction.php b/lib/PHPMathParser/Expressions/MathFunction.php new file mode 100644 index 0000000..058e408 --- /dev/null +++ b/lib/PHPMathParser/Expressions/MathFunction.php @@ -0,0 +1,41 @@ + + * @copyright 2017 The Authors + * @license http://www.opensource.org/licenses/mit-license.html MIT License + * @version Build @@version@@ + */ +namespace PHPMathParser\Expressions; + +use PHPMathParser\Stack; + +class MathFunction extends Operator +{ + protected $precedence = 10; + + protected static $functions = [ + 'ABS', + 'COS', 'COSH', 'SIN', 'SINH', 'TAN', 'TANH', 'ACOS', 'ACOSH', 'ASIN', 'ASINH', 'ATAN', 'ATAN2', 'ATANH', + 'DEG2GRAD', 'RAD2DEG', 'PI', + 'CEIL', 'FLOOR', 'ROUND', 'SQRT', 'LOG10' + ]; + + public static function isFunction($value) + { + return in_array($value, self::$functions); + } + + public function operate(Stack $stack) + { + $value = $stack->pop()->operate($stack); + + $function = strtolower($this->value); + + $result = new Number($function($value)); + + return $result->operate($stack); + } +} \ No newline at end of file diff --git a/lib/PHPMathParser/TerminalExpression.php b/lib/PHPMathParser/TerminalExpression.php index 33131d5..c311ae1 100644 --- a/lib/PHPMathParser/TerminalExpression.php +++ b/lib/PHPMathParser/TerminalExpression.php @@ -48,6 +48,8 @@ public static function factory($value) return new Parenthesis($value); } elseif ($value == '^') { return new Power($value); + } elseif (MathFunction::isFunction($value)) { + return new MathFunction($value); } throw new \Exception('Undefined Value ' . $value); } diff --git a/test.php b/test.php index 01f556d..cae7aae 100644 --- a/test.php +++ b/test.php @@ -98,4 +98,25 @@ $math->registerVariable('a', -5.5); $answer = $math->evaluate('($a + $a) * 4'); var_dump($answer);echo "

"; - // float(-44) \ No newline at end of file + // float(-44) + + // TEST added by motionSeed + $math->registerVariable('a', 5); + $answer = $math->evaluate('10 + CEIL($a / 4)'); + var_dump($answer);echo "

"; + // int(12) + + $math->registerVariable('a', 5); + $answer = $math->evaluate('10 + FLOOR($a / 4)'); + var_dump($answer);echo "

"; + // int(11) + + $math->registerVariable('a', 9); + $answer = $math->evaluate('10 + SQRT($a)'); + var_dump($answer);echo "

"; + // int(13) + + $math->registerVariable('a', 10); + $answer = $math->evaluate('10 + CEIL(SQRT($a))'); + var_dump($answer);echo "

"; + // int(14) \ No newline at end of file From 177b5148964f3ffcfadda6117be56d81fe00e4c6 Mon Sep 17 00:00:00 2001 From: motionSeed Dev Team Date: Sun, 19 Feb 2017 15:58:57 +0000 Subject: [PATCH 2/3] - Fix .gitignore --- .gitignore | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 0683cd6..4ef6ef4 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,4 @@ vendor # Netbeans /nbproject/private/ project.properties -project.xml -/.gitignore \ No newline at end of file +project.xml \ No newline at end of file From c24c4f2a5f213fcff396e2d344c95ff182851938 Mon Sep 17 00:00:00 2001 From: motionSeed Dev Team Date: Sun, 19 Feb 2017 17:26:12 +0000 Subject: [PATCH 3/3] - Miss "use PHPMathParser\Expressions\MathFunction;" --- lib/PHPMathParser/TerminalExpression.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/PHPMathParser/TerminalExpression.php b/lib/PHPMathParser/TerminalExpression.php index c311ae1..d7c128c 100644 --- a/lib/PHPMathParser/TerminalExpression.php +++ b/lib/PHPMathParser/TerminalExpression.php @@ -18,6 +18,7 @@ use PHPMathParser\Expressions\Power; use PHPMathParser\Expressions\Subtraction; use PHPMathParser\Expressions\Unary; +use PHPMathParser\Expressions\MathFunction; abstract class TerminalExpression {