This question already has answers here:
Floating point numbers do not work as expected
                                
                                    (6个答案)
                                
                        
                                5年前关闭。
            
                    
我无法理解以下程序生成的输出:

#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


尽管d为245.3,但它正在打印245.3000003。
在加上65之后,它是不准确的。

最佳答案

浮动有精度问题。原因是0.1不能完美地用float表示。

从以下内容开始读得很好:-What every computer scientist should know about floating-point arithmetic

10-08 06:54