Mercurial > hg > qm-dsp
comparison maths/MathUtilities.cpp @ 135:0fdbb93e92b7
Factorial: return double (int not big enough for many popular values)
author | Chris Cannam |
---|---|
date | Wed, 16 Oct 2013 16:44:14 +0100 |
parents | 5351b5e9ad9f |
children | e4a57215ddee |
comparison
equal
deleted
inserted
replaced
134:5f88f517b637 | 135:0fdbb93e92b7 |
---|---|
377 int n0 = previousPowerOfTwo(x), n1 = nextPowerOfTwo(x); | 377 int n0 = previousPowerOfTwo(x), n1 = nextPowerOfTwo(x); |
378 if (x - n0 < n1 - x) return n0; | 378 if (x - n0 < n1 - x) return n0; |
379 else return n1; | 379 else return n1; |
380 } | 380 } |
381 | 381 |
382 int | 382 double |
383 MathUtilities::factorial(int x) | 383 MathUtilities::factorial(int x) |
384 { | 384 { |
385 if (x < 0) return 0; | 385 if (x < 0) return 0; |
386 int f = 1; | 386 double f = 1; |
387 for (int i = 1; i <= x; ++i) { | 387 for (int i = 1; i <= x; ++i) { |
388 f = f * i; | 388 f = f * i; |
389 } | 389 } |
390 return f; | 390 return f; |
391 } | 391 } |