What is the result of casting float +INF, -INF, and NAN to integer in C?

后端 未结 1 866
我寻月下人不归
我寻月下人不归 2021-01-17 11:17

Does any standard specifies what should be the output?

For example this code:

#include 
#include 

int main(int argc, ch         


        
相关标签:
1条回答
  • 2021-01-17 11:54

    As Paul said, it's undefined:

    From §6.3.1.4:

    6.3.1.4 Real floating and integer

    When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined.50)

    Infinity isn't finite, and the integral part can't be represented in an integral type, so it's undefined.

    0 讨论(0)
提交回复
热议问题