且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

浮点数用C比较

更新时间:2021-11-01 01:54:13

要确定是否足以零接近,它将为 0.000000 打印6位小数,东西这样的:

To determine whether it's close enough to zero that it will print as 0.000000 to six decimal places, something like:

fabs(d) < 0.0000005

小误差在处理浮点运算可以得到一般比较复杂,虽然。

Dealing with small inaccuracies in floating-point calculations can get quite complicated in general, though.

如果你想有一个更好的想法是什么珍惜你所拥有,尝试用%G 打印,而不是%F

If you want a better idea what value you've got, try printing with %g instead of %f.