Anyway, in the script you'll see that test and test2 agree on red bars, but on green bars test gets a much lower value, which was pretty shocking to me until I traced down this particular cause within my larger script.
In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.
Edit referring to this one: https://www.tradingview.com/pine-script-reference/v4/#fun_barssince , specifically the description:
Number of bars since condition was true.
Perhaps rewrite to say something like it returns the number of bars since condition was true, ignoring those bars for which the function was not in context to execute.