Getting Started Documentation Glish Learn More Programming Contact Us
Version 1.9 Build 1556
News FAQ
Search Home


next up previous
Next: Arrays Up: NOTE 195 Getting Started with Glish for AIPS++ Previous: Starting AIPS++ and Glish.

Variable Assignment and Automatic Typing

Glish variables are automatically assigned a variable type when they are created. In other words, there is no variable declaration statement. After the following assignments:

- x := 2
- z := 23.786
- check := F
- status := T
- amp := 1.66 - 0.994i

'x' is an integer, 'z' is a double, 'check' and 'status' are booleans (true or false), and 'amp' is double complex. The available data types are boolean, byte, short, integer, float, double, complex, and double complex. Floating point constants are assumed to be double, and integer constants are assumed to be integer. In most cases the data type is self-evident, but, if your code needs to know a variable's type, there are built-in test functions such as is_boolean(), is_float(), is_dcomplex(), etc., which return a boolean value T or F. Also, the function type_name() will return a character string describing the type. For example,

- is_float(z)
F

- type_name(z)
double

Automatic variable typing is quite convenient, but it provides no warning when you inadvertently reassign a variable. The assignment

- status := 1.65e7

is perfectly legal resulting in 'status' now being a double. You can protect a variable from reassignment with the 'const' keyword as in

- const Pi := 3.14159

Values of variables and constants in an arithmetic expression are all converted to the type of highest numeric precision in the expression before performing the mathematical operations. In an expression using the variables above

a := x * z * check

'a' is a double. Variable typing may be forced for purposes such as integer truncation or storage economy with the built-in functions as_integer(), as_float(), etc. Keep in mind, however, that a variable is retyped every time it is assigned, so, on a few occasions, your expressions may need to be very explicit about type.

The arithmetic operators are

+  -  *  / %  ^

where % is the modulus operator that converts its operand values to integer and returns an integer result, and ^ is the exponentiation operator with floating point arguments and result.

- 26.8 % 4.2
2
- 3.01^1.99
8.960811

The value of a variable may be printed with the print statement or, in interactive mode, by simply typing the variable name without the ':=' assignment operator. The following two statements are equivalent:

- print x
- x

Be careful with array names! You can get a real screen full.


next up previous
Next: Arrays Up: NOTE 195 Getting Started with Glish for AIPS++ Previous: Starting AIPS++ and Glish.
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc., Washington, D.C.

Return to AIPS++ Home Page
2006-10-15