C语言:求一个数的平方根

正数n的平方根可以通过计算一系列近似值来获得,每个近似值都比前一个更加接近准确值。第一个近似值是1,接下来的近似值则通过下面的公式来获得。
《C语言:求一个数的平方根》

编写一个程序,读入一个值,计算并打印出它的平方根。如果你将所有的近似值都打印出来,你会发现这种方法获得准确结果的速度有多快。原则上,这种计算可以永远进行下去,它会不断产生更加精确的结果。但在实际中,由于浮点变量的精度限制,程序无法一直计算下去。当某个近似值与前一个近似值相等时,你就可以让程序停止继续计算了。

必须使用浮点变量,而且程序应该对负值输入进行检查。

  1. // 计算一个数的平方根
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main(){
  5. float new_guess;
  6. float last_guess;
  7. float number;
  8. // 催促用户输入,读取数据并对它进行检查
  9. printf(“Enter a number: ” );
  10. scanf(“%f”, &number );
  11. if( number < 0 ){
  12. printf(“Cannot compute the square root of a ” “negative number! \n”);
  13. return EXIT_FAILURE;
  14. }
  15. // 计算平方根的近似值,直到它的值不再变化
  16. new_guess = 1;
  17. do{
  18. last_guess = new_guess;
  19. new_guess = ( last_guess + number / last_guess ) / 2;
  20. printf (“%.15e\n”, new_guess );
  21. } while( new_guess != last_guess );
  22. // 打印结果
  23. printf (“Square root of %g is %g\n”, number, new_guess );
  24. return EXIT_SUCCESS;
  25. }
    原文作者:哆啦A梦_i
    原文地址: https://blog.csdn.net/weixin_44015669/article/details/90721441
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞