Can maths calculate wrong?

Good day.

I am having a very weird situation…

It looks like the maths is wrong! Sounds crazy right… I feel like going crazy because I can`t seem to figure this out…

I am taking a decimal value(Lets call it var1), then looping with a “For each item” loop, each time deducting the same value from var1.

It all works perfectly until I loop 3 times or more AND if the decimal is something,3 (e.g. 100,3 or 9856,3) if the first decimal after the comma is NOT 0 , 5 , or 75 the calculations goes wrong. It does not make sense to me at all.

I`ll try and explain better.

For example Person A has $5000 to spend. I then use for each item loop to deduct whatever he already has on order, thus calculating his remaining spending money. There can be many orders, no problem, all calculates correct. There an be 2 orders with the value of $200.30 each, no problem. As soon as I have 3 or more orders AND the cent value is e.g. 30, 70 the calculations go wrong. If the cent value is 0 or 50 calculations are correct.

image

You can see the above snip from Console. Started of with 5000, deducted 200.30 and got 4799.7. That is correct. Now I repeat above step and get 4599.4, which is correct. If I repeat it once more, I get 4399.099999999, which is incorrect. If I deduct lets say 200.50 instead of 200.30 everything works just fine…

Please help, I am lost here.

Thank you

Hello @Frans_Fourie

This question is JS-related because our Codeless system is based on JavaScript. But similar problems exist in another language too.

I could recommend reading an answer on Stack Overflow - link, with could help understand why that happens.

Regards, Dima

In our plans to implement so called BackendlessExpression feature.
It allows changing the value in the db without the computations in the code. It will help make the calculations platform/language independent.
[BKNDLSS-26416]
[BKNDLSS-20855]