I have very basic code to read raw data from array and interpret it as data of different type. The assertion in the code fails, from what it looks like incorrect recasting within ternary operator in C. This is platform specific, compiled for embedded processor (Analog Devices Tiger Sharc 201). Any idea what may be happening here? I'm at the point of contacting Analog Devices, but figured there is always a chance someone extremely smart can figure out what may be wrong.
#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;
}