Rounding in floating point operations

floating pointmachine precision

I'm given this set of floating point numbers:

enter image description here

And I'm given the function:

enter image description here

I'm then asked to find the value of f for:

enter image description here

enter image description here

So I've done this:

enter image description here

Now my guess is that I need to convert the exact value (1.666…) into a representable value according to the floating point numbers I'm allowed to work with. Rounding it up I get:

enter image description here

Now again I guess I need to round 1.000167 to 1.00 (maybe I should have already rounded 0.000167 to 0.00 ?):

enter image description here

f(x2) is 0 as well. And to be honest I doubt that the exercise required me to calculate 2 values only to find out they're both 0. Also because it then asks me to find the relative error in both cases and explain the differences. It also asks me to find an alternate form to avoid loss of significance but that's not what I'm posting this question for.

What I need to know is: is the function really 0 in both cases? Am I right when rounding the partial operations? Or should I maybe round at the end?

Best Answer

The usual rule is that you should round at all stages of the computation. This is supposed to represent how computers do floating point math but using a granularity of numbers that reduces the labor and makes the problem more obvious. Computers round at every stage because they have to store the value in memory, which they do in the defined floating point format. In that case $1+\frac 1x=1$ for both of your inputs and I agree with you it is strange that you were asked to do the computation twice when both give $0$. If I were writing the problem the first input would be $2$ or something like that where the computation comes out reasonably well. The second input would be something large like you got, showing that you get $0$.

You should not have already rounded $0.000166667$ to $0.00$ because you can represent it as $1.67\cdot 10^{-4}$. That is how floating point works. It only disappears when you add it to $1$.

As a nit, I would show all the numbers with two decimal places, so your $6\cdot 10^3$s should be $6.00\cdot 10^3$s. I think that is in the spirit of the problem.

Related Question