title: Poisson calculation (erlang C)
body: <p>i posted this before, user told me to post it on codereview. i did, and they closed it...so one more time here: (i deleted the old question)</p>
  
  <p>I have these formulas:</p>
  
  <p><a href="https://i.stack.imgur.com/nZzLd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nZzLd.png" alt="enter image description here"></a></p>
  
  <p>and I need the Poisson formulas for the erlangC formula:</p>
  
  <p><a href="https://i.stack.imgur.com/CKBUB.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CKBUB.gif" alt="enter image description here"></a></p>
  
  <p>I tried to rebuild the formulas in C:</p>
  
  <pre><code>double getPoisson(double m, double u, bool cumu)
  {
      double ret = 0;
      if(!cumu)
      {
          ret = (exp(-u)*pow(u,m)) / (factorial(m));
      }
      else
      {
          double facto = 1;
          double ehu = exp(-u);
          for(int i = 0; i &lt; m; i++)
          {
              ret = ret + (ehu * pow(u,i)) / facto;
              facto *= (i+1);
          }
       }
       return ret;
  }
  </code></pre>
  
  <p>The Erlang C Formula:</p>
  
  <pre><code>double getErlangC(double m, double u, double p)
  {
      double numerator = getPoisson(m, u, false);
      double denominator = getPoisson(m, u, false) + (1-p) * getPoisson(m, u, true);
      return numerator/denominator;
  }
  </code></pre>
  
  <p>The main problem is, the <code>m</code> parameter in <code>getPoisson</code> is a big value (>170)
  so it wants to calculate >170! but it cannot handle it.  I think the primitive data types are too small to work correctly, or what do you say?</p>
  
  <p>BTW: This is the factorial function I use for the first Poisson:</p>
  
  <pre><code>double factorial(double n)
  {
      if(n &gt;= 1)
          return n*factorial(n-1);
      else
          return 1;
  }
  </code></pre>
  
  <p>Some samples:</p>
  
  <p>Input:</p>
  
  <pre><code>double l = getErlangC(50, 48, 0.96);
  printf("%g", l);
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>0.694456 (correct)
  </code></pre>
  
  <p>Input:</p>
  
  <pre><code>double l = getErlangC(100, 96, 0.96);
  printf("%g", l);
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>0.5872811 (correct)
  </code></pre>
  
  <p>if i use a value higher than 170 for the first parameter (m) of getErlangC like:</p>
  
  <p>Input:</p>
  
  <pre><code>double l = getErlangC(500, 487, 0.974);
  printf("%g", l);
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>naN (incorrect)
  </code></pre>
  
  <p>Excepted:</p>
  
  <pre><code>0.45269
  </code></pre>
  
  <p>How's my approach? Would be there a better way to calculate Poisson and erlangC?</p>
  
  <p>Some Info: Excel has the POISSON Function, and on Excel it works perfekt... would there be a way to see the algorithm(code) EXCEL uses for POISSON?</p>