gpt4 book ai didi

c - math.h 方法工作不同吗?

转载 作者:行者123 更新时间:2023-11-30 18:20:02 28 4
gpt4 key购买 nike

我在c上尝试了一些计算,我的代码如下

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

int main(void)
{
float a = 1.8;
float b = 3.3;
float c = 1.5;
float d = 5.9;
float e = 2.6;

float result = a / (a + (abs(a - b) / (b + (pow(c, 5) / (e + (pow(d, 2) / (b + (pow(c, 3) / 4))))))));
float result2 = a / (a + ((-1) * (a-b)) / (b + ((c * c * c * c * c) / (e + ((d * d) / (b + ((c * c * c) / 4)))))));
printf("Result = %.4f\n", result);
printf("Result2 = %.4f\n", result2);
system("pause");
return(0);
}

Result2 是正确的结果。那么为什么 result 和 result2 不同呢?我在这里做错了什么?

最佳答案

abs 代表 int:

int abs(int j);

fabsfabsf 是您应该使用的:

double fabs(double x);
float fabsf(float x);

编辑:

abs 更改为 fabs 后,本地测试给出:

[pengyu@GLaDOS tmp]$ gcc a.c -lm -Wall -pedantic
[pengyu@GLaDOS tmp]$ ./a.out
Result = 0.8272
Result2 = 0.8272

关于c - math.h 方法工作不同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33186512/

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