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

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 gif, for a discussion of implicit type conversion (i.e., not requiring use of one of these functions).

Boolean Conversions

  Conversions to boolean yield T if the converted value is non-zero. A string value yields T if its length is non-zero; otherwise it yields F. 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)
yield 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 values as would the host machine's C++ compiler when doing the same conversion via a cast.

A float or double value yields the same integer value as would the host machine's C++ compiler when doing the same conversion via a cast. In particular, it is not well-defined (I believe) whether a value like -3.14159 is converted to -3 or -4. If the direction this conversion goes is important, you can use floor and ceiling:

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

 

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

   


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

Thu Nov 13 16:44:05 EST 1997