title: Golang floating point precision float32 vs float64
body: <p>I wrote a program to demonstrate floating point error in Go: </p>
  
  <pre><code>func main() {
      a := float64(0.2) 
      a += 0.1
      a -= 0.3
      var i int
      for i = 0; a &lt; 1.0; i++ {
          a += a
      }
      fmt.Printf("After %d iterations, a = %e\n", i, a)
  }
  </code></pre>
  
  <p>It prints:</p>
  
  <pre><code>After 54 iterations, a = 1.000000e+00
  </code></pre>
  
  <p>This matches the behaviour of the same program written in C (using the <code>double</code> type) </p>
  
  <p>However, if <code>float32</code> is used instead, the program gets stuck in an infinite loop! If you modify the C program to use a <code>float</code> instead of a <code>double</code>, it prints</p>
  
  <pre><code>After 27 iterations, a = 1.600000e+00
  </code></pre>
  
  <p>Why doesn't the Go program have the same output as the C program when using <code>float32</code>?</p>