variables 

algebra: variables


creating

The values of local variables are always set by the program - you can not change their values nor their name.

In contrast, you can create and (in most cases) modify Global variables. There are many methods to create them:

Also some global variables are defined by the program:


naming

Global variable names must satisfy the following conventions:

Examples:

Global a_1 = 5
Global abc = `This is a string`
Global v=row(20)

Although permitted, it is not advisable to force variable names to contain lower case characters as in the following:

// Not recommended
Global "Abc" 123

substitution

The current value of a formula in terms of global variables is substituted in input statements if the formula is enclosed in curly brackets.

If the formula evaluates to a Number, Logical or String, then the value is simply substituted. If the result is an Histogram, then the string "Histogram" is substituted for the formula. For a 1-dimensional Matrix, the first portion of the matrix is substituted but for higher dimensional matrices, the string "n-Matrix" will appear, where n is the dimension of the matrix.

Curly brackets { } are very often misused, in particular they should never be used in Do loops, in the conditions of If_lines and If_blocks, in the arguments of procedure Calls, in Parse and Global statements.

The contents of { } is evaluated and substituted before the statement is interpreted, while you would probably expect evaluation to happen only when the control statements are executed. For an overview of the use of round, square and curly brackets.

Format:

any text {formula} more text

Example:

Global a=60
Say "Tangent of {a} degrees is {tan(pi*a/180)}."

The global variable A is assigned the value 60. In the following SAY statement, A is converted from degrees to radians and its tangent is displayed.

Further examples of this use of global variables can be found in the examples given for many other commands.

// What one should not do
Global n=5               For i From 1 To 5 Do
For i From 1 To {n} Do      For j From 1 To {i} Do
    ...                        Say "j={j}"
Enddo                       Enddo
                         Enddo

The example on the left would still work if the value of n doesn't change during execution, the example on the right would fail. A priori, I is not defined at the time the loop on J is read. If it is, then the loop on J would be executed a fixed number of times that has no relation with the value of I in the outer loop.


types

Garfield currently knows 6 data types, which differ from those found e.g. in Fortran and in C.

Garfield variables know their own type. Garfield takes care that a version of the operators and the functions appropriate for the given data type or combination of data types is called. The type of the argument propagates to the result for most operators and functions.

Global variables come in the following types:

Type Explanation Mode
Undefined Declared but not initialised 0
String Character strings 1
Number A real or integer number 2
Logical Can be either True or False 3
Histogram 1-Dimensional histogram 4
Matrix n-dimensional matrices made up of numbers 5

The mode is a numeric equivalent of the type, the mode is used only internally for quick reference.

Additional information on:


storage

Every Histogram, Logical, Matrix, Number and String, whether constant, variable or intermediate result, is accessed through an array called R.

The value of Numbers and Logicals is stored directly in R, while for Histograms, Matrices and Strings a reference number is kept in R.

Each element of R has type information attached to it.

All R's from R(0) downwards are assumed to be constants that are not affected by the execution of the instruction list.


constants

Some frequently used constants are stored as constants for use by all instruction lists. These are stored in the space between R(0) and R(-6). They are not affected by garbage collects.

Although changing the values of the registers R(0) to R(-6) is not by itself prevented, the "jump always" code will for instance not work if R(-1) is set to something else than the value\ 1. Similar trouble can arise from changing the other constants in this range.

The space below R(-7) can be used for constants present in user entry points. These constants are deleted when the list is deleted with which they are associated.

The values of the constants which can be used in all lists are:

Location Name Value Type
R(0) 0 0 Number
R(-1) 1 1 Number
R(-2) 2 2 Number
R(-3) PI 3.14159265 Number
R(-4) FALSE False Logical
R(-5) TRUE True Logical
R(-6) NILL Nill Undefined

Notes:

  1. The values for False and True are internally represented as 0 and 1 respectively, but arithmetic between Logicals and Numbers is not encouraged.

predefined

Some global variables are defined during the initialisation phase and they are kept up to date automatically. Most of these variables can not be modified by the user.

Global Type Explanation
BATCH Logical True if running in batch, False otherwise.
FRAME Number Sequential number of the plot frame
INPUT String Name of the current input stream.
INTERACT Logical True if running interactively, False otherwise.
MACHINE String The type of computer on which you're running.
OK Logical True if last algebra instruction was successful.
OUTPUT String Name of the current output stream.
TIME_LEFT Number The amount of CPU time that remains [sec].
X Number Used for fitting

Additional information on:

 

Go to the top level, to algebra, to variables, to the topic index, to the table of contents, or to the full text.

Formatted on 21/01/18 at 16:55.