exact representation of floating points in c

前端 未结 9 949
离开以前
离开以前 2020-12-01 17:40
void main()
{
    float a = 0.7;

    if (a < 0.7)
        printf(\"c\");
    else
        printf(\"c++\");
} 

In the above question for 0.7, \"

9条回答
  •  臣服心动
    2020-12-01 18:09

    floats will be stored as described in IEEE 754: 1 bit for sign, 8 for a biased exponent, and the rest storing the fractional part.

    Think of numbers representable as floats as points on the number line, some distance apart; frequently, decimal fractions will fall in between these points, and the nearest representation will be used; this leads to the counterintuitive results you describe.

    "What every computer scientist should know about floating point arithmetic" should answer all your questions in detail.

提交回复
热议问题