麻烦浮动基于C [英] Trouble with float on C

查看:166
本文介绍了麻烦浮动基于C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须用C这个小程序,计算使用递归的正整数 N 的平方根 X 功能(使用whil​​e循环实现)。如果我计算 X 使用此:

I have this little program in C that calculates the square root x of a positive integer N using a recursive function (implemented using a while loop). If I calculate x using this:

x = (1/2)*(x + N/x) //x0 = 1.0

然后 X 持续增长为inf,然后南。但是,如果我用这个:

Then x keeps growing to inf and then nan. However if I use this:

x = (x + N/x)/2 //x0 = 1.0

它工作得很好,为什么呢?谢谢。

It works fine, why? Thanks.

推荐答案

1/2 并整数除法,其结果是 0 ,一个或两个操作数更改为双击,例如:

1/2 does integer division, its result is 0, change either or both operand to double, e.g:

1.0/2

这篇关于麻烦浮动基于C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆