0

I am reading the book Numerical Recipes in C in the chapter about the bisection algorithm for root finding but in the code I'm confounded by the following line of code:

rtb = f < 0.0 ? (dx=x2-x1,x1) : (dx=x1-x2,x2);

I know that the operator ( code1 ) ? ( code2 ) : ( code3 )

is like a if( code1 ) ( code2 ) else ( code3 )

but what i don't understand is that rtb that is a float is receiving either (dx=x2-x1,x1)

or (dx=x1-x2,x2), being x1 and x2 floats would't this be assigning some type of ordered pair of floats to a single float ?

Felipe Dilho
  • 167
  • 8
  • Take a look at comma operator. rtb gets x1 or x2, not a pair. – randomir Sep 08 '18 at 17:17
  • TL;DR: It's an operator, like `+`, `/` and so on. `X, Y` is equal to `Y` (`X` is evaluated and discarded). Here it's used to make the code more terse (and slightly ugly). It essentially means `if (f < 0.0) {dx = x2-x1; rtb = x1;} else {dx = x1-x2; rtb = x2}`. – HolyBlackCat Sep 08 '18 at 17:18

0 Answers0