Precision of multiplication by 1.0 and int to float conversion

后端 未结 4 1049
一个人的身影
一个人的身影 2020-12-16 08:53

Is it safe to assume that the condition (int)(i * 1.0f) == i is true for any integer i?

4条回答
  •  失恋的感觉
    2020-12-16 09:51

    No, the behavior is implementation defined because C and C++ don't require IEEE-754, even though that is the most common representation by far.

    To be sure that IEEE-754 is used:

    • in C, use #ifdef __STDC_IEC_559__
    • in C++, use the std::numeric_limits::is_iec559 constants

提交回复
热议问题