Factorial in C family languages

后端 未结 4 1102
我寻月下人不归
我寻月下人不归 2021-01-29 06:42

I\'ve been trying to make a factorial function in C++, and I just found that inputs which are greater than 10 are not calculated correctly. I tried C# but I faced the same probl

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-29 06:49

    That's because int is limited to 32 bits, and results of 10! much beyond 10 will give you a bigger result. For larger values, you can get an approximate result using double as the result. If you want more than about 16 digits precision, you need to use a multiprecision math library.

    Using uint64_t would allow a larger number, but still fairly limited.

提交回复
热议问题