I've been reading about the issues with floating point precision in C. I need 9 points of precision but as we can see this isn't working:
double d_lower = 0.123456789;
double d_upper = 123456789.0;
d_upper = d_upper + d_lower;
printf("d_upper: %.11f\n", d_upper);
// Outputs: d_upper: 123456789.12345679104
// Desired: 123456789.123456789
There are various posts about WHY this is happening on SO, such as these: Why Are Floating Point Numbers Inaccurate? Why can't decimal numbers be represented exactly in binary?
But I can't find any infomration on how to solve this problem? Is there a way to work around it? I need 9 points of accuracy, other people must have the same problem. Is it possible to make a union value that achieves this maybe?
My C skills are fairly basic so I can understand the problem but I can't see how to resolve it or work around it.