use min/max with inc or dec

jims

Senior Member
I find that it would be useful for me to be able to use min or max with the inc and dec commands. Any possibility to allow this?? Thank you, JimS
example:'
Code:
[color=Blue]dec [/color][color=Purple]b0 [/color][color=DarkCyan]min [/color][color=Navy]45       [/color][color=Green]'Fails syntex check.
[/color][color=Blue]let [/color][color=Purple]b0[/color][color=DarkCyan]=[/color][color=Purple]b0 [/color][color=DarkCyan]-[/color][color=Navy]1 [/color][color=DarkCyan]min [/color][color=Navy]45 [/color][color=Green]'Passes syntex check.[/color]
 

hippy

Technical Support
Staff member
I doubt it would be added as it's not something one couldn't do with a macro, for example ...

Code:
#Macro DecMin( var, minVal )
  var = var - 1 Min minVal
#EndMacro

DecMin( b0, 45 )
Though you might actually want that as follows so it works if b0 is zero to start with -

Code:
#Macro DecMin( var, minVal )
  var = minVal + 1 Min var - 1
#EndMacro
 

inglewoodpete

Senior Member
I find that it would be useful for me to be able to use min or max with the inc and dec commands. Any possibility to allow this?? Thank you, JimS
example:'
Code:
[color=Blue]dec [/color][color=Purple]b0 [/color][color=DarkCyan]min [/color][color=Navy]45       [/color][color=Green]'Fails syntex check.
[/color][color=Blue]let [/color][color=Purple]b0[/color][color=DarkCyan]=[/color][color=Purple]b0 [/color][color=DarkCyan]-[/color][color=Navy]1 [/color][color=DarkCyan]min [/color][color=Navy]45 [/color][color=Green]'Passes syntex check.[/color]
I recall that sometime in the dim and dark past, Technical said that Inc and Dec are pseudo commands that simply do "x = x + 1" and "x = x - 1". So typing the longhand versions take no additional code space: they just don't look quite as tidy as "Inc x" or "Dec x".
 

westaust55

Moderator
I recall that sometime in the dim and dark past, Technical said that Inc and Dec are pseudo commands that simply do "x = x + 1" and "x = x - 1". So typing the longhand versions take no additional code space: they just don't look quite as tidy as "Inc x" or "Dec x".
That is also my understanding.
 

Flenser

Senior Member
One option would be to create #DEFINEs for inc & dec

This code passes the syntax check:
Code:
#define dec(var) let var = var - 1
dec(b1) max 50
 

techElder

Well-known member
Flenser, while your variation is syntactically correct, you lose the generality of a macro like the one Hippy suggests in post #2.

Your version is hard coded with the number "50" while in Hippy's version the "50" (or some other limit) would be transferred to the code each time the macro is utilized. Your version is just a little different than the regular PICAXE "dec" statement.

This is ideally what the PICAXE macro is all about.
 

Flenser

Senior Member
Texasclodhopper, you have misread my code.

The first line is where I delcare a #DEFINE named dec():
Code:
#define dec(var) let var = var - 1
The second line is only example code to demonstrate how to decrement the variable b1 using the #DEFINE on first line. The value 50 is not a part of the #DEFINE, it is the parameter to the MAX command exactly the same as the value of 45 is the paramter for the MIN command in jims original example.

Perhaps it would have been clearer if I had demonstrated my #DEFINE using the same "MIN 45" command that jims used in his original post:
Code:
; Example code to decrement variable b0 using the #DEFINE above:
dec(b0) min 45     'Passes syntex check.

; jims original example of code that fails the syntax check
dec b0 min 45       'Fails syntex check.
 

techElder

Well-known member
Thanks, Peter, but again, I didn't misread anything. The "beast" is in the solution. I didn't say Flesner's post wasn't a notable option, but using text replacement on a 3-5 byte line of code just to get past the editor's format check seems rather silly when a better/improved solution is at hand with a macro.

Anyway, the unarguable result was presented by Hippy in a "faster than a speeding bullet" post. Happy New Year! :D
 

hippy

Technical Support
Staff member
Anyway, the unarguable result was presented by Hippy in a "faster than a speeding bullet" post.
Though having had time to reflect on that; a better solution may be -

Code:
#Macro DecMin( var, minVal )
  var = var Min 1 - 1 Min minVal
#EndMacro
Happy New Year.
 
Top