Suppose little Gauss lived in the modern age. Little Gauss’ teacher wanted to surf the internet, so he assigned all of his students the following integral to evaluate:
Being the clever alter-ego of the boy who immediately saw
and with a little bit of clever manipulation (integration by parts), he found that, using
and
Why, this is a simple recursive function! Little Gauss was absolutely thrilled, he has at his disposal a programmable calculator capable of python (because he’s Gauss, he can have whatever the fuck he wants), and he quickly coded up the recurrence:
import math
def s(k):
if k == 0:
return 1 - math.exp(-1)
else:
return 1 - k*s(k-1)He verified the first few cases by hand, and upon finding his code satisfactory, he computed the 100th element of the sequence as -1.1599285429663592e+141 and turned in the first few digits.
His teacher glanced at his solution, and knowing that there’s no way little Gauss could have done his school work with such proficiency, immediately declared it wrong. Upon repeated appeal, the teach finally relented and looked up the solution in his solution manual and, bewildered… again told little Gauss that he was WAAAAY off. Unfortunately for our tragic hero, this would not be a modern day retelling of a clever little boy who outsmarted his teacher. No, this is a tragic story of a clever little boy who succumbed to a fatal case of the roundoff bugs.
Suppose
or
and in general
of course
So what went wrong? Well, whenever you’re in trouble, just make a plot.
Source Source
Everything seems to be going fine until around
It turns out that there was nothing wrong with little Gauss’ method and the integral is perfectly well-behaved. The culprit lies in the fact that
The tiny perturbation in the computed value of
Little Gauss definitely should have paid attention to the lessons on round-off errors.
Problem: How would you turn this into a stable algorithm?
