In trying to optimize my code, I was working on this:
cls() a=254 c=0 b=band(a,2^c) print(b) |
Results are: 0, which is correct. There are no #1 (zero) bits in the number 254.
Now try this:
cls() a=254 c=0 b=sgn(band(a,2^c)) print(b) |
The result is: 1, which is INCORRECT. There are no #1 (zero) bits in the number 254.
With problems like these, it's no wonder we're constantly fighting code - as in this case - it's not my fault for this particular calculation coming up wrong.



What does sgn do?
Wait. Is this even a documented function? The only reference to it in the manual says that sgn(0) returns 1.



SGN() at least according to every BASIC I've known before behaves thus:
SGN(34) = 1
SGN(34.3749) = 1
SGN(-27) = -1
SGN(-27.3483) =-1
SGN(.000005) = 1
SGN(-.0001) = -1
SGN(0) = 0
It returns 3 possible values, -1 for a negative number, 0 for the number zero, and 1 for a positive number. Fractions and numbers with decimals are never rounded up or down.
You MUST have (0) zero, in order to return zero. SGN is convenient for returning "switch" values like 0 or 1.
I saw SGN light up as a command in PICO-8 so I expected it to be valid like other BASICs. I guess not.
[Please log in to post a comment]