Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem calcul simple addition #25

Open
WebCimes opened this issue Feb 4, 2022 · 2 comments
Open

Problem calcul simple addition #25

WebCimes opened this issue Feb 4, 2022 · 2 comments

Comments

@WebCimes
Copy link

WebCimes commented Feb 4, 2022

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

@mszyca
Copy link

mszyca commented Feb 5, 2022

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.

@mszyca
Copy link

mszyca commented Feb 7, 2022

(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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants