$Ife conditionals

Top  Previous  Next

This tests a numerical expression at compile time using values in control variables.

 

The format is

 $Ife %controlvariable%op item command

or

 $Ife %controlvariable% command

where

         %controlvariable% is the name of the control variable to test

         op is

                 = to test equality

                                 == to test equality with a tolerance

                 > to test greater than

                 < to test less than

                 <> to test not equal

         item is the item to compare with which is either

                 another control variable arising from an eval, valglobaol or evallocal

                 a constant

         command is a statement to execute in the GAMS program if the condition is true

Example: (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%>%vnumber% display "it exceeds vnumber","%anumber%","%vnumber%"

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

Notes:

Unfortunately GAMS allows one to define scoped local and global variables with the same name but treats them as different under some cases and prioritizes them when using $ife or $if as discussed here.
When testing whether item1 == item2 the test is true if abs (item2-item1)/ (1+abs(item2)) < 10^(-12)
When the command is used without an operator as in

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

  then it is same as

                 $ife %anumber% <> 0 display "anumber is nonzero";

                 i.e. the program tests whether an item is nonzero.

The evaluation of expressions follows the rules given under the discussion of $Eval