5

Why in the bisection method it is better to compute the midpoint c between a and b with

c = a + (b - a) / 2.

instead of the simpler:

c = (a + b) / 2.

all variables are floating points.

Ruggero Turra
  • 14,523
  • 14
  • 72
  • 123
  • Are we talking `int` or `float` ? And can you include a reference to "why it is better" ? – Henk Holterman Oct 31 '10 at 17:19
  • 2
    Why do you believe the first method is better? It involves one extra floating-point operation compared to the second method; it risks overflow in the rare case where `a` and `b` are huge with opposite signs (whereas the second method risks overflow in the almost equally rare case where `a` and `b` are huge with the same sign); ignoring overflow, the second method will always compute a correctly-rounded midpoint on a typical machine. So does the first, at least when `a` and `b` are close (and possibly in general; I'm not sure). So I don't see any clean win for the first method. – Mark Dickinson Nov 01 '10 at 19:48

1 Answers1

6

it is to avoid any potential overflows / loss of precision in intermediate calculations.

tenfour
  • 33,679
  • 12
  • 73
  • 135