-3

I can not understand the output generated by the following program:

#include <stdio.h>
main()
{
    float d =245.3;
    char c = 'A';
    printf("d = %f",d);
    d = d+c;
    printf("\nd = %f",d);
    getch();
    return 0;
}

Output:
d = 245.3000003
d = 310.2999988

Though d was 245.3, but it is printing 245.3000003. And after adding 65, it is not accurate.

1 Answers1

0

Floating have a precision problem. And the reason is that 0.1 is not perfectly represented with float.

And a good read to start with:- What every computer scientist should know about floating-point arithmetic

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 2
    I'm not sure that's the best place to *start*. You might also try section 14 of the [comp.lang.c FAQ](http://www.c-faq.com/). – Keith Thompson Aug 26 '14 at 18:41