Advertisement
furas

formatting float value

Mar 6th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. # question: http://pastebin.com/QwrjQn9n
  2.  
  3. import numpy as np
  4.  
  5. def exponential_approx(x, acc, digits=15):
  6.     approx = 0
  7.     real = np.math.exp(x)
  8.     diff = 1
  9.     ind = 0
  10.    
  11.     while diff > acc:
  12.         term = (x**ind)/(np.math.factorial(ind))
  13.         approx += term
  14.         diff = (real - approx)
  15.         ind += 1
  16.        
  17.     #return '%.15f' % approx
  18.     return '%.*f' % (digits, approx)
  19.  
  20. print(exponential_approx(0.1, 1e-3))      # default 15 digits
  21. print(exponential_approx(0.1, 1e-3, 3))
  22. print(exponential_approx(0.1, 1e-3, 10))
  23. print(exponential_approx(0.1, 1e-3, 15))
  24. print(exponential_approx(0.1, 1e-3, 20))
  25.  
  26. '''
  27. 1.105000000000000
  28. 1.105
  29. 1.1050000000
  30. 1.105000000000000
  31. 1.10499999999999998224
  32. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement