NVIDIA Interview Question
Software Engineer / Developersint main()
{
struct abc { int x; char b; float h;};
float k;
unsigned int n,j,i,d=0;
printf("enter float value k=");
scanf("%f",&k);
n=k*1000000; // to avoid float value correction
printf("k=%d\n",n);
i = n%1000000;
n = n/1000000;// to get the decimal value
if(i)
{
while( i%10 == 0)
i=i/10;
}
printf("%d %d\n",n,i);
n = n<<16 | i;
printf("Hex Value = %X\n",n);
return 0;
}
should be correct up to rounding:
- asm December 02, 2008unsigned float2fixed(float x) {
unsigned res = (unsigned)floorf(x) << 16;
res |= ((unsigned)floorf(x * 65536.0f) & 0xffff);
}
float fixed2float(unsigned x) {
float res = (float)(x >> 16);
res += (float)(x & 0xffff) / 65536.0f;
}