Post by uncledave on Feb 23, 2022 17:26:44 GMT
I have noticed that a StreamByter loop with 128 steps (0 to 127, for example) has a surprising effect on an If/Else inside the loop. When the If condition is true, the true branch is executed on every step, as expected. But on the last step, the false branch is also executed! When the If condition is false, only the false branch is executed on every step. The following script exhibits this behavior.
I used a random note on message to trigger the function. The output is attached. The Log messages in the attached show that both branches were taken on the last iteration, meaning that an incorrect result value was stored. This doesn't happen for shorter loops; it appears to be triggered by the artificial SB loop limit of 128 iterations. The easy workaround (once you know) is to test the alternate condition, instead of using Else.
I noticed this while creating a velocity map table, using one of two functions selected by GUI. Just like in this case, for input 127, one function returned 64, the other 127, and I could not understand why the last value jumped to the maximum. After quite a lot of debugging, I realized that my second function was also being run on the last iteration. nic??
#LoopSubTest
If load
Alias J00 theFlag
Ass J00 = 1
# loop is limited to 128 iterations, so cannot exceed 127 here
Alias $127 maxValue
Alias $123 printLimit
Sub FillTable
Ass I00 = 0
While I00 <= maxValue
If theFlag == 1
Mat I01 = I00 / 2
If I00 > printLimit
Log True_Input I00 +D
Log True_Result I01 +D
End
Else
# It seems like both branches of the if are taken when I00=127.
# Is this because SB forces this to be the last iteration?
# Replacing the Else with a second If test works correctly.
Ass I01 = I00
If I00 > printLimit
Log False_Input I00 +D
Log False_Result I01 +D
End
End # if theFlag
If I00 > printLimit
Log Storing I01 +D
End
Ass LI00 = I01
Mat I00 = I00 + 1
End # while
End
End
If MT == 90 # any note calls the function
FillTable
Block
End
If MT == 80
Block
End
I used a random note on message to trigger the function. The output is attached. The Log messages in the attached show that both branches were taken on the last iteration, meaning that an incorrect result value was stored. This doesn't happen for shorter loops; it appears to be triggered by the artificial SB loop limit of 128 iterations. The easy workaround (once you know) is to test the alternate condition, instead of using Else.
I noticed this while creating a velocity map table, using one of two functions selected by GUI. Just like in this case, for input 127, one function returned 64, the other 127, and I could not understand why the last value jumped to the maximum. After quite a lot of debugging, I realized that my second function was also being run on the last iteration. nic??