I'm using ulong for the factorial of 100, but it still overflows

三世轮回 提交于 2019-11-28 14:50:05

All integer types have limits. unsigned long int increased the upper limit. But apparently not nearly far enough. As the others have said in comments, ulong is to short by 100+ orders of magnitude.

For such huge numbers there is two options:

  1. use floating point numbers. Asuming you can live with their inherent inprecision and all the other Floating point stuff.
  2. Use BigInteger. That one will only run into limits like the max objects size or adresseable RAM. So you should be save up to 2 GiB or so.

Personally I tend to squeeze operations into the BigInt rather then use Floating point numbers. But that is a personal mater.

I've been playing around with my own large math research projects. You can use the .NET BigInteger class (under System.Numerics), but it's not the most efficient library out there.

If you aren't stuck on .NET, I'd suggest using the GNU Multiple Precision Arithmetic Library (https://gmplib.org/). It is both far faster and has far more functionality. You will need to study the docs to learn to use it properly.

Ports of it do exist, though I haven't seen a great port from an API perspective - do a search on Nuget.

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