ar86
July 19, 2022, 10:40am
1
I made a simple subtraction operation like this:
A=0.5
B=0.35
A-B=0.1500000000002 (wrong)
this should be 0.15
For comparison, I made a similar operation:
A=0.5
C=0.15
A-C=0.35 (right)
Is this a bug? Hope this gets fixed soon.
GDevelop version : 5.0.138
A roundoff error, also called rounding error, is the difference between the result produced by a given algorithm using exact arithmetic and the result produced by the same algorithm using finite-precision, rounded arithmetic. Rounding errors are due to inexactness in the representation of real numbers and the arithmetic operations done with them. This is a form of quantization error. When using approximation equations or algorithms, especially when using finitely many digits to repre When a sequ...
tl;dr That’s due to how computers stores non-integer numbers (floats) and do math on them, and this cannot be fixed.
ar86
July 19, 2022, 1:31pm
3
oh. maybe if I SubStr 0,150000000… to 4 letter (0,15) it will be correct. but I’m not sure.
If you know you only want 2 decimals, you could always use the roundTo() expression, it lets you round to a specific decimal point, so it’d be roundTo(yourmathhere,2).
ar86
July 19, 2022, 2:17pm
5
Yes maybe roundTo or SubStr. I’m affraid if roundTo I will get 0,2.
So I try to multiply it with a large number , like 1000000000.
then do the subtraction to avoid the error.
(0.5x1000000000)-(0.35x1000000000)
then divide the result with 1000000000 again.
Maybe it work.
Can you also multiply by 100, use the floor function and divide by 100?
Or something using the floor function.?
Might be worth looking into the toFixed? There should be a handy extension for is.
Thanks Silver, good to know it’s part of an extension. I have already got it in my own function but I might move to this if anymore of the functions provided are needed.