2

配列から生データを読み取り、それを異なるタイプのデータとして解釈するための非常に基本的なコードがあります。C の三項演算子内で誤った再キャストが行われているように見えるため、コード内のアサーションが失敗します。これはプラットフォーム固有であり、組み込みプロセッサ (Analog Devices Tiger Sharc 201) 用にコンパイルされています。ここで何が起こっているのでしょうか?私は現在、Analog Devices に連絡を取りつつありますが、非常に頭の良い人が何が問題なのかを突き止める可能性は常にあると考えていました。

#include <assert.h>

typedef union {
    unsigned int uint32;
    float float32;
} c_t;

int main( int argc, char *argv[] )
{   
    unsigned int r;

    int data_type;

    //data is a raw array, could be floats or unsigned int
    unsigned int data[] = {470698344};    

    //cast raw data as mixed union type
    c_t mixed = *(c_t*) data;       

    //interpret all data as unsigned integer
    data_type = 1;

    //this is where cast to float takes place, resulting in loss of precision
    r = data_type ? mixed.uint32 : mixed.float32;

    //also fails, with no union, results in same code
    //r = data_type ? *((unsigned int *)data) : *((float *) data);

    //r = 470698336, loss of precision 
    //due to incorrect cast inside ternary conditional statement at line 23?
    assert(r == 470698344);             

    return 0;
}
4

2 に答える 2