Most efficient implementation of a large number class

孤街醉人 提交于 2019-11-26 14:44:22

问题


When doing calculations on very large numbers where integral data types such as double or int64 falls short, a separate class to handle such large numbers may be needed.

Does anyone care to offer an efficient algorithm on how best to do this?


回答1:


There are 2 solutions to your problem:

  • Easy way: Use an external library such as 'The GNU MP Bignum Library and forget about implementation details.

  • Hard way: Design your own class/structure containing multiple higher order datatypes like double or int64 variables and define basic math operations for them using operator overloading (in C++) or via methods named add, subtract, multiply, shift, etc. (in JAVA and other OO languages).

Let me know if you need any further help. I have done this a couple of times in the past.




回答2:


In C# 4.0 use the BigInteger type




回答3:


You're asking about arbitrary-precision arithmetic, a subject on which books have been written. If you just want a simple and fairly efficient BigNum library for C#, you might want to check out IntX.




回答4:


Using the built-in features of a language work for me.

Java has BigInteger and BigDecimal, and Python automagicaly switches to an object similar to Java's if a number gets out of the range of an integer or whatnot.

As for other languages though, I have no idea.

I hate re-inventing the wheel.




回答5:


Doing your own BigNum library is complicated, so i'd say like jjnguy. Use whatever your language offers as libraries.

In .net, reference the VisualJ dll as they contain the BigInteger and BigDecimal classes. You should however be aware of some limitations of these libraries, like the lack of a square root method, for example.



来源:https://stackoverflow.com/questions/26094/most-efficient-implementation-of-a-large-number-class

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