Taking control of display decimals

Top  Previous  Next

A potential frustration with GAMS display output involves numerical formatting.  Consider the example dispnum.gms.  There I define the table DATA with rather disparate numbers.  A resultant display of DATA yields

 

----      8 PARAMETER DATA

         index21     index22

index11 1.000000E-5 1.000000E+7

index12       3.720     200.100

 

In that display GAMS mixes together numbers in exponential and normal format with the default being a print out of three decimal places.  If such a display is unsatisfactory, there are several ways of altering its appearance.

One can alter the number of decimals using the item specific option  (dispnum.gms)

 

option data:1:1:1;

 

 which displays this item with one decimal place, yielding

 

 ----     10 PARAMETER DATA

                      index21     index22

           index11 1.000000E-5  10000000.0

           index12         3.7       200.1

 

 Here, GAMS overrides the decimals choice to insure the small number does not become zero, but I don't get the exponential display for the large number as it fits.

One can alter the number of decimals using a global option statement to change the default decimals in all subsequent displays not subject to the specific option command just discussed.  For example, the following alters the default display and all subsequent displays to 2 decimal places but would not alter the display of the item called data as it already was subject to an item specific display formatting option command. (dispnum.gms)

 

 option decimals=2;

 

----     13 PARAMETER data2

 

            index21     index22

 

index11 1.000000E-5 10000000.00

index12        3.72      200.10

 

 Note, GAMS still overrides the decimals choice to insure the small number does not become zero.

One can suppress small numbers in the display manually.  For example, using (dispnum.gms)

 

 data2(index1,index2)$(data2(index1,index2) lt 0.01)=0;

 

 sets all numbers to zero which are less than 0.01, yielding

 

 ----     14 PARAMETER DATA2

                    index21     index22

 index11       _____    10000000.0

 index12         3.7       200.1

 

 Note one needs to employ absolute value if negative numbers are present i.e. using a command like:

 

 data2(index1,index2)

 $(abs(data2(index1,index2) lt 0.01))=0;

 

Users may wish to cap the value of large numbers.  I can cause the output to have the entry infinity for anything greater than the number 10,000 using (dispnum.gms)

 

 data2(index1,index2)

         $(data(index1,index2) gt  10000)=inf;

 

 yielding

 

 ----     18 PARAMETER DATA2

                 index21     index22

 index11 1.000000E-5        +INF

 index12       3.720     200.100

 

Users may wish to round numbers using syntax like (dispnum.gms)

 

 data2(index1,index2)=round(data(index1,index2),0);

 

Users may desire a report of percentage changes.  These first need to be calculated in a manner such as

 

 data4(index1,index2)$data2(index1,index2)=

   100*(data3(index1,index2)/data2(index1,index2)-1);

 data4(index1,index2)

         $(abs(data4(index1,index2)) lt 0.1)=0;

 data4(index1,index2)$(data2(index1,index2) eq 0)= na;

 

 Here I set small percentage changes to zero but are careful to use absolute values so negative changes are not zeroed out.  I also report numbers that would report percentage changes from a base of zero with the coding "na".  The result is (dispnum.gms)

 

 ----     29 PARAMETER DATA4

                 index21     index22

 index11          NA

 index12        80.6         1.5