MATLAB: About using ode45 with two events

ode45

I have the following event function for ode45. I would like the event to stop when it reaches line 7 as I set isTerminal = [1,1], however for some reason it continues computing the solution. I can't understand what I'm doing wrong. Could someone help me understand what is wrong?
function [value, isTerminal, direction] = myEvent(t, z)
xObst = 100;
dist = xObst - z(1);
if dist == 0
value = [0; z(3)];
isTerminal = [1; 1];
direction = [0; -1];
else
value = [dist; z(3)];
isTerminal = [0; 1];
direction = [-1 ; -1];
end
end

Best Answer

I found what the problem was. It was in the line:
if dist==0
Should have defined a tolerance and then made the floating point comparison like so:
tol = 1e-4
if dist <= tol