title: trying to divide complex numbers, division by zero
body: <p>I'm trying the program below to divide complex numbers, it works for complex numbers but not when the denominator is real (i.e, the complex part is zero). Division by zero occurs in this line <code>ratio = b-&gt;r / b-&gt;i ;</code>, when the complex part <code>b-&gt;i</code> is zero (in the case of a real denominator).</p>
  
  <p>How do I get around this? and why did the programmer do this, instead of the more straightforward rule for <a href="http://en.wikipedia.org/wiki/Division_%28mathematics%29#Division_of_complex_numbers" rel="nofollow">complex division</a></p>
  
  <p>The wikipedia rule seems to be better, and no division by zero error would occur here. Did I miss something? Why did the programmer not use the wikipedia formula??</p>
  
  <p>Thanks</p>
  
  <pre><code>/*! @file dcomplex.c
   * \brief Common arithmetic for complex type
   *
   * &lt;pre&gt;
   * -- SuperLU routine (version 2.0) --
   * Univ. of California Berkeley, Xerox Palo Alto Research Center,
   * and Lawrence Berkeley National Lab.
   * November 15, 1997
   *
   * This file defines common arithmetic operations for complex type.
   * &lt;/pre&gt;
   */
  
  #include &lt;math.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include "slu_dcomplex.h"
  
  
  /*! \brief Complex Division c = a/b */
  void z_div(doublecomplex *c, doublecomplex *a, doublecomplex *b)
  {
      double ratio, den;
      double abr, abi, cr, ci;
  
      if( (abr = b-&gt;r) &lt; 0.)
          abr = - abr;
      if( (abi = b-&gt;i) &lt; 0.)
           abi = - abi;
      if( abr &lt;= abi ) {
          if (abi == 0) {
              fprintf(stderr, "z_div.c: division by zero\n");
              exit(-1);
          }   
          ratio = b-&gt;r / b-&gt;i ;
          den = b-&gt;i * (1 + ratio*ratio);
          cr = (a-&gt;r*ratio + a-&gt;i) / den;
          ci = (a-&gt;i*ratio - a-&gt;r) / den;
      } else {
          ratio = b-&gt;i / b-&gt;r ;
          den = b-&gt;r * (1 + ratio*ratio);
          cr = (a-&gt;r + a-&gt;i*ratio) / den;
          ci = (a-&gt;i - a-&gt;r*ratio) / den;
      }
      c-&gt;r = cr;
      c-&gt;i = ci;
  }
  </code></pre>