6.20.1 Base and integer decimal point

By default, the number base used for integer number conversion is given by the contents of the variable base. Base affects both input integer conversion (unless a number prefix is used, see Literals in source code) and output integer conversion (unless a base-specific word such as h. or dec. is used, see Simple numeric output, see Integer to string conversion). In Gforth, non-decimal base disables floating-point conversion.

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; For dealing with the popular non-decimal bases, use number prefixes for inputting them (see Literals in source code) and h. and base-execute for outputting them.

base-execute ( i*x xt u – j*x  ) gforth-0.7

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

base ( – a-addr  ) core

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

Set base to $10 (hexadecimal). In many cases base-execute is a better alternative.

decimal ( ) core

Set base to #10 (decimal). In many cases base-execute is a better alternative.

dpl ( – a-addr  ) gforth-0.2 “d-p-l”

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

Number conversion has a number of traps for the unwary:

Words affected by base are rec-number (see Default recognizers), >number (see String to number conversion), and the words described in the sections Simple numeric output, and Integer to string conversion. Rec-number sets dpl.