简体   繁体   English

如何使用C++中的Class函数来保存大数

[英]How to use the Class function in C++ to save large numbers

I am creating a C++ program on Astronomy and the numbers are too big for any integer type to keep.我正在创建一个关于天文学的 C++ 程序,但对于任何整数类型来说,这些数字都太大了。 I found out some articles that they pointed out that it can be done using the class function.我发现了一些文章,他们指出可以使用类函数来完成。 The numbers I am dealing with is like a few hundred thousands of light years converted into kilometers, so the number is something like 3.081814457932196587132947576e+27.我处理的数字就像几十万光年转换成公里,所以这个数字类似于 3.081814457932196587132947576e+27。 Any ideas how to save such a large number?任何想法如何保存这么大的数字?

If you really need that many significant digits in floating point format, you should probably look at a well-known library like MPFR .如果您真的需要浮点格式的许多有效数字,您可能应该查看像MPFR这样的知名库。

If you don't need floating point, take a look at the de-facto standard multi-precision library GMP (which MPFR itself is based on).如果您不需要浮点数,请查看事实上的标准多精度库GMP (MPFR 本身就是基于该库)。

Note that both are C libraries.请注意,两者都是 C 库。 GMP has some C++ bindings/wrappers, but not everything is wrapped, so you'll still need to call the C functions for all but the most basic arithmetic. GMP 有一些 C++ 绑定/包装器,但并不是所有的东西都被包装了,所以除了最基本的算术之外,你仍然需要调用 C 函数。 Same goes for MPFR (there's a bunch of C++ wrappers listed on their site). MPFR 也是如此(在他们的网站上列出了一堆 C++ 包装器)。

If you only want to handle integers it's quite simple.如果你只想处理整数,那很简单。

You can store your numbers into arrays, like :您可以将数字存储到数组中,例如:

big_num = [3, 0, 8, 1, 8, 1, 4, 4, 5, 7, 9, 3, 2, 1, 9, 6, 5, 8, 7, 1, ... 6]

At each index of the array you have one digit在数组的每个索引处都有一个数字

You can implement yourself the computation functions, or you can use a big num lib like :您可以自己实现计算功能,或者您可以使用一个大的 num 库,如:

https://github.com/kokke/tiny-bignum-c https://github.com/kokke/tiny-bignum-c

https://www.di-mgt.com.au/bigdigits.html https://www.di-mgt.com.au/bigdigits.html

https://gmplib.org/ https://gmplib.org/

I sure you can find the lib which will realy suits you on the internet.我相信你可以在互联网上找到真正适合你的库。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM