Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
vendor
vendor

# Netbeans
/nbproject/private/
project.properties
project.xml
41 changes: 41 additions & 0 deletions lib/PHPMathParser/Expressions/MathFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* The PHP Math Parser library
*
* @author Mohamed BOUDAOUI (motionSeed) <dev@motionseed.com>
* @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);
}
}
3 changes: 3 additions & 0 deletions lib/PHPMathParser/TerminalExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PHPMathParser\Expressions\Power;
use PHPMathParser\Expressions\Subtraction;
use PHPMathParser\Expressions\Unary;
use PHPMathParser\Expressions\MathFunction;

abstract class TerminalExpression
{
Expand Down Expand Up @@ -48,6 +49,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);
}
Expand Down
23 changes: 22 additions & 1 deletion test.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,25 @@
$math->registerVariable('a', -5.5);
$answer = $math->evaluate('($a + $a) * 4');
var_dump($answer);echo "<br /><br />";
// float(-44)
// float(-44)

// TEST added by motionSeed
$math->registerVariable('a', 5);
$answer = $math->evaluate('10 + CEIL($a / 4)');
var_dump($answer);echo "<br /><br />";
// int(12)

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

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

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