I have the following source code in a simple main:
int main(int argc, char** argv)
{
double x = atof(argv[1]);
double y = atof(argv[2]);
double res = x + std::floor((y - x) * .5 * 100 + .5)*0.01;
std::cout << res << std::endl;
}
If I run the above with 75.21 75.22 it gives me 75.22 but if i run it with 7.21 and 7.22 it gives me 7.21. Both these number differ by 0.01 so I don’t understand why this is happening?
The short answer: floating-point values are imprecise.
The long answer: What Every Computer Scientist Should Know About Floating-Point Arithmetic