Cancellation Consider the following code import numpy def sqrt_single(x): X = x.astype( f ) # or use: X = numpy.cast[ f ](x) return numpy.sqrt(X) v = numpy.array([1.0e-5, 1.0e3, 1.0e3], f ) print SINGLE precision type: , v.dtype a = v[0]; b = v[1]; c = v[2] sq = sqrt_single(b*b-4.0*a*c) xa1 = (-b + sq)/(2.0*a) xa2 = (-b - sq)/(2.0*a) print \nx1,x2: , xa1,xa2 print We expect: x1*x2-c/a=0, but result is: , xa1*xa2-c/a print x1 from c/(a*x2)= , c/(a*xa2) xb1 = (-2.0*c) / (b + sq) xb2 = (-2.0*c) / (b - sq) print \nx1,x2: , xb1,xb2 print We expect x1*x2-c/a=0, but result is: , xb1*xb2-c/a print x2 from c/(a*x1) , c/(a*xb1) print \nsqrt should be 999.99998, but result is: %12f % sq9
{}