Have just found myself baffled by this seemingly innocuous code in Seastalker, which seems at though it ought to do the opposite of what it actually does. Clearly I am missing something:
<ROUTINE STARBOARD-OF-THORPE? (X Y)
<COND (<NOT <G? 0 <- <* ,THORPE-HLAT <- .X ,THORPE-LON>>
<* ,THORPE-HLON <- .Y ,THORPE-LAT>>>>>
<RTRUE>)
(T <RFALSE>)>>
For context, THORPE-HLAT
and THORPE-HLON
are integers from -1 to 1, which describe which direction Thorpe’s submarine is facing. South and west are -1, and north and east are 1, in latitude and longitude respectively, with 0 meaning the submarine is perpendicular to that particular axis. THORPE-LON
and THORPE-LAT
are what they look like, and this routine would usually be called with the player’s own longitude and latitude as arguments.
Now, I observe:
-
The expression
<- .X ,THORPE-LON>
is positive if the player is to the east of Thorpe and negative if they are to the west. -
The expression
<- .Y ,THORPE-LAT>
is positive if the player is to the north of Thorpe and negative if they are to the south. -
The expression
<* ,THORPE-HLAT <...>>
reverses the sign of the included expression if the submarine is facing south, and similarly the expression<* ,THORPE-HLON <...>>
reverses the sign if the submarine is facing west. -
However, the expression
<* ,THORPE-HLON <...>>
is the subtrahend in a subtraction operation, and we can think of this as one more sign reversal. If Thorpe is facing northwest (which he is at the start) and we are to the northeast of him, the core operation<- <* ,THORPE-HLAT <...>> <* ,THORPE-HLON <...>>>
is effectively the addition of two positive numbers. -
If Thorpe is facing northwest and we are to the northeast of him, we are to starboard of Thorpe. If we were to the southwest of him, we would be to port of Thorpe.
-
As we have seen, the central operation necessarily produces a positive number if Thorpe is facing northwest and we are northeast of him. If Thorpe is facing northwest and we are southwest of him, it would necessarily produce a negative number.
-
This routine returns false if the central operation produces a positive number. It returns true if the central operation produces a negative number. (I don’t care about the zero case at the moment.) Therefore this routine must return false if we are to starboard of Thorpe and true if we are to port of Thorpe.
-
Except the above is wrong, because if you play the compiled game it is easy to verify that the routine returns true if you are to starboard of Thorpe and false if you are to port of him, as the name of the routine would suggest.
I must be misunderstanding something, but I genuinely can’t tell what.