0

I am wondering that why only number 4.20 showing the number precision error, is there any special reason due to its binary representation?

#include <stdio.h>

int main() {
    
    float a = 4.20;
    float b = 3.20;
    float c = 5.20;

    printf("%f\n",a*100);// 419.999969 -> error

    printf("%f\n",b*100);// 320.000000

    printf("%f",c*100); // 520.000000


    
    return 0;
}
Ian Abbott
  • 15,083
  • 19
  • 33
Hmm
  • 23
  • 1
  • 7
  • Suggestion (unrelated): forget `float` exists and use only exclusively `double`. You will still have the exact same problem (**allmost all floating-point numbers are approximations**), but it will not be seen with a `%f` printf – pmg Feb 23 '22 at 13:50
  • As you will learn from the linked question, none of the numbers is exact. Try different formats like `"%.1f\n"`, `"%.3f\n"` or `"%.10f\n"` and see. – the busybee Feb 23 '22 at 14:10

0 Answers0