Catastrophic Cancellation
Rob Mahurin
rob at utk.edu
Tue Jul 1 17:54:39 CDT 2008
That's a great example! I love the distinction between the one-bit
and the continuous errors.
In Octave, you don't need the loop, though:
octave-3.0.1> x = 1e-7*linspace(-1,1,1e3);
octave-3.0.1> y = [ (1-cos(x))./x.^2;
1./x.^2 - cos(x)./x.^2;
(1 - x.^2/12)/2;
];
octave-3.0.1> plot(x,y,'o-');
Cheers,
Rob
On Jul 1, 2008, at 5:50 PM, A. Kalten wrote:
> Just to spread some awareness of the pitfalls of floating point
> calculations, I urge everyone to try this simple test. At the
> octave prompt enter:
>
> x=-4e-8:1e-10:4e-8;
> for i=1:length(x)
> y(i)=(1-cos(x(i)))/x(i)/x(i);
> endfor
> plot(x,y)
>
> Hopefully, the results should surprise, or even shock, you.
>
> Also try rewriting the above function as: y(i)=1/x(i)/x(i)-cos(x
> (i))/x(i)/x(i)
>
> For more info, the link is here:
>
> http://www.cs.princeton.edu/introcs/lectures/9scientific.pdf
>
> AK
>
> _______________________________________________
> Help-octave mailing list
> Help-octave at octave.org
> https://www.cae.wisc.edu/mailman/listinfo/help-octave
--
Rob Mahurin
Dept. of Physics & Astronomy
University of Tennessee phone: 865 207 2594
Knoxville, TN 37996 email: rob at utk.edu
More information about the Help-octave
mailing list