I have an array of "x" values (a grid for a PDE solver) that when I pass to a function that fills another array based on these x-values, a particular expression involving one x-value does not evaluate properly. The x-values range is -1:1 at an increment of 0.0125, and at x = -0.5, and at x = 0.5, I need to handle these cases differently than the other values. However the block below fails to evaluate to TRUE for the point x = 0.5 (it's ok for x = -0.5). Here's a stripped down snippet of the problem block, with details to follow:
int N = 160;
double delta_x = 0.0125;
const double lims = 0.5 * delta_x;
for(int i = 0; i <= N; i++)
{
if((x[i] < -0.5) || (x[i] > 0.5)) sol[i] = 0;
else if( (abs(x[i] + 0.5) < lims) || (abs(x[i] - 0.5) < lims) ) sol[i] = 0.5;
else sol[i] = 1;
cout << setprecision(30) << "lims: " << lims << ", abs(x[i] - 0.5): " << abs(x[i] - 0.5) << endl;
cout << "sol[" << i << "]: " << sol[i] << endl;
}
Here is the output for x = 0.5:
lims: 0.00625000000000000034694469519536, abs(x[i] - 0.5): 1.11022302462515654042363166809e-16
sol[120]: 0
So, it's looks like the expression in the if-statement should return TRUE when x = 0.5, even though it's not precisely 0.5 of course, since it is within the "lims" of the range. Any thoughts??