lol don't take any offence to the title XD
just being funny :P
anyways...
I'm having a problem with my function...
it's supposed to allow you to read/write float values in any specified byte length.
but there's a problem with the output value >_>
first off though I must state that the equation for processing the exponent field works perfectly for the pre-defined float types:
[ S, E, M ]
[ 1, 3, 4 ] - 8bit
[ 1, 5, 10 ] - 16bit
[ 1, 8, 23 ] - 32bit
[ 1, 11, 52 ] - 64bit
going along with these standards, IDK if the odd types such as 24bit or 48bit are correct.
anyways...
on with the code :P
def f(byte_cnt,val):
e=int(((byte_cnt*8)-1)/(byte_cnt+1)+(byte_cnt/2 if byte_cnt>2 else 0))
S,E,M=(val>>((byte_cnt*8)-1))&1,(val>>((byte_cnt*8)-(e+1)))&int('1'*e,2),val&int('1'*((byte_cnt*8)-(e+1)),2)
print str(byte_cnt*8)+'bits [[1],['+str(e)+'],['+str(((byte_cnt*8)-1)-e)+']]: '+\
str(S)+' '+('0'*(e-len(bin(E).replace('0b',''))))+bin(E).replace('0b','')+' '+\
('0'*(((byte_cnt*8)-(e+1))-len(bin(M).replace('0b',''))))+bin(M).replace('0b','')
return (pow(-1,S)*(pow(2.0,(E-int('1'*(e-1),2)))*float(('1.' if E!=0 else '0.')+str(M))) if E<int('1'*e,2) else 'NAN') #problem on this line
usage:
f( byte_length, int(float_hex,16) )
IDK what I'm doing wrong, as I've followed the notes of about 8 sites D:
the returned binary values are correct, but the float is not...
anyone mind lending a hand?? :/
many thanx in return :)