Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python
- def convertFloatBin(num):
- exponent=0
- shifted_num=num
- while shifted_num != int(shifted_num):
- shifted_num*=2
- exponent+=1
- if exponent==0:
- return '{0:0b}'.format(int(shifted_num))
- binary='{0:0{1}b}'.format(int(shifted_num),exponent+1)
- integer_part=binary[:-exponent]
- fractional_part=binary[-exponent:].rstrip('0')
- return '{0}.{1}'.format(integer_part, fractional_part)
- def floathex_to_binary(floathex):
- num = float.fromhex(floathex)
- return floathex_to_binary(num)
- print(floathex_to_binary('0x1.ccccccccccccdp+2'))
- print(floathex_to_binary('0x1.ccccccccccccdp+1'))
- # this doesn't work
- # I want to take the float 0.9 converted to hex 0x1.ccccccccccccdp-1
- # and convert it to a binary representation. I'll come back later
- # when my maths have improved.
Advertisement
Add Comment
Please, Sign In to add comment