Literal Values for Basic Types¶
Most of Chapel’s basic types support a corresponding literal format for specifying their values at the default bit-width. Values of other bit-widths are obtained via type conversions (casts or coercions).
Boolean Values¶
The bool
type supports true
and false
as its two literal
values:
var myBool: bool;
myBool = true;
myBool = false;
Integral Values¶
Literal values for int
types are typically written as a sequence
of decimal digits:
var myInt: int;
myInt = 123;
However, they can also be expressed as binary, octal, or hexadecimal
values via the 0b
/0B
, 0o
/0O
, and 0x
/0X
prefixes, respectively:
myInt = 0b1001;
myInt = 0o177;
myInt = 0x1ff;
where these values correspond to decimal values:
9
127
511
It’s worth noting that in Chapel, there are no negative integer
literal values, simply applications of the unary negation operator
(-
) to positive integer literals.
The uint
type does not have its own literal format. However,
integer literals that do not fit in an int(64)
yet do fit within
a uint(64)
are considered to be unsigned integer values. Thus, in
the following declarations, the first variable is inferred to be of
type int
while the second is inferred to be of type uint
:
var myMedInt = 2000000000;
var myLargeInt = 10000000000000000000;
To represent a smaller integral value as a uint
, type conversions
(casts or coercions) must be used.
Floating Point Values¶
The real
type supports literal values via either decimal or
exponential formats:
var myReal: real;
myReal = 1.23;
myReal = 1.2e3;
where these literals correspond to the values:
1.23
1200.0
In the decimal form, a .
must be used to distinguish the value
from an integral literal. Note that 10.
is not a valid floating
point literal value in Chapel due to a syntactic ambiguity with making
a method call on an integral value. For this reason, 10.0
must be
used instead.
Note that in the exponential form, the base decimal value can be an
integer value and/or capital E
may be used to set off the
exponent. Thus, the values in the following four assignments are all
equivalent:
myReal = 123.0e2;
myReal = 123e2;
myReal = 123.0E2;
myReal = 123E2;
Floating point values may also be specified using a hexadecimal format
using the 0x
prefix. In such cases, p
is used to indicate the
exponent (avoiding ambiguity with the hexadecimal digit e
) and
indicates a power of 2 rather than 10:
myReal = 0x1p-1;
myReal = 0x1.8;
Thus, these expressions correspond to the floating point values:
0.5
1.5
Literal values for the imag
type are identical to those for
int
and real
, yet with an i
suffix. Thus, the following
assignments demonstrate imaginary literals:
var myImag: imag;
myImag = 1.23i;
myImag = 1.2e3i;
myImag = 123i;
myImag = 0x0.4p-1i;
Note that whether using the int
or real
form, imaginary values
are stored using a floating point representation. Thus, these
expressions correspond to the values:
1.23i
1200.0i
123.0i
0.125i
Note that the standalone expression i
does not refer to the
mathematical value i, but rather to a Chapel symbol (e.g., variable,
procedure, etc.) named i. To get the mathematical value, use an
expression like 1i
or 1.0i
.
The complex
type does not support a native format for literal
values. Instead, complex values are typically expressed by adding or
subtracting real
and imag
values. For example:
var myComplex: complex;
myComplex = 1.2 + 3.4i;
String Literals¶
Literal string
values can be expressed using either single or
double quotes. For example, the following represent legal string
values:
var myString: string;
myString = "hello";
myString = 'hi';
One impact of this is that the other type of quotes can be used within a string literal without additional effort:
myString = "he said, 'hi there!'";
Subsequent sections will cover strings in more detail.