aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBruce Wells <brucekwells@gmail.com>2022-06-02 01:11:51 +0300
committerGitHub <noreply@github.com>2022-06-02 01:11:51 +0300
commita944fe4e5631a6b3085ee2962e3159261f90bfcd (patch)
tree8dbbb41307b76f23db01f420418c7b8fbc44a996 /src
parentcbada2b920782bdfd2f18ef88c09e24ae4840f4a (diff)
Bcmath (#115)v2.3.0
* Add useBCMath * Support for % operator (mod)
Diffstat (limited to 'src')
-rw-r--r--src/NXP/MathExecutor.php44
1 files changed, 41 insertions, 3 deletions
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php
index 1e1cd2d..6709e21 100644
--- a/src/NXP/MathExecutor.php
+++ b/src/NXP/MathExecutor.php
@@ -263,7 +263,7 @@ class MathExecutor
*
* @return array<Operator> of operator class names
*/
- public function getOperators()
+ public function getOperators() : array
{
return $this->operators;
}
@@ -280,6 +280,18 @@ class MathExecutor
}
/**
+ * Remove a specific operator
+ *
+ * @return array<Operator> of operator class names
+ */
+ public function removeOperator(string $operator) : self
+ {
+ unset($this->operators[$operator]);
+
+ return $this;
+ }
+
+ /**
* Set division by zero returns zero instead of throwing DivisionByZeroException
*/
public function setDivisionByZeroIsZero() : self
@@ -301,16 +313,39 @@ class MathExecutor
/**
* Clear token's cache
*/
- public function clearCache() : void
+ public function clearCache() : self
{
$this->cache = [];
+
+ return $this;
+ }
+
+ public function useBCMath(int $scale = 2) : self
+ {
+ \bcscale($scale);
+ $this->addOperator(new Operator('+', false, 170, static fn($a, $b) => \bcadd("{$a}", "{$b}")));
+ $this->addOperator(new Operator('-', false, 170, static fn($a, $b) => \bcsub("{$a}", "{$b}")));
+ $this->addOperator(new Operator('uNeg', false, 200, static fn($a) => \bcsub('0.0', "{$a}")));
+ $this->addOperator(new Operator('*', false, 180, static fn($a, $b) => \bcmul("{$a}", "{$b}")));
+ $this->addOperator(new Operator('/', false, 180, static function($a, $b) {
+ /** @todo PHP8: Use throw as expression -> static fn($a, $b) => 0 == $b ? throw new DivisionByZeroException() : $a / $b */
+ if (0 == $b) {
+ throw new DivisionByZeroException();
+ }
+
+ return \bcdiv("{$a}", "{$b}");
+ }));
+ $this->addOperator(new Operator('^', true, 220, static fn($a, $b) => \bcpow("{$a}", "{$b}")));
+ $this->addOperator(new Operator('%', false, 180, static fn($a, $b) => \bcmod("{$a}", "{$b}")));
+
+ return $this;
}
/**
* Set default operands and functions
* @throws ReflectionException
*/
- protected function addDefaults() : void
+ protected function addDefaults() : self
{
foreach ($this->defaultOperators() as $name => $operator) {
[$callable, $priority, $isRightAssoc] = $operator;
@@ -323,6 +358,8 @@ class MathExecutor
$this->onVarValidation = [$this, 'defaultVarValidation'];
$this->variables = $this->defaultVars();
+
+ return $this;
}
/**
@@ -352,6 +389,7 @@ class MathExecutor
false
],
'^' => [static fn($a, $b) => \pow($a, $b), 220, true],
+ '%' => [static fn($a, $b) => $a % $b, 180, false],
'&&' => [static fn($a, $b) => $a && $b, 100, false],
'||' => [static fn($a, $b) => $a || $b, 90, false],
'==' => [static fn($a, $b) => \is_string($a) || \is_string($b) ? 0 == \strcmp($a, $b) : $a == $b, 140, false],