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


next up previous contents index
Next: Manipulating Vectors and Arrays Up: Predefined Functions and Variables Previous: Type Identification

Subsections



Type Conversion

The following functions convert their argument to the stated type:

    as_boolean(x)
    as_byte(x)
    as_short(x)
    as_integer(x)
    as_float(x)
    as_double(x)
    as_complex(x)
    as_dcomplex(x)
    as_string(x)
The argument x must be either numeric- or string-valued.

See § 3.1.3, page [*], for a discussion of implicit type conversion (i.e., not requiring the use of one of these functions).

Boolean Conversions

Conversion of a numeric values to boolean yield T if the converted value is non-zero. A string value yields T if its length is non-zero. For example,

    as_boolean([3.14159, 0])
yields [T, F], and
    as_boolean("how are you?")
yields [T, T, T], and
    as_boolean(['','a',''])
yields [F, T, F], and
    as_boolean(".0000001")
yields T, and
    as_boolean(".0000001foo")
and
    as_boolean("0.")
and
    as_boolean(0+9i)
yields T.

Note that an empty string here means a string with no text in it; this is different from a string with no elements.

    as_boolean('')
yields F, but
    as_boolean("")
yields [], an empty (boolean) vector.


Integer Conversions

A boolean value converted to byte, short, or integer yields 1 if the value was T and 0 if F.

Conversions between byte, short, and integer types yields the same values as the host machine's C++ compiler doing the same conversion via a cast.

A float or double value yields the same integer value as the host machine's C++ compiler doing the same conversion via a cast. In particular, it is possible for a value like -3.14159 to be converted to -3 or -4 depending upon the particular compiler. If the direction of this conversion is important, you can use floor and ceiling:

ceiling(x)
returns the smallest integer which is greater than or equal to the argument, x. This function can be abbreviated as ceil. If x is a vector, ceiling is applied to each of the elements of the vector.

floor(x)
returns the largest integer which is less than or equal to the argument, x. If x is a vector, floor is applied to each of the elements of the vector.

These will reliably convert floating point numbers to integers.

complex or dcomplex values behave like float or double values except that complex or dcomplex values also lose their imaginary portion.

A string value is converted as per the C (and C++) routine atoi(). If the value is not a valid integer then it is converted to 0.

Float and Double Conversions

A boolean value converted to float or double yields 1.0 if T and 0.0 if F.

complex or dcomplex values lose their imaginary portion when converted to float or double.

A string value is converted as per the C (and C++) routine atof(). If the value is not a valid floating-point number then it is converted to 0.0.

Complex Conversions

A boolean value converted to complex or dcomplex yields 1.0+0.0i if T and 0.0+0.0i if F.

float or double numbers converted to complex or dcomplex results in a complex number whose real portion is equal to the float or double value, and whose imaginary portion is 0.0.

A string value is converted as per the C (and C++) routine atof().

String Conversions

boolean
values when converted to a string yields "T" if true and "F" if false.

byte
, short, or integer values yield their natural string representation.

float
values are converted as per printf()'s ``%.6g'' format.

double
values are converted as per printf()'s ``%.12g'' format.

complex
values are converted as per printf()'s ``%.6g+%.6gi'' format.

dcomplex
values are converted as per printf()'s ``%.12g+%.12gi'' format.

The conversion of floating point values to strings are changed by setting the system.print.precision value or the print.precision attribute for an individual value (see § 5.3.1, page [*]).


next up previous contents index
Next: Manipulating Vectors and Arrays Up: Predefined Functions and Variables Previous: Type Identification   Contents   Index
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