Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Day 003: SGN
#11
(11-08-2022, 05:47 PM)SMcNeill Wrote:
(11-08-2022, 04:11 PM)Pete Wrote: Nice. Without it, I'd probably just code:

Code: (Select All)
INPUT CarX.position, CarY.position
speedx = 1
IF CarX.position - CarY.position THEN CarX.position = CarX.position + (CarX.position - CarY.position) / ABS(CarX.position - CarY.position) * speedx
PRINT CarX.position
SLEEP
CLS
RUN

Aye, you can always code around notusing it.  Most commands are like that.  Don't like _PRINTSTRING?  Use LOCATE, PRINT, CSRLIN, and POS to duplicate its behavior.

One thing you have to admit though, comparing our two codes -- yours has a lot more to process going on under the hood.  In a game, where FPS matters, that might might a world of difference if inside some inner loop.

Agreed. I do like having keywords to handle things but I also like figuring out my own "functions" to make things I want happen. You have to admit the way I used that IF/THEN to avoid a division by zero error, in the code above, was PFC.

Pete

Pretty fine coding... Sure, let's go with that.
Reply
#12
I had some rounding code fixed for negative numbers with Sgn() in SmallBASIC board at Syntax Bomb this is QB64 version:
Code: (Select All)
$Console:Only
For i = 1 To 100
    r = Rnd * 200 - 100
    Print r, roundDP$(r, 0), roundDP$(r, 1), roundDP$(r, 2), roundDP$(r, 3)
Next

Function roundDP$ (num, digits) ' fixed with sgn 2022-11-08
    Dim s$, dot
    s$ = _Trim$(Str$(num + (Sgn(num) * .5) * 10 ^ -digits))
    dot = InStr(s$, ".")
    If dot Then roundDP$ = Mid$(s$, 1, dot + digits) Else roundDP$ = s$
End Function

Should be OK if stay out of exponential notation.
b = b + ...
Reply
#13
That's a good one, too.

It would otherwise take...

Code: (Select All)
FUNCTION roundDP$ (num, digits) ' fixed with sgn 2022-11-08
    DIM s$, dot
    SELECT CASE num
        CASE IS < 0
            s$ = _TRIM$(STR$(num - .5 * 10 ^ -digits))
        CASE 0
            s$ = _TRIM$(STR$(num * 10 ^ -digits))
        CASE IS > 0
            s$ = _TRIM$(STR$(num + .5 * 10 ^ -digits))
    END SELECT
    dot = INSTR(s$, ".")
    IF dot THEN roundDP$ = MID$(s$, 1, dot + digits) ELSE roundDP$ = s$
END FUNCTION

Pete
Reply
#14
@Pete check case > 0 in otherwise
b = b + ...
Reply
#15
Feeling better but still missing stuff. Noticed it immediately. Forgot to change sign after paste. Edited.

Thanks,

Pete
Reply
#16
Yeah bus stations will do that ;-))
b = b + ...
Reply
#17
I'd say my wife has a frying pan with my name on it... but hey, we're pretty modern folks. The one she uses has facial recognition.

Pete
Reply
#18
I've found SGN to frequently be quite useful. One of my better success stories in the use of SGN was this little snippet from a role playing character generator.

Code: (Select All)
FUNCTION Bonus% (mode AS INTEGER, stat AS INTEGER, PCin AS INTEGER)
    ' Returns the multiplier for characteristic based attributes.
    ' as per the following table.
    '            Characteristic Value {char(stat, PCin).value}
    '           1-4     5-8     9-12    13-16   17-20   each +4
    'Mode
    'Pri [ 1]   -2      -1       0        1       2       +1
    'Pri-[-1]    2       1       0       -1      -2       -1
    'Sec [ 2]   -1       0       0        0       1       +1
    'Sec-[-2]    1       0       0        0      -1       -1
    ' Use straight for hit points and multiply by 5 for skill category modifiers.
    ' mode: 1= primary  2= secondary  -1= negative primary  -2= negative secondary
    ' stat passes the index number of the characteristic in question
    ' PCin passes the PC index usually ActPC but also a loop control from
    ' Delete_PC removal routine
    v% = (_CEIL(char(stat, PCin).value / 4) - 3) * SGN(mode)
    Bonus% = v% - (ABS(mode) - 1) * SGN(v%)
END FUNCTION 'Bonus%

While it doesn't look like much to a casual observer, those two equations essentially replaced 16 lines of SELECT CASE block. Oh yes, and _CEIL was new to me when this was written too.
DO: LOOP: DO: LOOP
sha_na_na_na_na_na_na_na_na_na:
Reply
#19
And stirring the pot producing yet another beauty!

+ 1

Pete
Reply




Users browsing this thread: 1 Guest(s)