 # Strange subtracting for Sound time

Hello, I’m creating a Stop Signal Task which I present an arrow and a sound. The sound will be played with 0.25s delay after the arrow was presented. I wrote a code to check if the subject pressed any keys during Stop frames, the delay time will be decreased by 0.05s and when the delay was reached to 0.05s, there is no subtraction. The code works nicely except ending frame:

0.25
0.2
0.15000000000000002
0.10000000000000002
0.05000000000000002
1.3877787807814457e-17 Wrong One

You see I have 6 frames and in which frames the code decrease the delay seconds but in the last frame shows this number : 1.3877787807814457e-17

This is my code in Begin Exp :

``````startTime = 0.25
``````

This is my code in End Routine :

``````if stimType == 0 :
if str(respKeyboard.keys) != 'None' :
if startTime == 0.05 :
startTime = 0.05
else :
startTime -= 0.05
elif str(respKeyboard.keys) == 'None' :
startTime += 0.05
else :
pass
else :
pass
``````

Any help will be appreciated. You’re being bitten by the floating point arithmetic (see e.g. https://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-inaccurate). `0.05000000000000002 != 0.05`, so `if startTime == 0.05` will be false.

There’s a `numpy.isclose(...)` function that you could use in place of `==`, or you can use `math.isclose(...)` in Python 3.5+.

You could also replace

``````if startTime == 0.05:
startTime = 0.05
else:
startTime -= 0.05
``````

with

``````startTime -= 0.05
startTime = max(startTime, 0.05)
``````
1 Like

@aforren1 Thanks, great help. Can please explain more about `max` function? What does it do? :

``````startTime = max(startTime, 0.05)
``````

Returns which of the arguments is the higher value. max(2,1) returns 2, for example. So, it will return either startTime if startTime is greater than .05, or it will return .05.

1 Like

@jonathan.kominsky Great help.