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.)
我正在开发一个项目,要求我使用大于c中最大数值数据类型的数字。我正在考虑使用带有位字段的结构来表示这一点,但它已经闻起来很糟糕了。有人有任何提示吗? (不是在寻找一个图书馆,更多的是一个思考过程,以便做这样的事情。)
2 个解决方案
#1
I suggest to first check out the GNU MP Bignum library.
我建议先查看GNU MP Bignum库。
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.
应该足够了。
#1
I suggest to first check out the GNU MP Bignum library.
我建议先查看GNU MP Bignum库。
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.
GNU MP Bignum图书馆将是我的首选。