Page History: Constrain() function
Compare Page Revisions
Page Revision: 2018/09/26 13:34
Function to ensure a value exists within specific boundaries.
The following is a function for returning a numerical value constrained to a certain range. If the value is below the lower limit, the lower limit will be returned, likewise if the value is greater than the upper limit, the upper limit will be return. If teh value resides between the two, it will be returned intact. A similar function exists in other langauges but as shown below, it is easily fabricated using the inbuilt primitives of MMBasic.
As written below, it only works with integers. If your needs require it, adjust as necessary for Floats etc.
Syntax:=Constrain(TestValue,LowerLimit,UpperLimit)
Example:x=Constrain(TriggerTemp,7,10)
Function Constrain(v As Integer,Llim As Integer, Ulim As Integer) As Integer
V=Max(Min(v,Ulim ),Llim )
End Function