6.1 Numeric literals
The simplest form of expression is a literal that denotes a primitive numerical value.
Integer literals
Integer literals represent integers of type int
. Integer
literals are written in base 10 without any separators. Integer
literals may contain a single negative sign. (The expression
--1
is interpreted as the negation of the literal -1
.)
The following list contains well-formed integer literals.
0, 1, -1, 256, -127098, 24567898765
Integer literals must have values that fall within the bounds for integer values (see section).
Integer literals may not contain decimal points (.
). Thus the
expressions 1.
and 1.0
are of type real
and may
not be used where a value of type int
is required.
Real literals
A number written with a period or with scientific notation is assigned
to a the continuous numeric type real
. Real literals are
written in base 10 with a period (.
) as a separator and
optionally an exponent with optional sign. Examples
of well-formed real literals include the following.
0.0, 1.0, 3.14, -217.9387, 2.7e3, -2E-5, 1.23e+3.
The notation e
or E
followed by a positive or negative
integer denotes a power of 10 to multiply. For instance, 2.7e3
and 2.7e+3
denote \(2.7 \times 10^3\), whereas -2E-5
denotes \(-2 \times 10^{-5}\).
Imaginary literals
A number followed by the character i
denotes an imaginary number and
is assigned to the numeric type complex
. The number preceding i
may be either a real or integer literal and determines the magnitude
of the imaginary number. Examples of well-formed imaginary literals
include the following.
1i, 2i, -325.786i, 1e10i, 2.87e-10i.
Note that the character i
by itself is not a well-formed imaginary
literal. The unit imaginary number must be written as 1i
.
Complex literals
Stan does not include complex literals directly, but a real or integer literal can be added to an imaginary literal to derive an expression that behaves like a complex literal. Examples include the following.
1 + 2i, -3.2e9 + 1e10i
These will be assigned the type complex
, which is the result of
adding a real or integer and a complex number. They will also
function like literals in the sense that the C++ compiler is able to
reduce them to a single complex constant at compile time.