Original Post
Hi i have a code to convert a float into a halffloat, but it has errors when the number is negative or another special cases, i have doun a document with a code to do this convertion but i don´t know how to adapt it to my code. Anyone could adapt that code to my code? here is my code: public float inttohalf(short h) { int s, e, f; int hh; if (h == 0) return 0; s = (int)((h >> 15) & 0x00000001); e = (int)((h >> 10) & 0x0000001f); f = (int)(h & 0x000003ff); if (e == 0) { if (f == 0) { hh = (int)(s << 31); } else { hh = f & 0x00000400; while (hh == 0x00000000) { f <<= 1; e -= 1; e += 1; f &= ~0x00000400; } } } else if (e == 31) { if (f == 0) hh = (int)((s << 31) | 0x7f800000); else hh = (int)((s << 31) | 0x7f800000 | (f << 13)); } e = e + (127 - 15); f = f << 13; hh = (int)((s << 31) | (e << 23) | f); return BitConverter.ToSingle(BitConverter.GetBytes(hh), 0); } } And here the code that i have found: void generatetables(){ unsigned int i; int e; for(i=0; i<256; ++i){ e=i-127; if(e<-24){ // Very small numbers map to zero basetable[i|0x000]=0x0000; basetable[i|0x100]=0x8000; shifttable[i|0x000]=24; shifttable[i|0x100]=24; } else if(e<-14){ // Small numbers map to denorms basetable[i|0x000]=(0x0400>>(18-e)); basetable[i|0x100]=(0x0400>>(18-e)) | 0x8000; shifttable[i|0x000]=-e-1; shifttable[i|0x100]=-e-1; } else if(e<=15){ // Normal numbers just lose precision basetable[i|0x000]=((e+15)<<10); basetable[i|0x100]=((e+15)<<10) | 0x8000; shifttable[i|0x000]=13; shifttable[i|0x100]=13; } else if(e<128){ // Large numbers map to Infinity basetable[i|0x000]=0x7C00; basetable[i|0x100]=0xFC00; shifttable[i|0x000]=24; shifttable[i|0x100]=24; } else{ // Infinity and NaN's stay Infinity and NaN's basetable[i|0x000]=0x7C00; basetable[i|0x100]=0xFC00; shifttable[i|0x000]=13; shifttable[i|0x100]=13; } } }