gpt4 book ai didi

c - 不使用 sqrt 函数的 C 平方根程序

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

我正在尝试用 C 编写一个程序,使用公式 NG = 0.5( LG + N/LG) 查找数字的近似平方根。到目前为止我已经:

#include <stdio.h> 
#include <math.h>
int main(){
double n;
double LG=1;
double NG;
printf("Enter number");
scanf_s("%lf",&n);
do{
NG=(.5*(LG+n/LG));
LG=NG;
}while((NG*NG-n)<.005);
printf("The root is %lf",NG);
}

这个结构在java中工作正常,但循环似乎没有在C中执行。

感谢您的建议。

最佳答案

您不想在 NG*NG-n 小于 0.005 时循环。您希望在 NG*NG 距离 n 比预期更远时进行循环。

NG*NGn 之间的距离为 fabs(NG*NG - n)

关于c - 不使用 sqrt 函数的 C 平方根程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21858152/

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