Computing midpoint of an interval overflow

1.1k Views Asked by At

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.

2

There are 2 best solutions below

2
On

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}$.

0
On

Both cases have merrit and faults.

Just for demonstration purposes, suppose we have a computer that uses base $10$ arithmetic and can handle no more than $4$ digits plus an extra digit for powers of $10$.

So the number $x : abcd(e)$ would represent $ x = abcd \times 10^e$ We will write this as

$$x : abcd(e) \leftrightarrow abcd \times 10^e$$

Suppose
$m : 5555(0) \leftrightarrow 5555\times 10^0$
$n : 5561(0) \leftrightarrow 5561\times 10^0$

Then $(m+n)/2 = 5558$

(m+n)/2 get computed as \begin{align} m+n &: 1112(1) \leftrightarrow 1112\times 10^1 &\text{# Note}\; m+n = 11116\\ (m+n)/2 &: 5556(0) \leftrightarrow 5556\times 10^0 &\text{# Note}\; 11120/2 = 5556\\ \end{align}

So we get 5556 instead of 5558.

m + (n-m)/2 gets computed as \begin{align} n-m &: 0006(0) \leftrightarrow 6\times 10^0 \\ (n-m)/2 &: 0003(0) \leftrightarrow 3 \times 10^0 \\ m + (n-m)/2 &: 5558(0) \leftrightarrow 5558 \times 10^0 \end{align}

and we get the correct answer.

Now suppose
$m : 4444(4) \leftrightarrow 4444\times 10^4$
$n : 2222(0) \leftrightarrow 2222\times 10^0$

Then $(m+n)/2 = 22221111$, which the computer is only capable of storing as $(m+n)/2 : 2222(4) \leftrightarrow 2222 \times 10^4$

(m+n)/2 get computed as \begin{align} m+n &: 4444(4) \leftrightarrow 4444\times 10^4 &\text{# This is}\; (4444\times 10^4) + (0\times 10^4)\\ (m+n)/2 &: 2222(4) \leftrightarrow 2222\times 10^4 \end{align}

And we get 22220000 instead of 22221111.

m + (n-m)/2 gets computed as \begin{align} n-m &: 2222(4) \leftrightarrow 2222\times 10^4 &\text{# This is}\; (4444\times 10^4) - (0\times 10^4)\\\\ (n-m)/2 &: 2222(4) \leftrightarrow 2222 \times 10^4 \\ m + (n-m)/2 &: 2222(4) \leftrightarrow 2222 \times 10^4 \end{align}

And we get 22220000 instead of 22221111.