Day 003: SGN
#7
(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() Big Grin

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. Tongue

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.  Smile
Reply


Messages In This Thread
Day 003: SGN - by SMcNeill - 11-08-2022, 12:34 PM
RE: Day 003: SGN - by Pete - 11-08-2022, 01:39 PM
RE: Day 003: SGN - by SMcNeill - 11-08-2022, 02:29 PM
RE: Day 003: SGN - by Jack - 11-08-2022, 02:28 PM
RE: Day 003: SGN - by Pete - 11-08-2022, 03:13 PM
RE: Day 003: SGN - by SMcNeill - 11-08-2022, 03:50 PM
RE: Day 003: SGN - by Jack - 11-08-2022, 03:43 PM
RE: Day 003: SGN - by Pete - 11-08-2022, 04:11 PM
RE: Day 003: SGN - by SMcNeill - 11-08-2022, 05:47 PM
RE: Day 003: SGN - by Pete - 11-08-2022, 05:57 PM
RE: Day 003: SGN - by mnrvovrfc - 11-08-2022, 04:51 PM
RE: Day 003: SGN - by bplus - 11-08-2022, 08:20 PM
RE: Day 003: SGN - by Pete - 11-08-2022, 08:28 PM
RE: Day 003: SGN - by bplus - 11-08-2022, 09:37 PM
RE: Day 003: SGN - by Pete - 11-08-2022, 09:43 PM
RE: Day 003: SGN - by bplus - 11-08-2022, 09:53 PM
RE: Day 003: SGN - by Pete - 11-08-2022, 10:23 PM
RE: Day 003: SGN - by OldMoses - 11-09-2022, 07:18 AM
RE: Day 003: SGN - by Pete - 11-09-2022, 08:46 AM



Users browsing this thread: 10 Guest(s)