gpt4 book ai didi

c - 在 C 中分配 double 值会出乎意料地起作用

转载 作者:行者123 更新时间:2023-11-30 21:09:49 25 4
gpt4 key购买 nike

我尝试阅读很多有关 SO 的问题,但我似乎无法理解为什么我的程序 react 如此奇怪。我有这个简单的代码尝试实现 sqrt 函数:

double sqrt,high,low,currNum;
scanf("%lf",&sqrt);
high = sqrt;
low = 0.0;
currNum = (high+low)/2.0;

调试并到达最后一行之后的行后,我得到以下结果:sqrt 等于 6(这是输入的数字..)高等于 0(???)。低等于 0。currnum 等于 6(???)。

我只是不明白这个值(value)观的行为......请帮忙?

最佳答案

I cant seem to understand why my program reacts so strange.. I have this simple code...

我会解释你的程序。该行在内存中保留了四个空间,其中每个空间的大小都是 double ,但没有为任何空间分配特定值。然后您可以期望每个值的随机值。

double sqrt,high,low,currNum;

如果您想让每个值开始为零,请改用此行:

double sqrt=0,high=0,low=0,currNum=0;

该行读取并接受 double 类型的数字输入,并将该值存储在 sqrt 变量中:

scanf("%lf",&sqrt);

这将 sqrt 的值设置为高:

high = sqrt;

这将低值设置为 0.0:

low = 0.0;

这会计算用户输入内容的总和,然后将总数除以一半(也称为除以 2),然后将结果存储在 currNum 变量中:

currNum = (high+low)/2.0;

after debugging and reaching the line after the last one, I have these results: sqrt equals to 6(this is the number which was entered..) high equals to 0(????). low equals 0. currnum equals 6(???). I just dont understand the behavior of this values... help please?

因为您将“low”设置为零,所以您在此处创建的程序会计算用户输入的数字的一半。

...trying to Implement sqrt function:

我确信如果您使用 C 库,您将获得一个很好的平方根函数。该程序打印 9 的平方根。

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

int main(){
double x=sqrt(9);
printf("%lf",x);
return 0;
}

关于c - 在 C 中分配 double 值会出乎意料地起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33454553/

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