Numerical Value

Top  Previous  Next

One can test the numerical value of a control variable by using several syntax forms below

 

$Ife %controlvariablename% op valuetocompare     statementtoexecute;

$Ife %controlvariablename%  statementtoexecute;

 

Notes:

$IFe  or $IFTHENe must be used to do numerical comparisons
The %controlvariablename% retrieves the value that was placed in the control variable.  The % precedes and succeeds the control variable name.  .
The valuetocompare is a numerical value to compare with and can be another control variable name enclosed in % signs
The op is a symbol saying compare these two values.and is

                 =        to test equality

                                 ==        to test equality with a tolerance

                 >        to test greater than

                 <        to test less than

                 <>        to test not equal

When == is used the test is when testing whether item1 == item2 the test is true if abs (item2-item1)/ (1+abs(item2)) < 10^(-12)
When just the control variable name is used then the test is whether or not it holds a non zero value

 

Examples:

(setcontrol.gms)

$set znumber 4

       $Evalglobal anumber %znumber%+10

       $Evalglobal xnumber %znumber%+9

         $ife %anumber%>14 display "it exceeds 14", "%anumber%";

 $ife %anumber% == %anumber2% Display "they are close" ;

         $ife %anumber%<14 display "it is less than 14", "%anumber%"

         $ife %anumber%=14 display "it equals 14", "%anumber%"

         $ife %anumber%<>14 display "it does not equal 14", "%anumber%"

         $ife %anumber%>%xnumber% display "it exceeds xnumber","%anumber%","%xnumber%"

         $ife %anumber% display "anumber is nonzero again";

 

 

generates code that after compilation and looks like the following

 

39  *texttocompare

41  type1=ok;

42  type2=ok;

43  type3=ok;

44  type4=ok;

45

46  *add spaces

47  type1=ok;

 

This code shows that the quoted or uncontrolled quoted control variable name and target text works when spaces are not present.  If spaces are present then the text retrieved by use of %controlvariablename% must be encased in quotes and if the comparison is to work so must be the target text.

 

Often one may wish to see if the text for an item is blank.  This is frequently done with commands like the following

 

$If       "%controlvariablename%a"  == "a"  display "blank it is";

$If not "%controlvariablename%a" == "a" display "it is not blank";

 

which contain the element "%controlvariablename%a" that reduces just to "a" if the named control variable does not have a text entry.

 

Examples employing the environmental variables appear in environvar.gms.