This question already has answers here:
Why is my power operator (^) not working?
(7个答案)
5个月前关闭。
问题看起来像这样
https://i.imgur.com/u0LJO0g.png
我在哪里弄错了?
我希望输出看起来像这样
但实际输出是
(7个答案)
5个月前关闭。
问题看起来像这样
https://i.imgur.com/u0LJO0g.png
我在哪里弄错了?
#include<stdio.h>
int main(){
int i,n;
int a[] = {3,5,7};
float x[] = {0,0,0};
printf("function f(x)=(x^3-2x^2+10x-5)/(x-10)\n");
for(i = 0;i<3;i++){
x[i] = (a[i]^3-2*(a[i]^2)+10*a[i]-5)/(a[i]-10);
}
for(n = 0;n<3;n++){
printf("if x is %d,f(x) is %f\n",a[n],x[n]);
}
}
我希望输出看起来像这样
if x is 3,f(x) is -5.14
if x is 5,f(x) is -24.00
if x is 7,f(x) is -103.33
但实际输出是
if x is 3,f(x) is -3.000000
if x is 5,f(x) is -7.000000
if x is 7,f(x) is -20.000000
最佳答案
^
在C中是XOR,而不是幂。
如果对int
做数学运算,则将得到int
结果。您需要将其中一些a[i]
强制转换为float
或double
才能进行浮点运算。
关于c - 为什么我的数组以整数而不是浮点数显示,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58032445/