How can I manually set the bit value of a float that equates to NaN?

后端 未结 3 1779
一整个雨季
一整个雨季 2021-01-13 16:48

I\'m trying to run some tests with conversions and castings of floats to other types and I want to set my float variable to different values of nan.

\"a bit-wise exa

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-13 17:41

    Use memcpy:

    int32_t i = 0x7FC00000;
    float f;
    memcpy(&f, &i, sizeof(f));
    

    You could also assert that sizeof(f) == sizeof(i), but if you know that floats are IEEE then presumably you also know what size the basic types are.

提交回复
热议问题