Answer to the original question
The expression in the if
statement did evaluate true. If it did not, then you would not have seen the output that you included in the question.
FWIW, a simpler test would be for
abs(abs(x[i]) - 0.5) < lims
Answer to the latest version of the question
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;
You state that x[i]
is close to 0.5, but does not set sol[i]
to 0.5 and in fact sets it to 0
. In which case the only sane conclusion is that x[i] > 0.5
and the first condition is is met:
if((x[i] < -0.5) || (x[i] > 0.5)) sol[i] = 0;
So you need to change the order of your tests:
if( (abs(x[i] + 0.5) < lims) || (abs(x[i] - 0.5) < lims) ) sol[i] = 0.5;
else if((x[i] < -0.5) || (x[i] > 0.5)) sol[i] = 0;
else sol[i] = 1;
And I would write it like this:
if (abs(abs(x[i]) - 0.5) < lims)
sol[i] = 0.5;
else if ((x[i] < -0.5) || (x[i] > 0.5))
sol[i] = 0;
else
sol[i] = 1;
Please in future be sure to include the right code in the question that you ask.