C++ c+中功率法的5次方误差+;

C++ c+中功率法的5次方误差+;,c++,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; 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;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
双n,p;

您的数据类型可能有问题

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;

pow
返回一个double,可能是
24.99999999….
,当您将它保存在
int
中时,它将是
24
,但我无法复制:详细的答案并不能解决问题。如果电源参数是double,您仍然可能面临类似的问题,并且无法获得确切的电源。@aniliitb10如果您是指数字算法不会给出一个实数的精确值(它们正在舍入该值),这是真的,但这也是事实。
double result = pow(n, p);