I was performing addition and subtraction on an input of decimals that are precise to the second decimal place. I tried to improve accuracy by converting them to integers through multiplying 100, but achieved opposite effects.
Consider the following code and output:
double d = 2.01;
int a = (int) (d * 100.0);
cout << a << endl;
The output is:
200
Once and for all, what are some of the best practices regarding floating-point arithmetics? Is it at all feasible to first convert the double
to an int
using some variant of the code above, and then casting it back?