gpt4 book ai didi

c - 为什么我的数组显示为整数而不是 float

转载 作者:行者123 更新时间:2023-11-30 20:08:10 24 4
gpt4 key购买 nike

问题是这样的

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

最佳答案

  1. ^ 是 C 语言中的异或,而不是求幂。
  2. 如果您对 int 进行数学运算,您将得到 int 结果。您需要将其中一些 a[i] 转换为 floatdouble 以获得浮点运算。

关于c - 为什么我的数组显示为整数而不是 float ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58032445/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com