-2

I have function to calculate binomial coefficient with this formula p(n, x) = n!/(n-x)!.x!

As implementation is recursive, I want to stop the further function execution once resultant coefficient exceeds INT_MAX,and it should return -1.

But instead of returning from that case, recursive function considering this return value as computation value return by internal recursive function.

which giving a wrong result.

How can be this handled?

#include <iostream>
#include <cstdlib>
int INT_MAX = 100;
using namespace std;

int binomial_function(int n, int k){
  int res = -1;
  if (n<k || k<0 ){
  return -1;
  }
  if(k == n || k == 0){
    return 1;
  }
  res = binomial_function(n-1,k-1) + binomial_function(n-1,k);
  if (res >= INT_MAX){   // checking overflow of res
    return -1;
  }
  return res;
}

int main(int argc, char *argv[]){
  int coefficient = 0;

  coefficient = binomial_function(10,5); // actual binomial coeficient is 252 which overflows to INT_MAX
  cout<<coefficient<<endl;

  coefficient = binomial_function(10,6);  // actual binomial coeficient is 210 which overflows to INT_MAX
  cout<<coefficient<<endl;

  coefficient = binomial_function(10,8);  // actual binomial coeficient is 1 which does overflows to INT_MAX
  cout<<coefficient<<endl;

  return 0;
}

Actual output is:

-2
83
45

Expected output:

-1   >> this should be result because coefficient exceeds INT_MAX
-1   >> this should be result because coefficient exceeds INT_MAX
45   >> this should be result because coefficient does not exceeds INT_MAX
Akash Pagar
  • 637
  • 8
  • 22
  • 4
    `if (res >= INT_MAX){ // checking overflow of res` - this makes no sense. Signed integer overflow is UB. Once you've overflowed, you've already failed. – Evg Jun 28 '20 at 09:48
  • 1
    Seems a few people are missing that the code contains this line `int INT_MAX = 100;` – john Jun 28 '20 at 09:56
  • That line is still misleading. We have `INT_MAX` in the standard, and if I see `INT_MAX`, I expect that very constant, not some arbitrary number defined somewhere else. – Evg Jun 28 '20 at 10:03

1 Answers1

3

You should check if the return values are not errors before doing the addition.

  int res1, res2;
  res1 = binomial_function(n-1,k-1);
  res2 = binomial_function(n-1,k);
  if (res1 < 0 || res2 < 0){   // checking errors of return values
    return -1;
  }
  res = res1 + res2;
  if (res >= INT_MAX){   // checking overflow of res
    return -1;
  }
MikeCAT
  • 73,922
  • 11
  • 45
  • 70