gpt4 book ai didi

c - split 的意外结果

转载 作者:太空宇宙 更新时间:2023-11-04 08:41:09 25 4
gpt4 key购买 nike

我尝试实现以下计算:

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

int main(){

float sum = 0;
int c = -4;
float x1 = 136.67;
float x2 = 2.38;

sum = c / (pow(abs(x1 - x2), 2));
printf("%f %f %f",sum,x1,x2);

return 0;
}

但是sum执行后的值竟然是-4。怎么了?

最佳答案

abs<stdlib.h> 中声明.它需要一个 int参数并返回 int结果。

失踪的#include <stdlib.h>也很可能会导致问题。您正在调用 abs使用浮点参数;如果没有可见的声明,编译器可能会假设它需要一个 double参数,导致代码不正确。 (这是根据 C90 规则;根据 C99 规则,该调用违反了约束,需要进行编译时诊断。)

你想要 fabs <math.h> 中定义的函数.

关于c - split 的意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23525379/

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