Is the behaviour of casting a negative double to unsigned int defined in the C standard? Different behaviour on ARM vs. x86

后端 未结 1 1702
梦毁少年i
梦毁少年i 2020-11-27 08:38

I have code that runs on different platforms that seems to get different results. I am looking for a proper explanation.

I expected casting to unsigned

相关标签:
1条回答
  • 2020-11-27 08:49

    No


    This conversion is undefined and therefore not portable.

    C99/C11 6.3.1.4

    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.

    According to C11 6.3.1.4 footnote 61:

    The remaindering operation performed when a value of integer type is converted to unsigned type need not be performed when a value of real floating type is converted to unsigned type. Thus, the range of portable real floating values is (−1, Utype_MAX+1).

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