I am doing some floating point calculations and the results are not as accurate as I want them to be.
This is the algorithm:
... center = (max_x + min_x) / 2 distance = old_x - center new_x = center + (distance * factor) return new_x
min_x, max_x, and old_x are all floats.
I believe that the greatest error is introduced when I’m taking the average of the max and the min, and then the error is multiplied by the factor (which can be a float).
How can I minimize the error due to FP computation so that new_x is as precise as it can be?
If old_x and center are close then you’re losing precision.
It’s called Loss of significance
You could change the calculation so the subtraction happenS in the end: