1
I was trying to make a very basic little program to know if a variable had a second decimal place or not. However 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 because it is giving true in the if?
See on Ideone.
Exact, as suggested, the correct is to use your IF in this way: if ((int)mod != 0), with this, you will get the expected result.
– FabianoLothor