Friday 15 January 2010

c++ - How to compare doubles with 5 digits of precision? -


I'm trying to compare 2 couple who came in [0.0, 1.0] Are there.

My task (taken from) -

  Inline bool is equal (double x, double y) {const double ipsilon = 0.000001; Return fibers (X - Y) & lt; Epsilon; }   

Usage -

  cerr & lt; & Lt; Acquel (1.000001, 1.000002) & lt; & Lt; Endl; Serie Lieutenant; & Lt; Acquel (1.000010, 1.000020) & lt; & Lt; Endl;   

Output is -

  0   

I hope the first true , Second to be false please tell me where I am going wrong and how to fix it?

1.000001 being restricted to normal 64-bit IEEE floating point presentation In 1.0000009999999999177333620536956004798412 . Similarly 1.000002 is actually 1.0000020000000000575113290324225090444087 . Apart from 0.000001 , both are slightly more slightly different.

You can use a slightly larger comparison value to catch it:

  const double eppsilon = 0.0000011;   

It is not possible to completely eliminate problems of any time with floating point numbers.

No comments:

Post a Comment