diff options
author | Fatih Kızmaz <barka_21@hotmail.com> | 2022-12-26 18:35:27 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-26 18:35:27 +0300 |
commit | 3a18c7d47fdb64fd93963386da89df1170da80ed (patch) | |
tree | 6754a641e68adaa5ecb1af6d0f0585ba3c29f982 /src | |
parent | 84ac3bbdf07c01e021e0b5b1b462e859687f410a (diff) |
Added Not(!) logical operator and median function with tests. (#121)
Diffstat (limited to 'src')
-rw-r--r-- | src/NXP/MathExecutor.php | 221 |
1 files changed, 120 insertions, 101 deletions
diff --git a/src/NXP/MathExecutor.php b/src/NXP/MathExecutor.php index c857e99..84dd3d9 100644 --- a/src/NXP/MathExecutor.php +++ b/src/NXP/MathExecutor.php @@ -85,10 +85,10 @@ class MathExecutor /** * Execute expression * - * @throws Exception\IncorrectBracketsException * @throws Exception\IncorrectExpressionException * @throws Exception\UnknownOperatorException * @throws UnknownVariableException + * @throws Exception\IncorrectBracketsException * @return int|float|string|null */ public function execute(string $expression, bool $cache = true) @@ -113,7 +113,7 @@ class MathExecutor /** * Add function to executor * - * @param string $name Name of function + * @param string $name Name of function * @param callable|null $function Function * * @throws ReflectionException @@ -182,8 +182,8 @@ class MathExecutor /** * Add variables to executor * - * @param array<string, float|int|string> $variables - * @param bool $clear Clear previous variables + * @param array<string, float|int|string> $variables + * @param bool $clear Clear previous variables * @throws \Exception */ public function setVars(array $variables, bool $clear = true) : self @@ -285,7 +285,7 @@ class MathExecutor */ public function setDivisionByZeroIsZero() : self { - $this->addOperator(new Operator('/', false, 180, static fn ($a, $b) => 0 == $b ? 0 : $a / $b)); + $this->addOperator(new Operator('/', false, 180, static fn($a, $b) => 0 == $b ? 0 : $a / $b)); return $this; } @@ -311,23 +311,23 @@ class MathExecutor 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; + \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; } /** @@ -359,15 +359,16 @@ class MathExecutor protected function defaultOperators() : array { return [ - '+' => [static fn ($a, $b) => $a + $b, 170, false], - '-' => [static fn ($a, $b) => $a - $b, 170, false], + '+' => [static fn($a, $b) => $a + $b, 170, false], + '-' => [static fn($a, $b) => $a - $b, 170, false], // unary positive token - 'uPos' => [static fn ($a) => $a, 200, false], + 'uPos' => [static fn($a) => $a, 200, false], // unary minus token - 'uNeg' => [static fn ($a) => 0 - $a, 200, false], - '*' => [static fn ($a, $b) => $a * $b, 180, false], + 'uNeg' => [static fn($a) => 0 - $a, 200, false], + '*' => [static fn($a, $b) => $a * $b, 180, false], '/' => [ - static function($a, $b) { /** @todo PHP8: Use throw as expression -> static fn($a, $b) => 0 == $b ? throw new DivisionByZeroException() : $a / $b */ + 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(); } @@ -377,16 +378,17 @@ class MathExecutor 180, false ], - '^' => [static fn ($a, $b) => $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], - '!=' => [static fn ($a, $b) => \is_string($a) || \is_string($b) ? 0 != \strcmp($a, $b) : $a != $b, 140, false], - '>=' => [static fn ($a, $b) => $a >= $b, 150, false], - '>' => [static fn ($a, $b) => $a > $b, 150, false], - '<=' => [static fn ($a, $b) => $a <= $b, 150, false], - '<' => [static fn ($a, $b) => $a < $b, 150, false], + '^' => [static fn($a, $b) => $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], + '!=' => [static fn($a, $b) => \is_string($a) || \is_string($b) ? 0 != \strcmp($a, $b) : $a != $b, 140, false], + '>=' => [static fn($a, $b) => $a >= $b, 150, false], + '>' => [static fn($a, $b) => $a > $b, 150, false], + '<=' => [static fn($a, $b) => $a <= $b, 150, false], + '<' => [static fn($a, $b) => $a < $b, 150, false], + '!' => [static fn($a) => ! $a, 190, false], ]; } @@ -399,28 +401,28 @@ class MathExecutor protected function defaultFunctions() : array { return [ - 'abs' => static fn ($arg) => \abs($arg), - 'acos' => static fn ($arg) => \acos($arg), - 'acosh' => static fn ($arg) => \acosh($arg), - 'arcsin' => static fn ($arg) => \asin($arg), - 'arcctg' => static fn ($arg) => M_PI / 2 - \atan($arg), - 'arccot' => static fn ($arg) => M_PI / 2 - \atan($arg), - 'arccotan' => static fn ($arg) => M_PI / 2 - \atan($arg), - 'arcsec' => static fn ($arg) => \acos(1 / $arg), - 'arccosec' => static fn ($arg) => \asin(1 / $arg), - 'arccsc' => static fn ($arg) => \asin(1 / $arg), - 'arccos' => static fn ($arg) => \acos($arg), - 'arctan' => static fn ($arg) => \atan($arg), - 'arctg' => static fn ($arg) => \atan($arg), - 'array' => static fn (...$args) => $args, - 'asin' => static fn ($arg) => \asin($arg), - 'atan' => static fn ($arg) => \atan($arg), - 'atan2' => static fn ($arg1, $arg2) => \atan2($arg1, $arg2), - 'atanh' => static fn ($arg) => \atanh($arg), - 'atn' => static fn ($arg) => \atan($arg), + 'abs' => static fn($arg) => \abs($arg), + 'acos' => static fn($arg) => \acos($arg), + 'acosh' => static fn($arg) => \acosh($arg), + 'arcsin' => static fn($arg) => \asin($arg), + 'arcctg' => static fn($arg) => M_PI / 2 - \atan($arg), + 'arccot' => static fn($arg) => M_PI / 2 - \atan($arg), + 'arccotan' => static fn($arg) => M_PI / 2 - \atan($arg), + 'arcsec' => static fn($arg) => \acos(1 / $arg), + 'arccosec' => static fn($arg) => \asin(1 / $arg), + 'arccsc' => static fn($arg) => \asin(1 / $arg), + 'arccos' => static fn($arg) => \acos($arg), + 'arctan' => static fn($arg) => \atan($arg), + 'arctg' => static fn($arg) => \atan($arg), + 'array' => static fn(...$args) => $args, + 'asin' => static fn($arg) => \asin($arg), + 'atan' => static fn($arg) => \atan($arg), + 'atan2' => static fn($arg1, $arg2) => \atan2($arg1, $arg2), + 'atanh' => static fn($arg) => \atanh($arg), + 'atn' => static fn($arg) => \atan($arg), 'avg' => static function($arg1, ...$args) { - if (\is_array($arg1)){ - if (0 === \count($arg1)){ + if (\is_array($arg1)) { + if (0 === \count($arg1)) { throw new \InvalidArgumentException('avg() must have at least one argument!'); } @@ -431,27 +433,27 @@ class MathExecutor return \array_sum($args) / \count($args); }, - 'bindec' => static fn ($arg) => \bindec($arg), - 'ceil' => static fn ($arg) => \ceil($arg), - 'cos' => static fn ($arg) => \cos($arg), - 'cosec' => static fn ($arg) => 1 / \sin($arg), - 'csc' => static fn ($arg) => 1 / \sin($arg), - 'cosh' => static fn ($arg) => \cosh($arg), - 'ctg' => static fn ($arg) => \cos($arg) / \sin($arg), - 'cot' => static fn ($arg) => \cos($arg) / \sin($arg), - 'cotan' => static fn ($arg) => \cos($arg) / \sin($arg), - 'cotg' => static fn ($arg) => \cos($arg) / \sin($arg), - 'ctn' => static fn ($arg) => \cos($arg) / \sin($arg), - 'decbin' => static fn ($arg) => \decbin($arg), - 'dechex' => static fn ($arg) => \dechex($arg), - 'decoct' => static fn ($arg) => \decoct($arg), - 'deg2rad' => static fn ($arg) => \deg2rad($arg), - 'exp' => static fn ($arg) => \exp($arg), - 'expm1' => static fn ($arg) => \expm1($arg), - 'floor' => static fn ($arg) => \floor($arg), - 'fmod' => static fn ($arg1, $arg2) => \fmod($arg1, $arg2), - 'hexdec' => static fn ($arg) => \hexdec($arg), - 'hypot' => static fn ($arg1, $arg2) => \hypot($arg1, $arg2), + 'bindec' => static fn($arg) => \bindec($arg), + 'ceil' => static fn($arg) => \ceil($arg), + 'cos' => static fn($arg) => \cos($arg), + 'cosec' => static fn($arg) => 1 / \sin($arg), + 'csc' => static fn($arg) => 1 / \sin($arg), + 'cosh' => static fn($arg) => \cosh($arg), + 'ctg' => static fn($arg) => \cos($arg) / \sin($arg), + 'cot' => static fn($arg) => \cos($arg) / \sin($arg), + 'cotan' => static fn($arg) => \cos($arg) / \sin($arg), + 'cotg' => static fn($arg) => \cos($arg) / \sin($arg), + 'ctn' => static fn($arg) => \cos($arg) / \sin($arg), + 'decbin' => static fn($arg) => \decbin($arg), + 'dechex' => static fn($arg) => \dechex($arg), + 'decoct' => static fn($arg) => \decoct($arg), + 'deg2rad' => static fn($arg) => \deg2rad($arg), + 'exp' => static fn($arg) => \exp($arg), + 'expm1' => static fn($arg) => \expm1($arg), + 'floor' => static fn($arg) => \floor($arg), + 'fmod' => static fn($arg1, $arg2) => \fmod($arg1, $arg2), + 'hexdec' => static fn($arg) => \hexdec($arg), + 'hypot' => static fn($arg1, $arg2) => \hypot($arg1, $arg2), 'if' => function($expr, $trueval, $falseval) { if (true === $expr || false === $expr) { $exres = $expr; @@ -465,39 +467,56 @@ class MathExecutor return $this->execute($falseval); }, - 'intdiv' => static fn ($arg1, $arg2) => \intdiv($arg1, $arg2), - 'ln' => static fn ($arg) => \log($arg), - 'lg' => static fn ($arg) => \log10($arg), - 'log' => static fn ($arg) => \log($arg), - 'log10' => static fn ($arg) => \log10($arg), - 'log1p' => static fn ($arg) => \log1p($arg), + 'intdiv' => static fn($arg1, $arg2) => \intdiv($arg1, $arg2), + 'ln' => static fn($arg) => \log($arg), + 'lg' => static fn($arg) => \log10($arg), + 'log' => static fn($arg) => \log($arg), + 'log10' => static fn($arg) => \log10($arg), + 'log1p' => static fn($arg) => \log1p($arg), 'max' => static function($arg1, ...$args) { - if (\is_array($arg1) && 0 === \count($arg1)){ + if (\is_array($arg1) && 0 === \count($arg1)) { throw new \InvalidArgumentException('max() must have at least one argument!'); } return \max(\is_array($arg1) ? $arg1 : [$arg1, ...$args]); }, + 'median' => static function($arg1, ...$args) { + if (\is_array($arg1)) { + if (0 === \count($arg1)) { + throw new \InvalidArgumentException('Array must contain at least one element!'); + } + + $finalArgs = $arg1; + } else { + $finalArgs = [$arg1, ...$args]; + } + + $count = \count($finalArgs); + \sort($finalArgs); + $index = \floor($count / 2); + + return ($count & 1) ? $finalArgs[$index] : ($finalArgs[$index - 1] + $finalArgs[$index]) / 2; + }, 'min' => static function($arg1, ...$args) { - if (\is_array($arg1) && 0 === \count($arg1)){ + if (\is_array($arg1) && 0 === \count($arg1)) { throw new \InvalidArgumentException('min() must have at least one argument!'); } return \min(\is_array($arg1) ? $arg1 : [$arg1, ...$args]); }, - 'octdec' => static fn ($arg) => \octdec($arg), - 'pi' => static fn () => M_PI, - 'pow' => static fn ($arg1, $arg2) => $arg1 ** $arg2, - 'rad2deg' => static fn ($arg) => \rad2deg($arg), - 'round' => static fn ($num, int $precision = 0) => \round($num, $precision), - 'sin' => static fn ($arg) => \sin($arg), - 'sinh' => static fn ($arg) => \sinh($arg), - 'sec' => static fn ($arg) => 1 / \cos($arg), - 'sqrt' => static fn ($arg) => \sqrt($arg), - 'tan' => static fn ($arg) => \tan($arg), - 'tanh' => static fn ($arg) => \tanh($arg), - 'tn' => static fn ($arg) => \tan($arg), - 'tg' => static fn ($arg) => \tan($arg) + 'octdec' => static fn($arg) => \octdec($arg), + 'pi' => static fn() => M_PI, + 'pow' => static fn($arg1, $arg2) => $arg1 ** $arg2, + 'rad2deg' => static fn($arg) => \rad2deg($arg), + 'round' => static fn($num, int $precision = 0) => \round($num, $precision), + 'sin' => static fn($arg) => \sin($arg), + 'sinh' => static fn($arg) => \sinh($arg), + 'sec' => static fn($arg) => 1 / \cos($arg), + 'sqrt' => static fn($arg) => \sqrt($arg), + 'tan' => static fn($arg) => \tan($arg), + 'tanh' => static fn($arg) => \tanh($arg), + 'tn' => static fn($arg) => \tan($arg), + 'tg' => static fn($arg) => \tan($arg) ]; } |