【发布时间】:2019-08-08 06:13:03
【问题描述】:
我是 C++ 新手,我编写了一个函数,它接受 a 和 b 作为输入,并返回 a 的 b 次幂。我为函数返回数据类型和数据类型使用模板。但它无法返回大数的正确结果。我尝试计算 pow(2,50) 并返回 0。
template <class numeric_type>
numeric_type pow(numeric_type a, int b) {
numeric_type result = 1;
for (int i = 0; i < b; i++) {
result *= a;
cout << result << i << endl;
}
return result;
}
在main里面:
long long int powered = pow(2, 50);
cout << powered;
【问题讨论】: