浮在C上的麻烦

Ric*_*urt -1 c floating-point square-root

我在C中有这个小程序,它使用递归函数计算x正整数的平方根N(使用while循环实现).如果我x用这个计算:

x = (1/2)*(x + N/x) //x0 = 1.0
Run Code Online (Sandbox Code Playgroud)

然后x继续增长到inf,然后是nan.但是,如果我使用这个:

x = (x + N/x)/2 //x0 = 1.0
Run Code Online (Sandbox Code Playgroud)

它工作正常,为什么?谢谢.

Yu *_*Hao 6

1/2整数除法,其结果是0,改变一个或两个操作数double,例如:

1.0/2
Run Code Online (Sandbox Code Playgroud)