It was me, trying to do a pretty basic program to know if a variable had a second decimal place or not. But I came across a bug, which in my view is bizarre.
Code:
#include <stdio.h>
#include <math.h>
int main(void) {
float f = 1.10;
float truncado = (f - trunc(f)) * 10;
printf("Truncado: %f\n", truncado);
float mod = fmodf(truncado, 1.0f);
printf("Mod: %f\n", mod);
if (mod != 0){
printf("%.2f", f);
}
else{
printf("%.1f", f);
}
return 0;
}
Output:
Truncado: 1.000000
Mod: 0.000000
1.10
If the program is pointing out that the variable mod
is equal to zero, why is it giving true in if?
See the Ideone .