11.1.
Appropriate shrinkage
for
the
correct
type
.
.R D.
is
uniquely based on one or two digit generators.
With the same basic words,
we can write similar words at any time, which in some form print out the
value of a given type of verme.
The binary value on the worm
can be converted into digits that can be written into digits,
and the
#
# word starts and
#> ends.
You
can run the words that have been converted (
#, #S, HOLD,
SIGN
) after calling
<# before
the
#> call.
|
As you can see, there are several FORTH words in the # character. To avoid spending too much time searching for this character, note one important thing:
There
are no # characters on English keyboard machines, and we always have to use
£.
|
During
conversion, the memory creates a set of converted tickets and additional
symbols, whose title and length is #> placed on the stack (so you can
type it with TYPE).
Anyone who has tried to transcribe a number from one number system to another
can know that it only goes backwards, starting with the last one.
Conversion words are
"backward":
the
goblet of characters to be written is made from the back.
The last byte of the rope is
the first byte of the pad.
|
(Do
not be surprised, therefore, when converting words ruin your data stored
on the bench at the beginning!)
The
# (ud1 --- ud2)
word
inserts the (next) ticket to the garter.
Function: distributes ud1 unmarked doubling with BASE content.
The ud2 queue is placed on
the stack so the conversion can continue;
calculate the code of the
corresponding numeric code from the remainder and enter it into the cluster.
THE
#S
converts
the unmarked duplicate word on the vermin to the number of tickets that are
needed (so worthless, leading zeroes).
The worm remains a double-blind
0.
Source #S otherwise:
: #S (ud --- double-0)
BEGIN
# (one ticket)
2DUP OR 0 = (
Dweller Word 0?) UNTIL (if not 0 was the last double word, continue)
;
The DWORD in which the partial results of the conversion were kept, a
#>
throw it off the stack.
The
# , so the
#S also produces the corresponding number
BASE systemic tickets.
THE
HOLD (c ---)
word
will enter the received c character in front of the converted tickets.
For example, if you want to include a double-digit, unsigned number so that
the last two digits are displayed as decimal places:
: 2THE
<#
# #
46 HOLD
#S
#>
TYPE
;
(ud ---)
(start of conversion)
(the last two tickets have been generated)
(decimal point)
(additional tickets)
(end of conversion)
Or, if we want to give you the number of decimals you want in the worm:
: TJE
<#
0 DO # LOOP
46 HOLD
#S
#>
TYPE
;
(decimal places ---)
(start of conversion)
(decimals)
(decimal point)
(and others)
(end of conversion)
If you want to write an unmarked, one-word value, it is no problem either. We can easily make a double word, because (as a positive number) the higher value of the double word value is 0, simply this should be done only on the stack. One possible source for U. is, for example:
: U.
0
<# #S #>
TYPE SPACE
;
(ud ---)
(start of conversion)
(the last two tickets have been generated)
(decimal point)
(additional tickets)
(end of conversion)
Mostly, however, we work with signatures. To do this we need to get acquainted with
SIGN (nd-d)
so that is also used between <# and #>. SIGN sets the converted sequence with a minus sign or not, depending on the sign of n (ie the third element of the stack). For example, a . a possible source:
:.
DUP ABS
0
<# #S SIGN #>
TYPE SPACE
;
(n ---)
(the upper instance has no sign)
(easy without sign)
(make a duplicate)
Finally,
a witty example from Leo Brodie, STARTING FORTH:
Let's say that the doubling of the bug is the number of seconds, and what
time is the minute: minute: we want to post this time in seconds.
We have to work between the
10 and 6 number systems.
: SEXTAL
6 BASE! ;:: OO
#
SEXTAL
#
DECIMAL
58 HOLD
;
(transition to number 6)
(making a decimal ticket)
(making a "tick" ticket)
(colon)
The above program, if you think about it, is the remainder of the division with the 60, in a decimal system. (The quotient is passed on to the vermin.) So we can easily write the word you want:
: SEC (ud ---)
<#: OO: OO #S #> TYPE SPACE
;
11.2.
What do we get from NUMBER?
We know in the
first
chapter
that the interpreter will search the words received for the first time in
the dictionary and if he can not find it, he tries to interpret the number.
The latter is a
NUMBER (title --- d)
calling.
NUMBER will try to produce
the corresponding double word from the FORTH font specified by BASE (BASE).
If it does not go, it gets
"missed" with the error message, ie it quits the queue by quitting the queue.
If, on the other hand, the
girth is a sign, and possibly a decimal point, it consists of a sign that
can be interpreted in that particular numeric system, it places the double
binary value (calculated without taking a decimal point) into the stack;
Additionally, the DPL sets
the decimals number in the system variable.
If there was no decimal point,
the DPL is -1. (The interpreter,
when analyzing the numbers found in my influence, drops the higher value
of the converted doubling word by DPL -1, which we see as a single value.)
Example: "prefix" multiplication taking into account the decimal
point."*" can be used in this way:
"*" 1.2 -0.2
Read the numbers with the WORD and analyze them with NUMBER:
: "*"
2 0 DO
BL WORD
HERE NUMBER
DROP
DPL @
0 MAX
LOOP
(---)
(read on two numbers)
(FORTH string at HERE)
(conversion double talk)
(then considered a)
(number of decimals)
(DPL -1 if it was not)
(decimal)(the vermin: value 1 dpl1 value 2 dpl2) > R SWAP> R
M *
> R DUP R> DABS
R> R> +
<# -DUP IF
0 DO # LOOP
46 HOLD
ENDIF
#S SIGN #>
TYPE SPACE
;(value1 value 2)
(double-word product)
(preparation for conversion)
(number of decimal places)
What was that about?
Summary
of Chapter 11
The words learned:
<# | (---) | Conversion of numbers from binary to character. |
# | (ud1 --- ud2) |
Generates
the following character code in the output string.
The duplicate value added must
be used to generate additional characters (this is the fraction of the division
with the BASE content, and the character code is generated from the remainder).
You can use the words <#
and #>.
|
#S | (ud1 --- duplaszavas-0) |
Generates
numeric codes in the output string until the value of the duplicate value
to be converted runs out during the distribution;
that is, in the converted string,
the valuable ones (and only those) are displayed.
You can use the words <#
and #>.
|
MOON | (c ---) | Inserts ac in the output string. You can use the words <# and #>. |
SIGN | (nd-d) | One - adds a signal to the output string if n is negative. You can use the words <# and #>. |
#> | (ud --- length) | Complete Conversion. Drops the doubler used during the conversion from the stack, instead replaces the title and length of the converted string on the stack. |
NUMBER | (title --- d) |
For
FORTH that begins with the title, convert it to an ad doubling doubling,
according to the BASE value. If
the FORTH hanger to be converted includes a decimal point, then the number
of digits to the right of the decimal point is placed in the DPL system variable;
if not, the DPL will be -1.
If the conversion can not be
completed, the program run will be canceled with an error
message.
|
DPL | ( --- title ) | Changing the system. NUMBER sets the number of decimal places. |
Examples
11.1. Write a word T (n decimal places ---) that specifies the signed, one-word n value with the specified decimal number.
: T (N decimal places ---)
> R DUP ABS
0 (sign ud value)
<# R> (number of decimals)
-DUP IF (if decimal)
0 DO # LOOP 46 HOLD
ENDIF
#S SIGN
#> TYPE
;
11.2. Write a .L (n field width --- word), which sets the n value to the left in the width field, ie if the series to be written is shorter, multiplies the required number of spaces, if it is longer, cut off low-value tickets.
:. L
> R DUP ABS 0
<# # S SIGN #>
R MIN
SWAP OVER
TYPE
R> SWAP -
SPACES
;
(Field width n ---)
(stack of the converted sequence)
(length and address)
(if need be, cut off the excess)
(the stack: the longitudinal length of the title)
(the stack: length)
(if necessary, spaces)
11.3. Write a% (percentage) --- word, which takes the percentage base from the bottom of it and prints the percentage with the required number of decimal places! We assume that the percentages of the precious digits fit in a word, and that the percentage basis is given by a decimal point. For example:
50% 24.4
12% 3.
For Forbes: Percentage = Percentage Base * Percentage / 100.
:%
BL WORD
HERE NUMBER
DROP M *
SWAP OVER DABS
<# DPL @
2+
0 DO # LOOP
46 HOLD #S SIGN
#> TYPE SPACE
;
(N ---)
(% basic character charge)
(% base conversion)
(double word is the% value)
(preparation dump)
(that was corporal% base)
(the division by 100 in)
(decimal point tologatásával)
(indicated)
11.4.
What
can be the SIGN source?
The FIG solution:
: SIGN
ROT 0 <IF 45 HOLD ENDIF;