Guest User

Untitled

a guest
Jul 15th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.41 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. def sqrt_newton(x):
  4. if abs(x) > 1e16: raise ValueError("Too large to be calculated accurately.")
  5. f = lambda a: a ** 2 - x
  6. f1 = lambda a: 2.0 * a
  7. x1 = x / 2.0
  8. dx = 1e-16 * 10.0 ** (len(repr(x)) * 1.0)
  9. while abs(f(x1)) > dx:
  10. x1 = x1 - (f(x1) / f1(x1))
  11. return x1
  12.  
  13. if __name__ == "__main__":
  14. print [(sqrt_newton(10 ** x * 1.0), sqrt_newton(10 ** x * 5.0) ** 2.0) for x in range(1, 16)]
Add Comment
Please, Sign In to add comment