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
  • 16,929
  • 16
  • 85
  • 141
  • Are we talking `int` or `float` ? And can you include a reference to "why it is better" ? – H H 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
  • 36,141
  • 15
  • 83
  • 142
  • 2
    Overflow shouldn't be a problem with floating point types. Loss of precision might be. – H H Oct 31 '10 at 17:15
  • mmm, I've studied that the subtraction operation is worse than addition with near floating points numbers, so why I loose precision with + and not with -? – Ruggero Turra Nov 01 '10 at 17:36
  • How and why? I can't seem to find an explanation for why this works. – Will Oct 01 '15 at 05:03