11-08-2022, 02:29 PM
(11-08-2022, 01:39 PM)Pete Wrote: My proposed use for SGN is a 3-step process: 1) Wrap it up in a brown paper bag. 2) Place it on the porch of QB64 Official. 3) Light it on fire.
Honestly this is one of the few BASIC keywords that require you to type slightly more code instead of less...
Code: (Select All)FOR i = -5 TO 5
PRINT i;
IF SGN(i) < 0 THEN PRINT "neg", ELSE PRINT "pos",
IF i < 0 THEN PRINT "neg" ELSE PRINT "pos"
NEXT
PRINT: PRINT SGN(-2 + -6 + 8 + 22 - 33 + -77), -2 + -6 + 8 + 22 - 33 + -77 < 0
PRINT: PRINT SGN(ABS(-2 + -6 + 8 + 22 - 33 + -77)), ABS(-2 + -6 + 8 + 22 - 33 + -77) < 0
Note the only difference is a numerical one, involving the numbers in parentheses. SGN returns -1 for true and 1 for false, where "<" returns zero for false. I really can't see how even that is useful. Special consideration isn't even given to zero. SGN returns zero as positive, so I see no advantage there, either.
Oh well, "Random Generator" is just in its infancy, but I'm pretty sure given the way things are going, it is destined to grow up and be a Magic 8-Ball.
Pete
Like all commands, @Pete, it's all in how you make use of them.
SGN can be used very quickly to determine direction of objects.
DirectionX = SGN(Position1X - Position2X)
If Position1 is 10, Position2 is 15, 10 - 15 = -5.. You're moving in a negative direction on your screen.
If Position1 is 15, Position2 is 10, 15 - 10 = 5... You're moving in a positive direction on your screen.
If Position1 = 10, Position2 = 10, 10 - 10 = 0... You're not moving on your screen.
CAN you do the same thing with IF statements?
SURE -- but it's going to take a lot longer to process.
IF Position1 < Position2 THEN
direction is positive
ELSEIF Position1 > Position2 THEN
direction is positive
ELSE
direction is zero
END IF
Up to 3 comparison checks to do what one simple SGN command can do for you... Which do you think is going to be faster and keep your program flowing the best? Particularly when youre dealing with 100 bullets and which direction they're traveling across the screen on your meteoroid clone?
Another quick and easy use -- let's say you're making a ledger to help track your finances. You want expenditures to be in Red, deposits to be in Black, and zero can just be Gray
DIM Kolors(-1 TO 1) AS _UNSIGNED LONG
Kolors(-1) = Red
Kolors(0) = Gray
Kolors(1) = Black
Then all you need to do when you print onto the screen is a simple:
COLOR Kolors(SGN(number))
PRINT number
No Ifs. No decisions needing to be made. Just a quick call to a math function and it's done.