Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # question: http://pastebin.com/QwrjQn9n
- import numpy as np
- def exponential_approx(x, acc, digits=15):
- approx = 0
- real = np.math.exp(x)
- diff = 1
- ind = 0
- while diff > acc:
- term = (x**ind)/(np.math.factorial(ind))
- approx += term
- diff = (real - approx)
- ind += 1
- #return '%.15f' % approx
- return '%.*f' % (digits, approx)
- print(exponential_approx(0.1, 1e-3)) # default 15 digits
- print(exponential_approx(0.1, 1e-3, 3))
- print(exponential_approx(0.1, 1e-3, 10))
- print(exponential_approx(0.1, 1e-3, 15))
- print(exponential_approx(0.1, 1e-3, 20))
- '''
- 1.105000000000000
- 1.105
- 1.1050000000
- 1.105000000000000
- 1.10499999999999998224
- '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement