0

I was solving a problem in C while I came across another problem. Briefly while running the below code the output is 50.009998. How to correct the answer and why is this happening.

#include<stdio.h>
int main()
{
    float x;
    x = 50.000000 + 0.010000;
    printf("%f\n", x);
    return 0;
}
Priyam
  • 21
  • 1
  • 1
    besides what @Acorn has suggested, to fix the code use double instead of float for more precision. – Breakpoint Apr 16 '20 at 20:02
  • Try defining `double x;`. Or otherwise, unless you are actually interested to 6 decimals (that's %f default) you can set `printf("%0.2f\n", x);` that will limit your print to 2 decimals (that are not affected to float precision error) – Roberto Caboni Apr 16 '20 at 20:04

0 Answers0