? Limit Variable to Range ● SmileBASIC Source

Sign In

Register
*Usernames are case-sensitive
Forgot my password

Limit Variable to Range

Code like:IF X>maximum THEN X=maximum IF X<minimum THEN X=minimum Can be written as:

<variable>=MIN(MAX(<variable>, minimum), maximum)

Example: X=MIN(MAX(X,0),400) Y=MIN(MAX(Y,0),240) Limits X and Y to the screen limits. Use this instead of: IF X<0 THEN X=0 IF X>399 THEN X=399 IF Y<0 THEN Y=0 IF Y>239 THEN Y=239 I mean you should probably just have checks when you modify your variables
Author
12Me21
Updated
Rating
4 votes
Categories
Keywords
8 Comment(s) auceps auceps ddr Sam Sam Amazing Page Hidden Achievements Great Page Hidden Achievements Good Page Hidden Achievements This seemed interesting, but according to this test I made, your method seems much slower... I%=0 D=MILLISEC FOR I%=1 TO 1000000 X=MIN(MAX(X,0),400) Y=MIN(MAX(Y,0),240) NEXT ?MILLISEC-D;"ms" I%=0 D=MILLISEC FOR I%=1 TO 1000000 IF X<0 THEN X=0 IF X>399 THEN X=399 IF Y<0 THEN Y=0 IF Y>239 THEN Y=239 NEXT ?MILLISEC-D;"ms" On my New 3DS, using this code, the MIN(MAX( part takes about 2520ms, while the regular IF checks take about 2160ms. Did I screw up, or is it really slower? Yttria Yttria Head Admin 4 simple IFs that don't run are obviously faster than 4 function calls and two assignments that always run, but 0.00036ms per iteration is negligible. The snippet is useful for being short and elegant. To speed it up for the average case (which your test doesn't consider) it could be surrounded by a guarding IF...ENDIF. 12Me21 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself In your test, none of the IFs actually run because X and Y are always 0. 2 argument MIN/MAX is actually very fast (for a function call), because it's basically just implemented as A>B ? B : A You can also write things like: X=MIN(MAX(X+100,0),1000) vs X=X+100:IF X<0 THEN X=0 ELSEIF X>1000 THEN X=1000 Gaelstrom_Valence Gaelstrom_Valence Drawing I like to draw! Hobbies Intermediate Programmer I can make programs, but I still have trouble here and there. Programming Strength First Month Joined in the very first month of SmileBASIC Source Website Ohhh my goodness I needed this so much in my life, thank you 12Me21 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself I thought I was so clever when I "discovered" this, but it turns out that it's pretty well known in other languages too... randomous randomous Robot Hidden Easter Eggs Second Year My account is over 2 years old Website Drawing I like to draw! Hobbies You can't use italics in a [code] segment. 12Me21 12Me21 Syntax Highlighter Received for creating the code syntax highlighter on SBS Night Person I like the quiet night and sleep late. Express Yourself Fixed.