For computing the midpoint m of an interval $[a, b]$, which of the following two formulas is preferable in floating-point arithmetic? Why? When? (Hint: Devise examples for which the "midpoint" given by the formula lies outside the interval $[b,b]$.)
$$(a)\;\;\; m = (a+b)/2.0$$ $$(b)\;\;\; m = a + (b-a)/2.0$$
So I have attempted the followings:
I think $(b)$ is better because $b$ guarantees the result to lie within the interval however, I cannot come up with examples to prove that a will result in overflow and result being outside of the interval.
On computer, when both $a$ and $b$ near the maximum of the flow number for the given bits of the flow number, $a + b$ will overflow the float number within the given bits, but $b-a$ fairs better in this case. You can certainly come up a case in which $-a$ and $b$ are near the maximum of the float number within given bits, and that $a + b$ will not overflow but $b - a$ will overflow the float number. The correct code will check the sign of $a$ and $b$, put the proper code to use. For the same sign, using $a + \frac{(b -a)}{2.0}$, for the opposite sign, using $\frac{(a + b)}{2.0}$.