Skip to content

Commit a944fe4

Browse files
authored
Bcmath (#115)
* Add useBCMath * Support for % operator (mod)
1 parent cbada2b commit a944fe4

3 files changed

Lines changed: 289 additions & 7 deletions

File tree

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# A simple and extensible math expressions calculator
44

55
## Features:
6-
* Built in support for +, -, *, / and power (^) operators
6+
* Built in support for +, -, *, %, / and power (^) operators
77
* Paratheses () and arrays [] are fully supported
88
* Logical operators (==, !=, <, <, >=, <=, &&, ||)
99
* Built in support for most PHP math functions
@@ -101,7 +101,7 @@ $executor->calculate('avarage(1, 3, 4, 8)'); // 4
101101
```
102102

103103
## Operators:
104-
Default operators: `+ - * / ^`
104+
Default operators: `+ - * / % ^`
105105

106106
Add custom operator to executor:
107107

@@ -111,7 +111,7 @@ use NXP\Classes\Operator;
111111
$executor->addOperator(new Operator(
112112
'%', // Operator sign
113113
false, // Is right associated operator
114-
170, // Operator priority
114+
180, // Operator priority
115115
function (&$stack)
116116
{
117117
$op2 = array_pop($stack);
@@ -189,6 +189,10 @@ $calculator->setVarNotFoundHandler(
189189
);
190190
```
191191

192+
## Floating Point BCMath Support
193+
By default, `MathExecutor` uses PHP floating point math, but if you need a fixed precision, call **useBCMath()**. Precision defaults to 2 decimal points, or pass the required number.
194+
`WARNING`: Functions may return a PHP floating point number. By doing the basic math functions on the results, you will get back a fixed number of decimal points. Use a plus sign in from of any stand alone function to return the proper number of decimal places.
195+
192196
## Division By Zero Support:
193197
Division by zero throws a `\NXP\Exception\DivisionByZeroException` by default
194198
```php

src/NXP/MathExecutor.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public function removeVars() : self
263263
*
264264
* @return array<Operator> of operator class names
265265
*/
266-
public function getOperators()
266+
public function getOperators() : array
267267
{
268268
return $this->operators;
269269
}
@@ -279,6 +279,18 @@ public function getFunctions() : array
279279
return $this->functions;
280280
}
281281

282+
/**
283+
* Remove a specific operator
284+
*
285+
* @return array<Operator> of operator class names
286+
*/
287+
public function removeOperator(string $operator) : self
288+
{
289+
unset($this->operators[$operator]);
290+
291+
return $this;
292+
}
293+
282294
/**
283295
* Set division by zero returns zero instead of throwing DivisionByZeroException
284296
*/
@@ -301,16 +313,39 @@ public function getCache() : array
301313
/**
302314
* Clear token's cache
303315
*/
304-
public function clearCache() : void
316+
public function clearCache() : self
305317
{
306318
$this->cache = [];
319+
320+
return $this;
321+
}
322+
323+
public function useBCMath(int $scale = 2) : self
324+
{
325+
\bcscale($scale);
326+
$this->addOperator(new Operator('+', false, 170, static fn($a, $b) => \bcadd("{$a}", "{$b}")));
327+
$this->addOperator(new Operator('-', false, 170, static fn($a, $b) => \bcsub("{$a}", "{$b}")));
328+
$this->addOperator(new Operator('uNeg', false, 200, static fn($a) => \bcsub('0.0', "{$a}")));
329+
$this->addOperator(new Operator('*', false, 180, static fn($a, $b) => \bcmul("{$a}", "{$b}")));
330+
$this->addOperator(new Operator('/', false, 180, static function($a, $b) {
331+
/** @todo PHP8: Use throw as expression -> static fn($a, $b) => 0 == $b ? throw new DivisionByZeroException() : $a / $b */
332+
if (0 == $b) {
333+
throw new DivisionByZeroException();
334+
}
335+
336+
return \bcdiv("{$a}", "{$b}");
337+
}));
338+
$this->addOperator(new Operator('^', true, 220, static fn($a, $b) => \bcpow("{$a}", "{$b}")));
339+
$this->addOperator(new Operator('%', false, 180, static fn($a, $b) => \bcmod("{$a}", "{$b}")));
340+
341+
return $this;
307342
}
308343

309344
/**
310345
* Set default operands and functions
311346
* @throws ReflectionException
312347
*/
313-
protected function addDefaults() : void
348+
protected function addDefaults() : self
314349
{
315350
foreach ($this->defaultOperators() as $name => $operator) {
316351
[$callable, $priority, $isRightAssoc] = $operator;
@@ -323,6 +358,8 @@ protected function addDefaults() : void
323358

324359
$this->onVarValidation = [$this, 'defaultVarValidation'];
325360
$this->variables = $this->defaultVars();
361+
362+
return $this;
326363
}
327364

328365
/**
@@ -352,6 +389,7 @@ static function($a, $b) { /** @todo PHP8: Use throw as expression -> static fn($
352389
false
353390
],
354391
'^' => [static fn($a, $b) => \pow($a, $b), 220, true],
392+
'%' => [static fn($a, $b) => $a % $b, 180, false],
355393
'&&' => [static fn($a, $b) => $a && $b, 100, false],
356394
'||' => [static fn($a, $b) => $a || $b, 90, false],
357395
'==' => [static fn($a, $b) => \is_string($a) || \is_string($b) ? 0 == \strcmp($a, $b) : $a == $b, 140, false],

tests/MathTest.php

Lines changed: 241 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public function providerExpressions()
113113
['tanh(1.5)'],
114114

115115
['0.1 + 0.2'],
116+
['0.1 + 0.2 - 0.3'],
116117
['1 + 2'],
117118

118119
['0.1 - 0.2'],
@@ -246,7 +247,246 @@ public function providerExpressions()
246247
['max(1,2,4.9,3)'],
247248
['min(1,2,4.9,3)'],
248249
['max([1,2,4.9,3])'],
249-
['min([1,2,4.9,3])']
250+
['min([1,2,4.9,3])'],
251+
252+
['4 % 4'],
253+
['7 % 4'],
254+
['99 % 4'],
255+
['123 % 7'],
256+
];
257+
}
258+
259+
/**
260+
* @dataProvider bcMathExpressions
261+
*/
262+
public function testBCMathCalculating(string $expression, string $expected = '') : void
263+
{
264+
$calculator = new MathExecutor();
265+
$calculator->useBCMath();
266+
267+
if ('' === $expected)
268+
{
269+
$expected = $expression;
270+
}
271+
272+
/** @var float $phpResult */
273+
eval('$phpResult = ' . $expected . ';');
274+
275+
try {
276+
$result = $calculator->execute($expression);
277+
} catch (Exception $e) {
278+
$this->fail(\sprintf('Exception: %s (%s:%d), expression was: %s', \get_class($e), $e->getFile(), $e->getLine(), $expression));
279+
}
280+
$this->assertEquals($phpResult, $result, "Expression was: {$expression}");
281+
}
282+
283+
/**
284+
* Expressions data provider
285+
*
286+
* Most tests can go in here. The idea is that each expression will be evaluated by MathExecutor and by PHP with eval.
287+
* The results should be the same. If they are not, then the test fails. No need to add extra test unless you are doing
288+
* something more complex and not a simple mathmatical expression.
289+
*/
290+
public function bcMathExpressions()
291+
{
292+
return [
293+
['-5'],
294+
['-5+10'],
295+
['4-5'],
296+
['4 -5'],
297+
['(4*2)-5'],
298+
['(4*2) - 5'],
299+
['4*-5'],
300+
['4 * -5'],
301+
['+5'],
302+
['+(3+2)'],
303+
['+(+3+2)'],
304+
['+(-3+2)'],
305+
['-5'],
306+
['-(-5)'],
307+
['-(+5)'],
308+
['+(-5)'],
309+
['+(+5)'],
310+
['-(3+2)'],
311+
['-(-3+-2)'],
312+
313+
['abs(1.5)'],
314+
['acos(0.15)'],
315+
['acosh(1.5)'],
316+
['asin(0.15)'],
317+
['atan(0.15)'],
318+
['atan2(1.5, 3.5)'],
319+
['atanh(0.15)'],
320+
['bindec("10101")'],
321+
['ceil(1.5)'],
322+
['cos(1.5)'],
323+
['cosh(1.5)'],
324+
['decbin("15")'],
325+
['dechex("15")'],
326+
['decoct("15")'],
327+
['deg2rad(1.5)'],
328+
['exp(1.5)'],
329+
['expm1(1.5)'],
330+
['floor(1.5)'],
331+
['fmod(1.5, 3.5)'],
332+
['hexdec("abcdef")'],
333+
['hypot(1.5, 3.5)'],
334+
['intdiv(10, 2)'],
335+
['log(1.5)'],
336+
['log10(1.5)'],
337+
['log1p(1.5)'],
338+
['max(1.5, 3.5)'],
339+
['min(1.5, 3.5)'],
340+
['octdec("15")'],
341+
['pi()'],
342+
['pow(1.5, 3.5)'],
343+
['rad2deg(1.5)'],
344+
['round(1.5)'],
345+
['sin(1.5)'],
346+
['sin(12)'],
347+
['+sin(12)'],
348+
['-sin(12)', '0.53'],
349+
['sinh(1.5)'],
350+
['sqrt(1.5)'],
351+
['tan(1.5)'],
352+
['tanh(1.5)'],
353+
354+
['0.1 + 0.2'],
355+
['0.1 + 0.2 - 0.3'],
356+
['1 + 2'],
357+
358+
['0.1 - 0.2'],
359+
['1 - 2'],
360+
361+
['0.1 * 2'],
362+
['1 * 2'],
363+
364+
['0.1 / 0.2'],
365+
['1 / 2'],
366+
367+
['2 * 2 + 3 * 3'],
368+
['2 * 2 / 3 * 3', '3.99'],
369+
['2 / 2 / 3 / 3', '0.11'],
370+
['2 / 2 * 3 / 3'],
371+
['2 / 2 * 3 * 3'],
372+
373+
['1 + 0.6 - 3 * 2 / 50'],
374+
375+
['(5 + 3) * -1'],
376+
377+
['-2- 2*2'],
378+
['2- 2*2'],
379+
['2-(2*2)'],
380+
['(2- 2)*2'],
381+
['2 + 2*2'],
382+
['2+ 2*2'],
383+
['2+2*2'],
384+
['(2+2)*2'],
385+
['(2 + 2)*-2'],
386+
['(2+-2)*2'],
387+
388+
['1 + 2 * 3 / (min(1, 5) + 2 + 1)'],
389+
['1 + 2 * 3 / (min(1, 5) - 2 + 5)'],
390+
['1 + 2 * 3 / (min(1, 5) * 2 + 1)'],
391+
['1 + 2 * 3 / (min(1, 5) / 2 + 1)'],
392+
['1 + 2 * 3 / (min(1, 5) / 2 * 1)'],
393+
['1 + 2 * 3 / (min(1, 5) / 2 / 1)'],
394+
['1 + 2 * 3 / (3 + min(1, 5) + 2 + 1)', '1.85'],
395+
['1 + 2 * 3 / (3 - min(1, 5) - 2 + 1)'],
396+
['1 + 2 * 3 / (3 * min(1, 5) * 2 + 1)', '1.85'],
397+
['1 + 2 * 3 / (3 / min(1, 5) / 2 + 1)'],
398+
399+
['(1 + 2) * 3 / (3 / min(1, 5) / 2 + 1)'],
400+
401+
['sin(10) * cos(50) / min(10, 20/2)', '-0.05'],
402+
['sin(10) * cos(50) / min(10, (20/2))', '-0.05'],
403+
['sin(10) * cos(50) / min(10, (max(10,20)/2))', '-0.05'],
404+
405+
['1 + "2" / 3', '1.66'],
406+
["1.5 + '2.5' / 4", '2.12'],
407+
['1.5 + "2.5" * ".5"'],
408+
409+
['-1 + -2'],
410+
['-1+-2'],
411+
['-1- -2'],
412+
['-1/-2'],
413+
['-1*-2'],
414+
415+
['(1+2+3+4-5)*7/100'],
416+
['(-1+2+3+4- 5)*7/100'],
417+
['(1+2+3+4- 5)*7/100'],
418+
['( 1 + 2 + 3 + 4 - 5 ) * 7 / 100'],
419+
420+
['1 && 0'],
421+
['1 && 0 && 1'],
422+
['1 || 0'],
423+
['1 && 0 || 1'],
424+
425+
['5 == 3'],
426+
['5 == 5'],
427+
['5 != 3'],
428+
['5 != 5'],
429+
['5 > 3'],
430+
['3 > 5'],
431+
['3 >= 5'],
432+
['3 >= 3'],
433+
['3 < 5'],
434+
['5 < 3'],
435+
['3 <= 5'],
436+
['5 <= 5'],
437+
['10 < 9 || 4 > (2+1)'],
438+
['10 < 9 || 4 > (-2+1)'],
439+
['10 < 9 || 4 > (2+1) && 5 == 5 || 4 != 6 || 3 >= 4 || 3 <= 7'],
440+
441+
['1 + 5 == 3 + 1'],
442+
['1 + 5 == 5 + 1'],
443+
['1 + 5 != 3 + 1'],
444+
['1 + 5 != 5 + 1'],
445+
['1 + 5 > 3 + 1'],
446+
['1 + 3 > 5 + 1'],
447+
['1 + 3 >= 5 + 1'],
448+
['1 + 3 >= 3 + 1'],
449+
['1 + 3 < 5 + 1'],
450+
['1 + 5 < 3 + 1'],
451+
['1 + 3 <= 5 + 1'],
452+
['1 + 5 <= 5 + 1'],
453+
454+
['(-4)'],
455+
['(-4 + 5)'],
456+
['(3 * 1)'],
457+
['(-3 * -1)'],
458+
['1 + (-3 * -1)'],
459+
['1 + ( -3 * 1)'],
460+
['1 + (3 *-1)'],
461+
['1 - 0'],
462+
['1-0'],
463+
464+
['-(1.5)'],
465+
['-log(4)', '-1.38'],
466+
['0-acosh(1.5)', '-0.96'],
467+
['-acosh(1.5)', '-0.96'],
468+
['-(-4)'],
469+
['-(-4 + 5)'],
470+
['-(3 * 1)'],
471+
['-(-3 * -1)'],
472+
['-1 + (-3 * -1)'],
473+
['-1 + ( -3 * 1)'],
474+
['-1 + (3 *-1)'],
475+
['-1 - 0'],
476+
['-1-0'],
477+
['-(4*2)-5'],
478+
['-(4*-2)-5'],
479+
['-(-4*2) - 5'],
480+
['-4*-5'],
481+
['max(1,2,4.9,3)'],
482+
['min(1,2,4.9,3)'],
483+
['max([1,2,4.9,3])'],
484+
['min([1,2,4.9,3])'],
485+
486+
['4 % 4'],
487+
['7 % 4'],
488+
['99 % 4'],
489+
['123 % 7'],
250490
];
251491
}
252492

0 commit comments

Comments
 (0)