trap representation

后端 未结 3 1439
情话喂你
情话喂你 2020-11-22 11:56
  1. What is a \"trap representation\" in C (some examples might help)? Does this apply to C++?

  2. Given this code...

    float f=3.5;
    int *pi          
    
    
            
3条回答
  •  不要未来只要你来
    2020-11-22 12:51

    In general, any non-trap IEEE-754 floating point value can be represented as an integer on some platforms without any issue. However, there are floating point values that can result in unexpected behavior if you assume that all floating point values have a unique integer representation and you happen to force the FPU to load that value.

    (Example taken from http://www.dmh2000.com/cpp/dswap.shtml)

    For instance, when working with FP data you need to marshal between CPUs with different endianness, you might think of doing the following:

    double swap(double)

    Unfortunately, if the compiler loads the input into an FPU register and it's a trap representation, the FPU can write it back out with an equivalent trap representation that happens to be a different bit representation.

    In other words, there are some FP values that do not have a corresponding bit representation if you don't convert correctly (by correct I mean through a union, memcpy via char * or other standard mechanism).

提交回复
热议问题