This could be easier to predict in cases where one of `left` and `right`
is significantly greater than the other, and as such the same branch is
taken multiple times in a row
This commit includes two fixes:
- a temporary cast to make the function compile
- a fix to a logic error that caused the function to hang or return
incorrect results