Next: , Previous: Input Sources, Up: The Text Interpreter


5.13.2 Number Conversion

This section describes the rules that the text interpreter uses when it tries to convert a string into a number.

Let <digit> represent any character that is a legal digit in the current number base1.

Let <decimal digit> represent any character in the range 0-9.

Let {a b} represent the optional presence of any of the characters in the braces (a or b or neither).

Let * represent any number of instances of the previous character (including none).

Let any other character represent itself.

Now, the conversion rules are:

By default, the number base used for integer number conversion is given by the contents of the variable base. Note that a lot of confusion can result from unexpected values of base. If you change base anywhere, make sure to save the old value and restore it afterwards; better yet, use base-execute, which does this for you. In general I recommend keeping base decimal, and using the prefixes described below for the popular non-decimal bases.

dpl       – a-addr         gforth       “dpl”

User variable – a-addr is the address of a cell that stores the position of the decimal point in the most recent numeric conversion. Initialised to -1. After the conversion of a number containing no decimal point, dpl is -1. After the conversion of 2. it holds 0. After the conversion of 234123.9 it contains 1, and so forth.

base-execute       i*x xt u – j*x         gforth       “base-execute”

execute xt with the content of BASE being u, and restoring the original BASE afterwards.

base       – a-addr         core       “base”

User variable – a-addr is the address of a cell that stores the number base used by default for number conversion during input and output. Don't store to base, use base-execute instead.

hex              core-ext       “hex”

Set base to &16 (hexadecimal). Don't use hex, use base-execute instead.

decimal              core       “decimal”

Set base to &10 (decimal). Don't use hex, use base-execute instead.

Gforth allows you to override the value of base by using a prefix2 before the first digit of an (integer) number. The following prefixes are supported:

Here are some examples, with the equivalent decimal number shown after in braces:

-$41 (-65), %1001101 (205), %1001.0001 (145 - a double-precision number), 'A (65), -'a' (-97), &905 (905), $abc (2478), $ABC (2478).

Number conversion has a number of traps for the unwary:

You can read numbers into your programs with the words described in Line input and conversion.


Footnotes

[1] For example, 0-9 when the number base is decimal or 0-9, A-F when the number base is hexadecimal.

[2] Some Forth implementations provide a similar scheme by implementing $ etc. as parsing words that process the subsequent number in the input stream and push it onto the stack. For example, see Number Conversion and Literals, by Wil Baden; Forth Dimensions 20(3) pages 26–27. In such implementations, unlike in Gforth, a space is required between the prefix and the number.