11-08-2022, 03:50 PM
(11-08-2022, 03:13 PM)Pete Wrote: @Jack
You're right. I didn't check SGN(0) in the code I posted for zero. Oops, my bad. So it does 3 things, but...
Hurry Steve, stomp out that flaming bag of SGN()
But wait a sec... Instead of...
Code: (Select All)Position1X = 10: Position2X = 15
DirectionX = SGN(Position1X - Position2X)
IF DirectionX =-1 THEN PRINT "NEG" ELSE IF DirectionX THEN PRINT "POS" ELSE PRINT "ZERO"
I'd do...
Code: (Select All)Position1X = 10: Position2X = 15
DirectionX = Position1X - Position2X
IF DirectionX < 0 THEN PRINT "NEG" ELSE IF DirectionX THEN PRINT "POS" ELSE PRINT "ZERO"
A bit less code that way, or a slight bit more to use SELECT CASE for evaluation as:
SELECT CASE. 1, 0 , -1 vs IS > 0 , 0, IS < 0. Not much of a difference.
It is good as a reference point in your code, to see where you need such comparisons on a search. Jut type search SGN.
I vaguely remember using this statement maybe once decades ago but can't recall what for.
Now Steve's second example is a good one, as you'd get bit in the ASCII doing it this way...
COLOR Kolors((number / ABS(number)))
Division by zero is a killer.
So it is good for at least that nice example. Kudos Steve + 1. Now wipe off your shoes before coming back to the forum!
Pete
See, @Pete, you're once again doing it wrong.
If you just wanted to print your direction, then you'd basically just do as I did with Kolors() above -- make an array of strings and then PRINT Direction(DirectionX). What such routines are generally used for are something more like the following:
Let's say you're making a car chase game. Your car is chasing the opponents car across the screen. Instead of a series of IF statements such as you've got drawn out, you'd probably go with something like:
CarX.position = CarX.position + SGN(CarX.position - CarY.position) * SpeedX
Now, CarX is going to change directions if it somehow passes by the other vehicle without crashing into it (they jumped and you went under them, or they hit the transparent boost, or whatever). No IF is needed, no decisions at work -- just a simple function call to determine SGN which, in this case, determines direction.
SGN isn't useless. You just need to know when and how to use it properly.