c ++中pow方法的5次幂误差

时间:2017-07-03 14:16:38

标签: c++

每当我写5为n和p为2时,我得到输出为24 ...请让我知道什么是错的?对于其他数字,它完全没问题。

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

int main()
{
    double n, p;
    cout << "enter the number" <<endl;
    cin >> n;
    cout << "enter the power" <<endl;
    cin >> p;
    int result = pow(n, p);
    cout << "Result is " << result;
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您的数据类型有问题!

double n, p; // here you use double types
std::cout << "enter the number" << std::endl;
std::cin >> n;
std::cout << "enter the power" << std::endl;
std::cin >> p;
int result = pow(n, p); // then here you use double pow(double, double) but assign it to an int
std::cout << "Result is " << std::result;

<强>解决方案:

double result = pow(n, p);