C - Comparing floating point numbers?

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

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

Assuming your compiler support it, you should use DBL_MIN rather than an arbitrary constant. DBL_MIN is (usually? ) defined as 2.2250738585072014e-308 which is rather a lot less than 0.

0000005 and may help eliminate false positives – Steve Mallam Aug 10 at 9:53 4 @Steve Mallam: If DBL_MIN is the smallest representable floating point number, then what would be the purpose of checking for less than that? – Greg Hewgill Aug 10 at 9:54 1 @Greg: in fact DBL_MIN is the smallest normalized double value, the implementation may support denormed values that are smaller. – Steve Jessop Aug 10 at 9:58 @Steve Jessop: Good point about denormalised values, thanks.

– Greg Hewgill Aug 10 at 10:02 2 @Steve Mallam: yeah, depends how the questioner is printing it and how small a value they think should count as zero for their purposes. I was assuming to six decimal places although I didn't state that, hence my code. DBL_MIN is very small, though, not many calculations that have errors at all, are quite that close.

Sometimes DBL_EPSILON, multiplied by the magnitude of the numbers in in the calculation, is good. With %g, even the exact double value 0.0 doesn't print as 0.000000, I don't think any value does. So I don't know exactly what's being asked, this is my best guess, %f.

– Steve Jessop Aug 10 at 10:15.

This is fundamental problem with floating point arithmetic on modern computers. They are by nature imprecise, and cannot be reliably compared. For example, the language ML explicitly disallows equality comparison on real types because it was considered too unsafe.

See also the excellent (if a bit long and mathematically oriented) paper by David Goldberg on this topic. Edit: tl;dr: you might be doing it wrong.

It will compile, but it doesn't do what you want it to. Try it with x=-0.00002; or with x=5e-6;. – dmckee Aug 10 at 16:04 2 You want -0.00001 – Keith Thompson Aug 10 at 22:16 it was meant in a mathmatical way... but you are right keith – theknut Aug 23 at 21:12.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions