`if`

: C++

Copy Code

printf( "i is %18.16f\n", i );That will show you the value of i to sixteen decimal places.

To get around this you need to allow for a tolerance which is often called epsilon. There are lots of ways to deal with this and here is one option. Write a function called

`CloseEnough`

or something similar. Here's how it might look. C++

Copy Code

// function will return 1 if close enough, 0 if not int CloseEnough( double value1, double value2 ) { double delta = fabs( value1 - value2 ); // obtain absolute value of difference return ( delta < 1.0E-6 ) ? 1 : 0; // compare with epsilon value }In this logic the epsilon value is 1E-6 or 0.000001. When comparing with 2.0 it will return 1 if the value is between 1.999999 and 2.000001 and zero if it isn't. This not an particularly general purpose solution because it will give bad results for very large and very small numbers but for values of the magnitude of yours it should be suitable.