Log In  

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.

P#30926 2016-10-14 11:56 ( Edited 2016-10-14 17:56)

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.

P#30930 2016-10-14 12:47 ( Edited 2016-10-14 17:06)

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.

P#30937 2016-10-14 13:56 ( Edited 2016-10-14 17:56)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-03-28 11:16:43 | 0.006s | Q:11