denissimon / formula-parser

Parsing and evaluating mathematical formulas given as strings.
MIT License
76 stars 24 forks source link

Calculation error #11

Closed Firebird75 closed 5 days ago

Firebird75 commented 1 week ago

Hello,

The following calculation : -((5+5)*(1+-2))-10

will throw a warning : Warning: Undefined array key 4 in FormulaParser.php on line 327

And an invalid result : 10

The expected result is rather 0

denissimon commented 1 week ago

Hello, I tried running your example using several versions of PHP, including 8.3.12, and every time it gives the correct expected result.

Firebird75 commented 1 week ago

Tried again and still getting the same error : My code : $formula = '-((5+5)*(1+-2))-10'; $precision = 2; try { $parser = new FormulaParser($formula, $precision); $result = $parser->getResult(); // [0 => 'done', 1 => 16.38] print_r($result); } catch (\Exception $e) { echo $e->getMessage(), "\n"; } Warning: Undefined array key 4 in FormulaParser.php on line 327 Result : Array ( [0] => done [1] => 10 )

Php release : 8.2.25

denissimon commented 5 days ago

Still cannot reproduce this. I tested this both locally on my device and on https://onlinephp.io with different PHP versions and the result was always:

Array
(
    [0] => done
    [1] => 0
)

I also included your example (thanks for it btw) in FormulaParserTest.php and all tests pass successfully using phpunit.

So the problem might be related to your PHP settings or environment. I also suggest you update your PHP version to the latest.