%%%%%%%%% Cancellation %%%%%%%%%%%%%%%%%%% % try to evaluate this as x-->0 % % f(x)=(1-cos(x))/x^2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % we know that \lim_{x\to 0} (1-cos(x))/x^2 = 1/2 % So, we take a small value of x: x=1.2*10^(-5) % we check that cos(x) is close to 1: cos(x) % therefore, we expect massive cancellation in (1-cos(x)) (1-cos(x))/x^2 % the result is not bad: 0.49999973297490 % this is correct up to abs(1/2-(1-cos(x))/x^2) % approximately 0.000000267025, so the error is about 10^{-6} % % But the thing is that arithmetics is done and numbers % are kept in the double precision: ~10^{-18} There is a huge loss of % accuracy due to cancellation: %%%%%%% % To make the problem really obvious, let us imagine that % we can only compute cos(x) correct up to 10 significant digits, so c=0.9999999999 % with ten correct digits in c, the result is wrong in the first digit: (1-c)/x^2