I have the following code which minus 0.1 from 0.5 for 5 times, but the result isn't exact 0.0.
I understand why there is an error, and I'm finding a way to deal with it.
The only way I came up with is to check if abs(num - 0.0) < 0.01
, but the 0.01 value may not be suitable in all situations (too big or too small).
Is there a more elegant way to deal with it?
#include <stdio.h>
int main() {
double num = 0.5;
for (int i = 0; i < 5; i++) {
num -= 0.1;
}
printf("num=%lf %d", num, num == 0);
}