# Magnitude of floating point rounding errors in operations

I'm doing the following exercise:

Consider $$f(x)= \begin{cases} \displaystyle\frac{1-(\cos(x))^3}{x^2}, \quad if\ x\neq 0\\ \displaystyle\frac{3}{2}, \quad if\ x= 0\\ \end{cases}$$

Calculate $f(x_0)$, with $x_0=0.000011$, with a C program (simple and double precision) and with a pocket calculator. Identify the cause of the error. Justify the differences and the magnitude of the errors.

I know that there's cancellation on the numerator and the rounding errors on every operation ($\cos(x)$, $(\cos(x))^3$, $1-(\cos(x))^3$ and dividing that quantity by $x^2$). The calculator gives me $0$, and the correct answer is very near to $3/2$. This is because a pocket calculator can store only 9 significant digits, and $1-(\cos(x_0))^3$ is $0$ for a calculator. But how can I measure the magnitude of the errors when there're not rounding errors on the input? The quantity given by $x_0$ is exact and the computer write this quantity correctly in simple and double precision, so there's only rounding errors inside every operation. Can I measure the magnitude by using the propagation of errors' formula? This formula says: $$|\Delta f(x)|=|f'(x)|\cdot|\Delta x|.$$

Thanks!

• The correct answer is not close to $3/2$. Check your function definition. – Yves Daoust Mar 25 '16 at 11:40
• @YvesDaoust The exact value is like 1.499999999894125000... – Relure Mar 25 '16 at 11:44
• Oooops, sorry, I was cubing the whole numerator. – Yves Daoust Mar 25 '16 at 11:54
• (1) The C program will not represent $0.000011$ exactly, since it's not an integer divided by an integer power of $2$. But that's not the main cause of error, as you know. (2) Don't assume everyone knows which propagation of errors formula you mean. What's the formula? – David K Mar 25 '16 at 12:04

The error of the denominator can be estimated using relative error formulas for floating point evaluations as $$(1-(\cos(x)·(1+δ_1))^3·(1+δ_2))·(1+δ_3) \\ = (1-\cos(x)^3)·(1+δ_3)-\cos(x)^3·(3δ_1+δ_2)+O(δ^2)$$ where $|δ_k|<\mu=2^{-52}$ (the last for double). Thus the relative error of the denominator computation is $$δ_4=δ_3+\frac{\cos(x)^3}{1-\cos(x)^3}·(3δ_1+δ_2)$$ which for $x=1.1·10^{-5}$ is bounded by $|δ_4|<5.51·10^9·\mu$. For the full quotient this relative error transfers and using a more exact evaluation formula, this error is confirmed as

In [1]: raw = (1-cos(x)**3)/x**2
In [2]: raw
Out[2]: 1.5000012251581842
In [3]: refined = 2*(sin(x/2)/x)**2*(1+cos(x)+cos(x)**2)
In [4]: refined
Out[4]: 1.4999999998941247
In [5]: (raw-refined)/refined
Out[5]: 8.168427064213852e-07
In [6]: (raw-refined)/refined*2**52
Out[6]: 3678732508.259658


which gives the relative error as $3.68·10^9⋅μ$.

• Thanks for the answer! Where does the error of applying $\frac{1}{x^2}$ appear in your solution? Thanks! – Relure Mar 25 '16 at 15:43
• Nowhere. Like the error $δ_3$ of the subtraction, the squaring and division will add an additional $2$ or $3$ to the factor $5.51·10^9$, so I left out this trivial amount. For $x\gg 0$ these would play a greater role. – LutzL Mar 25 '16 at 15:48
• I've been trying to figure it out by myself, but I can't see why you turn this $(1-(\cos(x)·(1+δ_1))^3·(1+δ_2))·(1+δ_3) \\ = (1-\cos(x)^3)·(1+δ_3)-\cos(x)^3·(3δ_1+δ_2)$ into this $δ_4=δ_3+\frac{\cos(x)^3}{1-\cos(x)^3}·(3δ_1+δ_2)$. Where does the $δ_4$ come from? – Relure Mar 25 '16 at 17:47
• $δ_4=$(approxvalue-exactvalue)/exactvalue for the computation of $1-\cos(x)^3$, omitting the higher-order error terms. Which means it is the first expression minus $1-\cos(x)^3$ divided by $1-\cos(x)^3$. – LutzL Mar 25 '16 at 18:17