title: How floats are stored in python
body: <p>I have been using python for my assignments for past few days. I noticed one strange thing that - </p>
  
  <ol>
  <li>When I convert string to float - it gives exactly same number of digits that were in string.</li>
  <li><p>When I put this number in file using struct.pack() with 4 bytes floats and read it back using struct.unpack(), it gives a number not exactly same but some longer string which I expect if as per the floating point storage</p>
  
  <p>Ex. -  String - 0.931973 </p>
  
  <p>Float num - 0.931973 </p>
  
  <p>from file - 0.931972980499 (after struct pack and unpack into 4 bytes)</p></li>
  </ol>
  
  <p>So I am unable to understand how python actually stored my number previously when I read it from string.</p>
  
  <p><strong>EDIT</strong>
  Writing the float (I think in python 2.7 on ubuntu its other way around, d- double and f-float)</p>
  
  <pre><code>buf = struct.pack("f", float(self.dataArray[i]))
  fout.write(buf)
  </code></pre>
  
  <p>Query - </p>
  
  <pre><code>buf = struct.pack("f", dataPoint)
  dataPoint = struct.unpack("f", buf)[0] 
  node = root
  while(node.isBPlusNodeLeaf()) == False:
      node = node.findNextNode(dataPoint)
  </code></pre>
  
  <p>findNextNode - </p>
  
  <pre><code>def findNextNode(self, num):
      i = 0
      for d in self.dataArray:
          if float(num) &gt; float(d):
              i = i + 1
              continue
          else:                
              break
      ptr = self.pointerArray[i]
      #open the node before passing on the pointer to it
      out, tptr = self.isNodeAlive(ptr)
      if out == False:
          node = BPlusNode(name = ptr)
          node.readBPlusNode(ptr)
          return node
      else:            
          return BPlusNode.allNodes[tptr]
  </code></pre>
  
  <p>once I reach to leaf it reads the leaf and check if the datapoint exist there. </p>
  
  <pre><code>for data in node.dataArray:
          if data == dataPoint:
              return True
      return False    
  </code></pre>
  
  <p>So in this case it returns unsuccessful search for datapoint - 0.931972980499 which is there although.</p>
  
  <p>While following code works fine - </p>
  
  <pre><code>for data in node.dataArray:
          if round(float(data), 6) == dataPoint:
              return True
      return False    
  </code></pre>
  
  <p>I am not able to understand why this is happening</p>