Getting Started | Documentation | Glish | Learn More | Programming | Contact Us |
Version 1.9 Build 1556 |
|
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).
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.
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:
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.
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.
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().
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 ).