1
Today I went through a strange problem, a simple calculation of the sum of the difference according to the ordination.
If in the browser console run this calculation below:
2.3+2.3+2.1
Would the expected value be 6.7 correct? Yes, but from 6.699999999999, so far so good, it is understandable....
But.. if I change the order of the values by putting the lowest first.
2.1+2.3+2.3
Surprise "mothafoca"! 6.7
Note: I can not use toFixed because I do not want to harness this number, where can fix me a problem now, but result me in something worse there in front.
A solution that happened in my head but I refuse to use it would be.
soma = 0;
[2.3, 2.3, 2.1].sort().forEach( valor => {
soma += valor
});
Inaccurate result in broken numbers calculation
– rray
I believe the strangest problem is the order of values.
– KiverTeixeira
Why 0.1 + 0.2 + 0.3 is different than 0.1 + 0.2 + 0.3?
– rray
One solution I found would be to use . toPrecision(14)
– KiverTeixeira