-2

A converting between the decimal to single-precision floating,but i intro to this,so did any useful references or any summarize can help?

Thousand Thanks!

so,the python function will be like

  1. require a user to select convert from decimal to to floating point number or floating point number convert to decimal.

  2. input value

  3. will show the step such as

    i. Sign

    ii. Exponent

    iii. Mantissa

  4. return the result.


below is the example i searched from internet but this is 64bit so,how change it to 32bit and show the step when converting?

    import struct

getBin = lambda x: x > 0 and str(bin(x))[2:] or "-" + str(bin(x))[3:]

def floatToBinary64(value):
    val = struct.unpack('Q', struct.pack('d', value))[0]
    return getBin(val)

def binaryToFloat(value):
    hx = hex(int(value, 2))   
    return struct.unpack("d", struct.pack("q", int(hx, 16)))[0]

# floats are represented by IEEE 754 floating-point format which are 
# 64 bits long (not 32 bits)

# float to binary
binstr = floatToBinary64(19.5)
print('Binary equivalent of 19.5:')
print(binstr + '\n')

# binary to float
fl = binaryToFloat(binstr)
print('Decimal equivalent of ' + binstr)
print(fl)
8
  • Could you clarify the formats (and Python types) of the inputs and outputs? Some examples might help, too. Commented Jul 8, 2019 at 19:57
  • It makes not much sense to convert a float to a decimal. Since due to rounding errors, the values will not be very accurate. Commented Jul 8, 2019 at 19:59
  • what about numpy.float32( ... )? Commented Jul 8, 2019 at 20:04
  • @MarkDickinson i had updated the question,thanks. Commented Jul 8, 2019 at 20:06
  • @WillemVanOnsem i had create a example link that with C language,but i am looking for python,thanks. Commented Jul 8, 2019 at 20:07

1 Answer 1

0

Using the answers to this SO question we get:

import struct

def floatToBinary32(value):
    return ''.join(f'{c:0>8b}' for c in struct.pack('!f', value))

def binaryToFloat(value):
    hx = hex(int(value, 2))   
    return struct.unpack("f", struct.pack("l", int(hx, 16)))[0]

# float to binary
fl0 = 19.5
binstr = floatToBinary32(fl0)
print(f'Binary equivalent of {fl0}: {binstr}')

# binary to float
fl1 = binaryToFloat(binstr)
print(f'Decimal equivalent of      {binstr}: {fl1}')

print(f'\nSign     ( 1 bit ) = {binstr[0]}\nExponent ( 8 bits) = {binstr[1:9]}\nMantissa (23 bits) = {binstr[9:]}')

assert fl0 == fl1

Output:

Binary equivalent of 19.5: 01000001100111000000000000000000
Decimal equivalent of      01000001100111000000000000000000: 19.5

Sign     ( 1 bit ) = 0
Exponent ( 8 bits) = 10000011
Mantissa (23 bits) = 00111000000000000000000

Please note however that, as already pointed out in the comments above, such conversions are not exact as you can see when using for example fl0 = 0.1. This also shows that python (CPython) uses 64 bit floats (implemented using double in C).

Sign up to request clarification or add additional context in comments.

1 Comment

hi,thanks for helping me about this question. i check on this have some problem with return struct.unpack("f", struct.pack("l", int(hx, 16)))[0],but i had modified, and it worked now. A thousand thanks again! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.