Skip to content

Instantly share code, notes, and snippets.

@koverholt
Created September 23, 2011 20:06
Show Gist options
  • Save koverholt/1238338 to your computer and use it in GitHub Desktop.
Save koverholt/1238338 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from __future__ import division
import math
def myexp(x):
''' myexp(x) will compute e^x for any x by measuring the difference in the last 2 terms of the series. '''
tot = (1 + x)
n = 1
while (tot != tot + x):
n = n + 1
x = x * (x / n)
tot = tot + x
return tot
x = float(raw_input("Evaluate e at x = "))
e = myexp(x)
z = math.exp(x)
diff = z - e
print "Ans: %0.4f" % e
print "Difference: %0.4f" % diff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment