Issue 517 - add some interim error checking and reporting
git-svn-id: http://google-refine.googlecode.com/svn/trunk@2420 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
parent
8ec10a6ea6
commit
fa2e6fe608
@ -59,16 +59,17 @@ public class Combin implements Function {
|
||||
return Combin.combination(((Number) args[0]).intValue(), ((Number) args[1]).intValue());
|
||||
}
|
||||
|
||||
public static int combination(int n, int k){
|
||||
public static long combination(long n, long k){
|
||||
if (k > n) {
|
||||
throw new IllegalArgumentException ("the number of elements, n, should be larger than the number of combinations, k");
|
||||
}
|
||||
if (n < 1) {
|
||||
throw new IllegalArgumentException ("the number of elements, n, cannot be less than 1");
|
||||
}
|
||||
int nFact = FactN.factorial(n, 1);
|
||||
int rFact = FactN.factorial(k, 1);
|
||||
int nminusrFact = FactN.factorial(n - k, 1);
|
||||
// TODO: This needs to use a more efficient Binomial Coefficient algorithm
|
||||
long nFact = FactN.factorial(n, 1);
|
||||
long rFact = FactN.factorial(k, 1);
|
||||
long nminusrFact = FactN.factorial(n - k, 1);
|
||||
return nFact / (rFact * nminusrFact);
|
||||
}
|
||||
|
||||
|
@ -65,11 +65,17 @@ public class FactN implements Function {
|
||||
* e.g. A double factorial would have a step of 2.
|
||||
* Returns 1 for zero and negative integers.
|
||||
*/
|
||||
public static int factorial(int i, int step){
|
||||
if(i <= 1) {
|
||||
public static long factorial(long i, long step){
|
||||
if (i < 0) {
|
||||
throw new IllegalArgumentException("Can't compute the factorial of a negative number");
|
||||
} else if(i <= 1) {
|
||||
return 1;
|
||||
} else {
|
||||
return i * FactN.factorial(i - step, step);
|
||||
long result = i * FactN.factorial(i - step, step);
|
||||
if (result < 0) {
|
||||
throw new ArithmeticException("Integer overflow computing factorial");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user