为什么我没有得到我期望的输出? [关闭]

问题描述 投票:0回答:1

我找到了数字25的近似平方根。

#include <stdio.h>

main() {
  float a = 25.0;
  float b = 0.0;

  while (abs(b*b - a) >= 0.01) {
    b += 0.00001;
  }
  printf("%f\n", b);
}

我希望循环后b大约为5,有人能解释为什么它显示为0吗?

当我在Python中编写此代码时,它可以正常工作:

a=25.0
b=0.0

while abs(b*b - a) >= 0.01:
    b+=0.00001

print(b)
python c
1个回答
3
投票

是的,abs() doesn't do what you think it does(暗示:它在integers上运作)。相反,使用来自fabsf()math.hhttps://ideone.com/4HphCr

© www.soinside.com 2019 - 2024. All rights reserved.