What is the best way to represent arbitrarily big numbers in c?

可紊 提交于 2019-12-28 06:24:27

问题


I'm working on a project that requires me to work with numbers larger than the largest numerical datatype in c. I was thinking of using structs with bit fields to represent this, but it's already smelling bad. Anyone got any tips? (Not looking for a library, more of a thought process to go behind doing something like this.)


回答1:


I suggest to first check out the GNU MP Bignum library.

If licensing is a problem you have to roll your own. My first choice for the data-type would be a simple array of unsigned chars along with some extra data to denote how large that array is.

Something like this:

typedef struct 
{
  unsigned char * NumberData;
  size_t          AllocatedSize;
} MyBigNum;

Should be sufficient.




回答2:


The GNU MP Bignum Library would be my first choice.



来源:https://stackoverflow.com/questions/619764/what-is-the-best-way-to-represent-arbitrarily-big-numbers-in-c

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