简体   繁体   English

从任何基础到基础10 c ++的转换

[英]conversion from any base to base 10 c++

I found two ways of conversion from any base to base 10 . 我发现从任何基数到10的两种转换方式。 the first one is the normal one we do in colleges like 521(base-15) ---> (5*15^2)+(2*15^1)+(1*15^0)=1125+30+1 = 1156 (base-10) . 第一个是我们在大学里像521(base-15)这样的普通人--->(5 * 15 ^ 2)+(2 * 15 ^ 1)+(1 * 15 ^ 0)= 1125 + 30 + 1 = 1156(以10为基)。 my problem is that i applied both methods to a number (1023456789ABCDE(Base-15)) but i am getting different result . 我的问题是我将这两种方法都应用于一个数字(1023456789ABCDE(Base-15)),但结果却有所不同。 google code jam accepts the value generated from second method only for this particular number (ie 1023456789ABCDE(Base-15)) . google code jam仅接受该特定数字(即1023456789ABCDE(Base-15))从第二种方法生成的值。 for all other cases both generates same results . 对于其他所有情况都产生相同的结果。 whats big deal with this special number ?? 这个特殊号码有什么大不了的? can anybody suggest ... 有人可以建议...

#include <iostream>
#include <math.h>
using namespace std;

int main()
{   //number in base 15 is 1023456789ABCDE
    int value[15]={1,0,2,3,4,5,6,7,8,9,10,11,12,13,14};
    int base =15;
    unsigned long long sum=0;
    for (int i=0;i<15;i++)
    {
        sum+=(pow(base,i)*value[14-i]);
    }
    cout << sum << endl; 
    //this prints 29480883458974408
    sum=0;
    for (int i=0;i<15;i++)
    {
        sum=(sum*base)+value[i];
    }
    cout << sum << endl;
    //this prints 29480883458974409
    return 0;
}

Consider using std::stol ( ref ) to convert a string into a long. 考虑使用std::stolref )将字符串转换为long。 It let you choose the base to use, here an example for your number wiuth base 15 . 它允许您选择要使用的基数,这里是使用基数15的示例。

int main()
{
    std::string s = "1023456789ABCDE";
    long n = std::stol(s,0,15);
    std::cout<< s<<" in base 15: "<<n<<std::endl;
    // -> 1023456789ABCDE in base 15: 29480883458974409
}

pow(base, i)使用浮点数,因此您会降低某些数字的精度。

Exceeded double precision. 超过double精度。

Precision of double , the return value from pow() , is precise for at least DBL_DIG significant decimal digits. double精度,即pow()的返回值,至少对于DBL_DIG有效的十进制数字是精确的。 DBL_DIG is at least 10 and typically is 15 IEEE 754 double-precision binary . DBL_DIG 至少为 10,通常为15 IEEE 754双精度二进制文件

The desired number 29480883458974409 is 17 digits, so some calculation error should be expected. 所需的数字29480883458974409是17位数字,因此应该会出现一些计算错误。

In particular, sum += pow(base,i)*value[14-i] is done as a long long = long long + (double * long long) which results in long long = double . 特别地, sum += pow(base,i)*value[14-i]是因为long long = long long + (double * long long)导致long long = double The nearest double to 29480883458974409 is 29480883458974408 . 最近double2948088345897440929480883458974408 So it is not an imprecise value from pow() that causes the issue here, but an imprecise sum from the addition. 因此,造成此问题的不是pow()的不精确值,而是加法的不精确总和。

@Mooing Duck in a comment references code to avoid using pow() and its double limitation`. @Mooing Duck在注释中引用了代码,以避免使用pow()及其double限制。 Following is a slight variant. 以下是一个轻微的变体。

unsigned long long ullongpow(unsigned value, unsigned exp) {
  unsigned long long result = !!value;
  while (exp-- > 0) {
    result *= value;
  }
  return result;
}

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

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