Comparing floating point numbers in C

后端 未结 4 1995
终归单人心
终归单人心 2020-11-29 10:03

I\'ve got a double that prints as 0.000000 and I\'m trying to compare it to 0.0f, unsuccessfully. Why is there a difference here? What

4条回答
  •  情话喂你
    2020-11-29 10:43

    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.

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

提交回复
热议问题