Advertisement
Programmin-in-Python

Program to Compute the Binary representation of FLOATING-POINT numbers

Jan 26th, 2021
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.47 KB | None | 0 0
  1. def float_bin(num):
  2.     str_num = str(num)
  3.     index_deci = str_num.find('.')
  4.     float_num = float(str_num[(index_deci):])
  5.  
  6.     res_int_num = bin(int(num))
  7.     res_float_num, i = ".", 0
  8.  
  9.     while bool(float_num):
  10.         if i < 8:
  11.             res = float_num*2
  12.             res_float_num += str(int(res))
  13.  
  14.             str_res = str(res)
  15.             index_deci = str_res.find('.')
  16.             float_num = float(str_res[(index_deci):])
  17.  
  18.             i += 1
  19.         else:
  20.             break
  21.  
  22.     return res_int_num+res_float_num
  23.  
  24. var = float_bin(2.77)
  25. print(var)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement