-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)
step
  • 51
  • 1
  • 1
  • 10
  • Could you clarify the formats (and Python types) of the inputs and outputs? Some examples might help, too. – Mark Dickinson Jul 08 '19 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. – Willem Van Onsem Jul 08 '19 at 19:59
  • what about numpy.float32( ... )? – Stef Jul 08 '19 at 20:04
  • @MarkDickinson i had updated the question,thanks. – step Jul 08 '19 at 20:06
  • @WillemVanOnsem i had create a example link that with C language,but i am looking for python,thanks. – step Jul 08 '19 at 20:07
  • @Stef sry, i can't understand what your mean,can explain in details? thanks. – step Jul 08 '19 at 20:09
  • You have example code for 64 bit conversion, and you should be able to find the IEE754 specification so unless you want someone to write the code for you (which is off-topic for stackoverflow, because Stackoverflow is not a code writing service) you have all the information needed to write the code yourself. – DisappointedByUnaccountableMod Jul 08 '19 at 20:28
  • from https://stackoverflow.com/questions/16444726/binary-repreentation-of-float-in-python-bits-not-hex: `''.join('{:0>8b}'.format(c) for c in struct.pack('!f', num))` – Stef Jul 08 '19 at 21:26

1 Answers1

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).

Stef
  • 28,728
  • 2
  • 24
  • 52
  • 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! :) – step Jul 12 '19 at 08:26