C++ 如何将无限长的二进制转换为十进制

C++ 如何将无限长的二进制转换为十进制,c++,algorithm,math,C++,Algorithm,Math,最常见的方法是求二进制数的每个非零位置的2的幂,然后求和。当二进制数很大时,这是不可行的,比如 10000…0001//1000000个职位 不可能让计算机计算功率(21000000)。因此,传统的方法是行不通的 还有别的办法吗 有人能给出一个关于如何计算的算术方法,而不是库吗 您可以编写自己的类来处理任意大的整数(您可以将其表示为整数数组或任何最有意义的东西),并自己实现操作(*、pow等)。或者你可以在谷歌上搜索“C++大整数库”,然后找到其他已经实现了它的人。我在Smalltalk中计算了

最常见的方法是求二进制数的每个非零位置的2的幂,然后求和。当二进制数很大时,这是不可行的,比如

10000…0001//1000000个职位

不可能让计算机计算功率(21000000)。因此,传统的方法是行不通的

还有别的办法吗


有人能给出一个关于如何计算的算术方法,而不是库吗

您可以编写自己的类来处理任意大的整数(您可以将其表示为整数数组或任何最有意义的东西),并自己实现操作(*、pow等)。或者你可以在谷歌上搜索“C++大整数库”,然后找到其他已经实现了它的人。

我在Smalltalk中计算了2**1000000,并在9.3秒内将其转换为十进制,所以这不是不可能的。Smalltalk内置了大型整数库

2 raisedToInteger: 1000000
正如在另一个答案中提到的,您需要一个处理任意精度整数的库。一旦你有了它,你就对它进行MOD 10和DIV 10运算,以反序计算十进制数字(从最低有效到最高有效)

大致的想法是这样的:

LargeInteger *a;
char *string;


while (a != 0) {
    int remainder;
    LargeInteger *quotient;

    remainder = a % 10.
    *string++ = remainder + 48.
    quotient = a / 10.
    } 
关于类型转换、内存管理和对象分配,这里缺少(或错误)许多细节,但这是为了演示一般技术

不可能让计算机计算功率(21000000)。因此,传统的方法是行不通的

这并非不可能。例如,Python可以立即进行算术计算,并在大约两秒钟内(在我的机器上)转换为十进制数。Python内置了处理超过机器字大小的大整数的工具

在C++(和C)中,大整数库的一个好的选择是。它是健壮的、经过良好测试的,并且能够积极维护。它包括一个使用运算符重载来提供一个漂亮的接口(除了,没有C++运算符的<代码>()(代码/操作)。

这里是一个使用GMP:

的C++示例
#include <iostream>
#include <gmpxx.h>

int main(int, char *[])
{
    mpz_class a, b;
    a = 2;
    mpz_pow_ui(b.get_mpz_t(), a.get_mpz_t(), 1000000);
    std::string s = b.get_str();
    std::cout << "length is " << s.length() << std::endl;
    return 0;
}
#包括
#包括
int main(int,char*[])
{
mpz_a类、b类;
a=2;
mpz_pow_ui(b.get_mpz_t(),a.get_mpz_t(),1000000);
std::string s=b.get_str();

std::cout使用非常简单。不幸的是,我无法测试这个程序,因为在编译器升级后,我似乎需要重建我的库。但不会有太多的错误空间

#include "gmpxx.h"
#include <iostream>

int main() {
    mpz_class megabit( "1", 10 );
    megabit <<= 1000000;
    megabit += 1;
    std::cout << megabit << '\n';
}
#包括“gmpxx.h”
#包括
int main(){
mpz_级兆位(“1”,10);

megabit正如happydave所说,现有的库(如GMP)可用于这类内容。如果出于某种原因需要推出自己的库,以下是一个合理有效的方法概述。 你需要大整数减法、比较法和乘法

以二进制格式缓存10^(2^n)的值,直到下一个值大于二进制数。这将允许您通过执行以下操作快速生成10的幂:

Select the largest value in your cache smaller than your remaining number, store this
in a working variable.
do{
  Multiply it by the next largest value in your cache and store the result in a
  temporary value.
  If the new value is still smaller, set your working value to this number (swapping 
  references here rather than allocating new memory is a good idea),
  Keep a counter to see which digit you're at. If this changes by more than one
  between instances of the outer loop, you need to pad with zeros
} Until you run out of cache
This is your next base ten value in binary, subtract it from your binary number while
the binary number is larger than your digit, the number of times you do this is the 
decimal digit -- you can cheat a little here by comparing the most significant bits
and finding a lower bound before trying subtraction.
Repeat until your binary number is 0

关于二进制数字的数量,这大约是O(n^4),关于内存,这大约是O(nlog(n))。通过使用更复杂的乘法算法,可以使n^4更接近n^3。

“关于二进制数字的数量,这大约是O(n^4),关于内存,这大约是O(nlog(n))。你可以做O(n^(2+epsilon))操作(其中n是二进制数字的数量)和O(n)内存如下:设n是二进制长度n的巨大数字。计算剩余mod 2(简单;获取低位)和mod 5(不容易,但也不可怕;将二进制字符串分成连续的四位字符串;计算每一个这样的四元组的剩余模5,并将它们相加,就像对十进制数字进行9的转换一样。)通过计算剩余模2和5,你可以读出低十进制数字。减去它;除以10(互联网文档提供了这样做的方法),然后重复以获得下一个最低的数字。

能够将1兆位数字转换为十进制。“关于如何计算的算术方法”术语“长乘法”听起来熟悉吗?
Select the largest value in your cache smaller than your remaining number, store this
in a working variable.
do{
  Multiply it by the next largest value in your cache and store the result in a
  temporary value.
  If the new value is still smaller, set your working value to this number (swapping 
  references here rather than allocating new memory is a good idea),
  Keep a counter to see which digit you're at. If this changes by more than one
  between instances of the outer loop, you need to pad with zeros
} Until you run out of cache
This is your next base ten value in binary, subtract it from your binary number while
the binary number is larger than your digit, the number of times you do this is the 
decimal digit -- you can cheat a little here by comparing the most significant bits
and finding a lower bound before trying subtraction.
Repeat until your binary number is 0