# Function to convert IEEE-754 to decimal

Here’s what I have so far:

``````def f_func(f_binary):
f_value = 0
f_binary_place = 1
f_binary = list(f_binary)
for i in f_binary:
if int(i) == 0:
f_binary_place = f_binary_place + 1
elif int(i) == 1:
f_value = f_value + (2**(-(f_binary_place)))
f_binary_place = f_binary_place + 1
return f_value

def mySingle2Decimal(binary):
f_binary = binary[10:]
s = int(binary[0])
d = 127
e = myBinary2Num(binary[1:9], 'UnSign')
f = f_func(f_binary)
if (e != 0) & (e != 255):
return (((-1)**s)*(2**(e-d))*(1+f))
elif (e == 0) & (f != 0):
return (((-1)**s)*(2**(1-d))*(f))
elif (e == 0) & (f == 0):
return 0
elif (e == 255) & (f == 0):
return ((-1)**s)*(np.inf)
elif (e == 255) & (f != 0):
return np.nan
``````

It seems to be working for the last two cases, which makes me assume my function for the decimal is wrong, but it only seems to be wrong after about 6 decimal places. Could anyone help me out, it would be greatly appreciated!

Hello @owenmosk, welcome!

Can you explain a little deeper what are you trying to do? How are you using those two functions? Examples?

What do you mean with the “last two cases”? Also, note that your code is calling `myBinary2Num` and that is not included in the post.

1 Like