Strange behavior when casting an int to float in C

前端 未结 5 1734
予麋鹿
予麋鹿 2021-02-18 14:27

I have a doubt concerning the output of the following C program. I tried to compile it using both Visual C++ 6.0 and MinGW32 (gcc 3.4.2).

#include 

        
5条回答
  •  情书的邮戳
    2021-02-18 14:49

    some of you guys said that it's a optimization bug, but i am kind of disagree. i think it's a reasonable floating point precision error and a good example showing people how floating point works.

    http://ideone.com/Ssw8GR

    maybe OP could try to paste my program into your computer and try to compile with your compiler and see what happens. or try:

    http://ideone.com/OGypBC

    (with explicit float conversion).

    anyway, if we calculate the error, it's 4.656612875245797e-10 that much, and should be considered as pretty precise.

    it could relate to the preference of printf too.

提交回复
热议问题