0

I am trying the below mentioned code(PHP) to find probability. The calculation includes combination calculation of large numbers, using BCmaths function but not getting results. Please suggest, how this can be done.

 function combin($n, $r)
{
  $C = 1;

  for ($i=0;   $i < $n-$r;   $i++)
      {
       $C = bcdiv(bcmul($C, $n-$i), $i+1);
      }
  return $C;
}

$dv = (combin(68, 17))*(combin((7866-68),(177-17)))/combin(7866, 177);
echo $dv;
?>```
  • The code in your question has a syntax error (missing a closing parenthesis at the end of the `$dv = ...` line). Is this actual code or did you just make a mistake when transferring it to the question? – El_Vanja Apr 09 '21 at 16:39
  • made a mistake while copying it here. Syntax is correct as I have checked it for smaller numbers – Indra Kundu Apr 09 '21 at 16:49
  • In that case, start by checking for accidental [division by zero errors](https://stackoverflow.com/questions/3071067/php-how-to-catch-a-division-by-zero). – El_Vanja Apr 09 '21 at 16:53

1 Answers1

0

Once you start using the bc* functions, you should continue to use them throughout the code. You however, are taking the results of these functions and then using the standard PHP operators on them. You should be able to change your calling code to:

$dv = bcdiv(combin(7866, 177), bcmul(combin(68, 17), (combin(7866 - 68, 177 - 17))));
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • Thankyou, Chris Haas. I was doing ```$dv = bcmul((combin(68, 17)),bcdiv((combin((7866-68),(177-17))), combin(7866, 177)));``` and this was leading to division by zero error. – Indra Kundu Apr 09 '21 at 18:16
  • It should be ```$dv = bcdiv((bcmul(combin(68, 17), (combin(7866 - 68, 177 - 17)))),combin(7866, 177))``` – Indra Kundu Apr 09 '21 at 19:13