title: Decimal place issues with floats and decimal.Decimal
body: <p>I seem to be losing a lot of precision with floats.</p>
  
  <p>For example I need to solve a matrix:</p>
  
  <pre><code>4.0x -2.0y 1.0z =11.0
  1.0x +5.0y -3.0z =-6.0
  2.0x +2.0y +5.0z =7.0
  </code></pre>
  
  <p>This is the code I use to import the matrix from a text file:</p>
  
  <pre><code>f = open('gauss.dat')
  lines =  f.readlines()
  f.close()
  
  j=0
  for line in lines:
      bits = string.split(line, ',')
      s=[]
      for i in range(len(bits)):
          if (i!= len(bits)-1):
              s.append(float(bits[i]))
              #print s[i]
      b.append(s)
      y.append(float(bits[len(bits)-1]))
  </code></pre>
  
  <p>I need to solve using gauss-seidel so I need to rearrange the equations for x, y, and z:</p>
  
  <pre><code>x=(11+2y-1z)/4
  y=(-6-x+3z)/5
  z=(7-2x-2y)/7
  </code></pre>
  
  <p>Here is the code I use to rearrange the equations. <code>b</code> is a matrix of coefficients and <code>y</code> is the answer vector:</p>
  
  <pre><code>def equations(b,y):
      i=0
      eqn=[]
      row=[]
      while(i&lt;len(b)):
          j=0
          row=[]
          while(j&lt;len(b)):
              if(i==j):
                  row.append(y[i]/b[i][i])
              else:
                  row.append(-b[i][j]/b[i][i])
              j=j+1
          eqn.append(row)
          i=i+1
      return eqn
  </code></pre>
  
  <p>However the answers I get back aren't precise to the decimal place.</p>
  
  <p>For example, upon rearranging the second equation from above, I should get:</p>
  
  <pre><code>y=-1.2-.2x+.6z
  </code></pre>
  
  <p>What I get is:</p>
  
  <pre><code>y=-1.2-0.20000000000000001x+0.59999999999999998z
  </code></pre>
  
  <p>This might not seem like a big issue but when you raise the number to a very high power the error is quite large. Is there a way around this? I tried the <code>Decimal</code> class but it does not work well with powers (i.e, <code>Decimal(x)**2</code>).</p>
  
  <p>Any ideas?</p>