コード例 #1
0
def test_newton4sqrt2():
   """
   Applies Newton's method to approximate the
   square root of two with double double arithmetic.
   """
   x = [2.0, 0.0]
   print 'step 0 :', doubleDouble.str(x[0],x[1])
   for i in range(1,9):
      z = doubleDouble.mul(x[0],x[1],x[0],x[1])
      z = doubleDouble.add(z[0],z[1],2.0,0.0)
      z = doubleDouble.div(z[0],z[1],x[0],x[1])
      z = doubleDouble.mul(z[0],z[1],0.5,0.0)
      x[0] = z[0]
      x[1] = z[1]
      print 'step', i, ':', doubleDouble.str(x[0],x[1])
コード例 #2
0
def test_arithmetic():
   """
   Basic test on arithmetical operations.
   """
   hi = 1.4142135623730951
   lo = -9.6672933134529147e-17
   x = (hi,lo)     # defines the sqrt(2)
   print 'x = sqrt(2) = ', doubleDouble.str(x[0],x[1])
   y = (1.0,0.0)
   z = doubleDouble.add(x[0],x[1],y[0],y[1])
   print 'x + 1 = ', doubleDouble.str(z[0],z[1])
   xx = doubleDouble.sub(z[0],z[1],y[0],y[1])
   print 'x + 1 - 1 = ', doubleDouble.str(x[0],x[1])
   x2 = doubleDouble.mul(x[0],x[1],x[0],x[1])
   print 'x*x = ', doubleDouble.str(x2[0],x2[1])
   x2dx = doubleDouble.div(x2[0],x2[1],x[0],x[1])
   print 'x*x/x = ', doubleDouble.str(x2dx[0],x2dx[1])
コード例 #3
0
 def __div__(self,other):
    """
    Returns the division of self by the other.
    """
    z = doubleDouble.div(self.high,self.low,other.high,other.low)
    return DoubleDouble(z[0],z[1])