Next: Forth Words, Previous: An Introduction to Standard Forth, Up: Gforth [Contents][Index]
To push an integer number on the data stack, you write the number in
source code, e.g., 123
. You can prefix the digits with
-
to indicate a negative number, e.g. -123
. This works
both inside colon definitions and outside. The number is interpreted
according to the value of base
(see Number Conversion).
The digits are 0
to 9
and a
(decimal 10) to
z
(decimal 35), but only digits smaller than the base are
recognized. The conversion is case-insensitive, so A
and
a
are the same digit.
You can make the base explicit for the number by using a prefix:
#
– decimal
%
– binary
$
– hexadecimal
&
– decimal (non-standard)
0x
– hexadecimal, if base<33 (non-standard).
For combinations including base-prefix and sign, the standard order is
to have the base-prefix first (e.g., #-123
); Gforth supports
both orders.
You can put a decimal point .
at the end of a number (or,
non-standardly, anywhere else except before a prefix) to get a
double-cell integer (e.g., #-123.
or #-.123
(the same
number)). If users experienced in another programming language see or
write such a number without base prefix (e.g., -123.
), they may
expect that the number represents a floating-point value. To clear up
the confusion early, Gforth warns of such usage; to avoid the
warnings, the best approach is to always write double numbers with a
base prefix (e.g., #-123.
)
Here are some examples, with the equivalent decimal number shown after in braces:
$-41
(-65), %1001101
(205), %1001.0001
(145 - a double-precision number),
#905
(905), $abc
(2478), $ABC
(2478).
You can get the numeric value of a (character) code point by
surrounding the character with '
(e.g., 'a'
). The
trailing '
is required by the standard, but you can leave it
away in Gforth. Note that this also works for non-ASCII characters.
For many uses, it is more useful to have the character as a string
rather than as a cell; see below for the string syntax.
For floating-point numbers in Forth, you recognize them due to their
exponent. I.e. 1.
is a double-cell integer, and 1e0
is
a floating-point number; the latter can be (and usually is) shortened
to 1e
. Both the significand (the part before the e
or
E
) and the exponent may have signs (including +
); the
significand must contain at least one digit and may contain a decimal
point, the exponent can be empty. Floating-point numbers always use
decimal base for both significand and exponent, and are only
recognized when the base is decimal. Examples are: 1e 1e0 1.e
1.e0 +1e+0
(which all represent the same number) +12.E-4
.
A Gforth extension (since 1.0) is to write a floating-point number in
scaled notation: It can optionally have a sign, then one or more
digits, then use one of the mostly SI-defined scaling symbols (aka
metric prefixes) or %
, and then optionally more digits. Here’s
the full list of scaling symbols that Gforth accepts:
Q
e30
quetta
R
e27
ronna
Y
e24
yotta
Z
e21
zetta
X
e18
exa (not E
)
P
e15
peta
T
e12
tera
G
e9
giga
M
e6
mega
k
e3
kilo
h
e2
hecto
d
e-1
deci
%
e-2
percent (not c
)
m
e-3
milli
u
e-6
micro (not μ
)
n
e-9
nano
p
e-12
pico
f
e-15
femto
a
e-18
atto
z
e-21
zepto
y
e-24
yocto
r
e-27
ronto
q
e-30
quecto
Unlike most of the rest of Gforth, scaling symbols are treated
case-sensitively. Using the scaled notation is equivalent to using a
decimal point instead of the scaling symbol and appending the
exponential notation at the end. Examples of scaled notation:
6k5
(6500e) 23%
(0.23e).
You can input a string by surrounding it with "
(e.g. "abc"
, "a b"
). The result is the starting address
and byte (=char) count of the string on the data stack.
You have to escape any "
inside the string with \
(e.g.,
"double-quote->\"<-"
). In addition, this string syntax
supports all the ways to write control characters that are supported
by s\"
(see String and Character literals). A disadvantage
of this string syntax is that it is non-standard; for standard
programs, use s\"
instead.
You can input an environment variable by first loading
rec-env.fs and then prefixing the environment variable with
$
, e.g., $HOME
; the result is a string descriptor on the
data stack in the format described above. This is equivalent to
"HOME" getenv
, i.e., the environment variable is resolved at
run-time.
You can input an execution token (xt) of a word by prefixing the name
of the word with `
(e.g., `dup
). An advantage over
using '
or [']
is you do not need to switch between them
when copying and pasting code from inside to outside a colon
definition or vice versa. A disadvantage is that this syntax is
non-standard.
You can input a name token (nt) of a word by prefixing the name of the
word with ``
(e.g., ``dup
). This syntax is also
non-standard.
You can input a body address of a word by surrounding it with <
and >
(e.g., <spaces>
). You can also input an address
that is at a positive offset from the body address (typically an
address in that body), by putting +
and a number (see syntax
above) between the word name and the closing >
(e.g.,
<spaces+$15>
, spaces+-3
). You will get the body address
plus the number. This non-standard feature exists to allow copying
and pasting the output of ...
(see Examining data and code).
Next: Forth Words, Previous: An Introduction to Standard Forth, Up: Gforth [Contents][Index]