Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么C上的powerint不正确?_C_Math_Pow - Fatal编程技术网

为什么C上的powerint不正确?

为什么C上的powerint不正确?,c,math,pow,C,Math,Pow,大家好,我正在用C99做我的家庭作业,但是有一些事情让我很不安,请在回答之前先试试我的代码。 问题是,当我尝试数字5,10,15时,答案是不正确的,它总是正确的值-1 有人能解释一下为什么吗 #include <stdio.h> #include <math.h> int main() { int P, x; printf("Calculons le polynome P(x)\n"); printf("Entrer la valeur de

大家好,我正在用C99做我的家庭作业,但是有一些事情让我很不安,请在回答之前先试试我的代码。 问题是,当我尝试数字5,10,15时,答案是不正确的,它总是正确的值-1

有人能解释一下为什么吗

#include <stdio.h>
#include <math.h>

int main() {
    int P, x;

    printf("Calculons le polynome P(x)\n");

    printf("Entrer la valeur de x :");

    scanf("%d", &x);

    P = pow(x,2);

    printf("%d", P);

    return 0;
}
#包括
#包括
int main(){
int P,x;
printf(“计算多项式P(x)\n”);
printf(“enter la valeur de x:”);
scanf(“%d”和&x);
P=功率(x,2);
printf(“%d”,P);
返回0;
}

数学.h中函数pow的签名为

double pow(double x, double y);
由于传递两个整数并将结果赋给一个整数,因此我猜a由于舍入误差和小数部分的截断而得到错误的结果(如果将24.999999赋给整数变量,则得到24)。您还需要始终检查scanf的返回值。此外,输出需要以换行结束,否则结果可能不会显示

以下是更正的版本:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    double P, x;
    int n;

    printf("Calculons le polynome P(x)\n");
    printf("Entrer la valeur de x: ");
    n = scanf("%lf", &x);
    if (n == 1) {
        P = pow(x, 2.0);
        printf("%.2f\n", P);
    } else {
        fprintf(stderr, "Entrée invalide\n");
        exit(EXIT_FAILURE);
    }
    return 0;
}
#包括
#包括
#包括
内部主(空)
{
双P,x;
int n;
printf(“计算多项式P(x)\n”);
printf(“enter la valeur de x:”);
n=扫描频率(“%lf”和&x);
如果(n==1){
P=功率(x,2.0);
printf(“%.2f\n”,P);
}否则{
fprintf(标准,无效登记);
退出(退出失败);
}
返回0;
}

我不想使用x*x,我只是简化了方程,方程比x^2有点复杂,而且我想知道为什么

问题是,如果我把pow(5,2)放进去,它工作得很好,我有25个

当我给x一个值,比如5,10,15,我就得到了正确的值减去1

我试着将p声明为双精度,它也起作用,我有25.00000 感谢August Karlstrom提供的解决方案,问题是我仍然想知道,如果我只使用pow(5,2),为什么它会起作用

在这里,代码使用了double,并且可以正常工作

#include <stdio.h>
#include <math.h>

int main() {
    double P;
    int x;

    printf("Calculons le polynome P(x)\n");    
    printf("Entrer la valeur de x :");    
    scanf("%d", &x);

    P = pow(x,2);

    printf("%f", P);    
    return 0;
}
#包括
#包括
int main(){
双P;
int x;
printf(“计算多项式P(x)\n”);
printf(“enter la valeur de x:”);
scanf(“%d”和&x);
P=功率(x,2);
printf(“%f”,P);
返回0;
}

@MichaelWalz它应该按原样工作。不能复制;你的代码给了我正确的结果。@TavianBarnes:我认为OP意味着它总是正确的值减去1。对整数使用
pow
通常是个坏主意,特别是对于像整数平方这样简单的事情。您正在从
int
转换为double,将转换后的值传递给
pow
,然后从
double
转换回
int
。要求整数的平方,只需将其自身相乘(标准库中没有整数幂函数)。我可以想象,在某些系统上,例如,
pow(5,2)
可能会产生类似
24.9999999
,当转换为
int
时,会截断为
24
。您使用的是什么操作系统和C实现?这假设OP想要解决的问题是正确使用
pow
。如果没有更多的反馈,我们就无法判断,但我怀疑真正的问题是如何对整数进行平方运算,而这方面的最佳解决方案是
x*x
@KeithThompson!我假设海报的代码稍微简化了一些,以说明问题。但是,即使对于一般多项式,使用Horner方法而不是pow也可能更好。我明白了,谢谢你的解释,我会检查Horner方法,但是你有没有解释为什么pow的结果不正确?请不要在回答中提问,而是编辑你的问题。