0

I have the following Matlab function:

function in_out = in_or_out(x)
    sum = 0;
    for i = 1 : length(x)
        sum = sum + (x(i) - 1/2).^2;
    end 

    if sum <= 1/4
        in_out = 1;
    else
        in_out = 0;
    end
end

If I pass as input [.8, .1], sum = 0.250000000000000 after the for loop terminates. However, the if sum <= 1/4 check fails and 0 is returned (i.e. according to Matlab 0.250000000000000 is not <= 1/4). Why is this happening?

Adam
  • 8,752
  • 12
  • 54
  • 96

1 Answers1

1

Try this:

tol = 0.000001
if ((sum<(1/4)) || (sum-(1/4))<tol)
    in_out = 1
else
    in_out = 0
end