# Rounding in floating point operations

I'm given this set of floating point numbers:

And I'm given the function:

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

So I've done this:

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:

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

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?

• What does "the function: $f(x)=...$" mean here? Are you allowed to rearrange the formula in a way that is mathematically equivalent but better for FP computations? Dec 19, 2019 at 23:17
• @kimchilover As pointed out in the question: "...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...." I know what the alternate form is and I know it's way more precise. But that's not what I'm asking. What I really was looking for has been explained in the accepted answer.
– zcb
Dec 20, 2019 at 14:52

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.
• Yes, it needs to represent them with the same exponent to add. You can represent $1.67\cdot 10^{-4}$ in your floating point system, so you should not round it to zero. You might do something different with it later on. I could have a program that then multiplies it by $6.00\cdot 10^2$ and gets $1.02\cdot 10^{-1},$ for example. When you add it to $1.00 \cdot 10^0$ you get $1.00\cdot 10^0$ again because the extra rounds off to zero. Dec 20, 2019 at 15:26