madorin/matex

Problem calcul simple addition

WebCimes opened this issue · 2 comments

Hi,

I have a problem when calculate simple sum :
$evaluator = new \Matex\Evaluator();
$evaluator->execute('0.37+0.31-0.68');

The result must be "0", but it return : "-1.1102230246252E-16"

Have you an idea of why ?

Thanks

Hi

From math point of view it should be 0. I dont have a time to look in code but it looks like data type problem double, float.
Interesting i will test it and check result. Its very good library but need some more work.

(a + b - c)

Calculated value from (a+b )0.37+0.31 which gives 0.68 is not equal to c which is 0.68.
Im not php expert but it looks like php is working on bigger floating point precision.

The solution is to change calculate() method. If boths variables are double to use PHP BC Math — BCMath Arbitrary Precision Mathematics library and set probobly max variables preciosion

OR

I found simpler solution (cast to string and then to float and make addition.
Line 181 of calculate() method, replace for:
$value =floatval((string)$value)+ floatval((string)$subTerm);