News:

;) This forum is the property of Proton software developers

Main Menu

Between maths operator / using a procedure inline

Started by RGV250, Jul 11, 2024, 07:00 PM

Previous topic - Next topic

RGV250

I was doing a bit of code and where I had things like "if Test >= 50 and Test <= 100 then" I thought a between function would be a nice addition.
I wondered if I could create a procedure and put it inline so it makes sense, something like "if Test Between(50,100) then"

Bob

 

diebobo

Symbol TRUE  = 1
Symbol FALSE = 0

Dim Number_Arg As Word

Number_Arg = 4444

If  Between_Nrs ( Number_Arg, 200, 4000) = TRUE Then PORTA.1 = TRUE

Proc Between_Nrs ( Number_To_Check As Word, Number_Low_Limit As Word, Number_High_Limit As Word), Byte
    Result = FALSE
    If Number_To_Check >= Number_Low_Limit And Number_To_Check <= Number_High_Limit Then Result = TRUE
EndProc


Try this code,just written fast, does compile and think should work.. Not Programmed to PIC.

top204

#2
You can also use the Select-EndSelect for this, if checking a single variable name:

    Select MyVar
        Case 50 To 100
            ' Perform the code here if MyVar is between 50 and 100
    EndSelect

It also produces more efficient assembler code because it does not have to look for other operators, so it does not bring the boolean comparison stack into play.

Stephen Moss

In regard to the @diebobo answer it has been a while since I wrote any procedures but I believe the Result type has to be specified in the calling code and I am not sure bit (required for a Boolean response) is allowed, you will have to try it and so I think
If  Between_Nrs ( Number_Arg, 200, 4000) = TRUE Then PORTA.1 = TRUE should be If  Between_Nrs ( Number_Arg, 200, 4000), Bit = TRUE Then PORTA.1 = TRUE

Another way would be
Select Test
 Case 50 To 100
    Do something here if Test is within the specified range
  Case Else
    Do something here if Test is outside the specified ranges (optional)
End Select

This is particularly handy of you want to test a variable for ranges of possible values, i.e.
Select Test
 Case 0 To 49
    Do something here if Test is within the specified range
 Case 50 To 100
    Do something here if Test is within the specified range
 Case 101 To 134
    Do something here if Test is within the specified range
  Case Else
    Do something here if Test is outside the specified ranges (optional)
End Select


diebobo

Stephen, i've lost my trust in bit operations and use in bit declared variables a longggggggggg time ( no rant to Les ;) ).. I've spend days and days find problems which originated in mallfunctioning bit using code. That's why i used byte as the result. I think i've used it for many years now this way, no probs.. Or i am reading you suggestion wrongly.