title: Float to binary
body: <p>I'm trying to convert a floating point number to binary representation; how can I achieve this?
  My goal is, however, not to be limited by 2m so I'm hoping for something that could be easily extended to any base (3, 4, 8) ecc.</p>
  
  <p>I've got a straightforward implementation so far for integers:</p>
  
  <pre><code>import string
  
  LETTER = '0123456789' + string.ascii_lowercase
  def convert_int(num, base):
      if base == 1:
          print "WARNING! ASKING FOR BASE = 1"
          return '1' * num if num != 0 else '0'
  
      if base &gt; 36: raise ValueError('base must be &gt;= 1 and &lt;= 36')
  
      num, rest = divmod(num, base)
      rest = [LETTER[rest]]
      while num &gt;= base:
          num, r = divmod(num, base)
          rest.append(LETTER[r])
      rest.reverse()
      return (LETTER[num] if num else '') + ''.join(str(x) for x in rest)
  </code></pre>
  
  <p>any help appreciated :)</p>
  
  <p>edit:</p>
  
  <pre><code>def convert_float(num, base, digits=None):
      num = float(num)
      if digits is None: digits = 6
  
      num = int(round(num * pow(base, digits)))
      num = convert_int(num, base)
      num = num[:-digits] + '.' + num[:digits]
      if num.startswith('.'): num = '0' + num
      return num
  </code></pre>
  
  <p>is that right? why do i get this behaviour?</p>
  
  <pre><code>&gt;&gt;&gt; convert_float(1289.2893, 16)
  '509.5094a0'
  &gt;&gt;&gt; float.hex(1289.2983)
  '0x1.42531758e2196p+10'
  </code></pre>
  
  <p>p.s.
  <a href="https://stackoverflow.com/questions/3954498/how-to-convert-float-number-to-binary">How to convert float number to Binary?</a></p>
  
  <p>I've read that discussion, but I don't get the answer.. I mean, does it work only for 0.25, 0.125? and I dont understand the phrase 'must be in reverse order'...</p>