Strange output when comparing same float values?

后端 未结 3 1559
[愿得一人]
[愿得一人] 2020-12-18 15:33

Comparing Same Float Values In C

strange output in comparison of float with float literal

Float addition promoted to double?


I read the above l

3条回答
  •  庸人自扰
    2020-12-18 16:09

    As you have read the links regarding problems with floating point types and comparisons, you are probably expecting that 0.5 is rounded during conversion and hence the comparison should fail. But 0.5 is a power of 2 and can be represented perfectly without any rounding in a float or double type variable. Therefore the comparison results in TRUE.

    After your edited your question: Yes, if you took 0.1 or one of the other values you mention, you should run into the else part.

提交回复
热议问题