comparison maths/MathUtilities.cpp @ 360:a694700f71d8

Factorial: return double (int not big enough for many popular values)
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 16 Oct 2013 16:44:14 +0100
parents a067c2eeb13c
children 7e8d1f26b098
comparison
equal deleted inserted replaced
359:0ea56b09577e 360:a694700f71d8
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 }