Factorial does not work for all values

試著忘記壹切 提交于 2019-11-29 17:46:15

Integer overflow.

If int is 32 bits on your system, the largest value it can represent is 2147483647. 13 factorial is 6227020800.

If you use a 64-bit integer type such a long long, you can go up to 20 factorial.

Floating-point will give you more range, but with a loss of precision.

If you really need to compute large factorials, you'll need to use some multi-precision library like GMP, or use a language that has built-in arbitrary-precision integer arithmetic (C doesn't).

The following might call factorial with a zero or negative argument:

z=factorial(n-1-k);
w=factorial(n-k);

You need to make sure that your factorial function can handle such arguments without crashing (I suspect it doesn't).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!