Part two.
Continuing on with our exploration of decimals in Forth we shall explore number formatting and the use of the system variable DPL.
At this point I would like to recommend a most excellent Forth publication which is not only based on Forth-79 but whose superb, pertinent examples and exercise contained therein are compliant with Toddy Forth-79. One of the main grumblings concerning Forth is the lack of standardisation between dialects, even those supposedly of the same dialect can contain words not compliant with each other, and there is nothing more infuriating than typing in a long listing only to bump into a word or operator not included in your particular Forth tongue! ?DO anyone?
Just imagine if there were three different versions of Z80 assembly language, all doing the same thing, but unable to communicate with each other!
The book, entitled "The Complete Forth" by Alan Winfield is available in PDF format and can be found here...
https://ia803101.us.archive.org/6/items ... _forth.pdf
I was intending to write about the use of the DPL variable but given my penchant for, no doubt noticeable, verbosity I feel it's best to let Professor Winfield do the talking.
The relevant chapter in his book can be found at 8.4 pages 80 through to 85.
These pages also contain a section on number formatting and so should suffice as a lesson however I feel I should earn my metaphorical salt and write something of my own as promised, which although identical in concept to Winfield's text goes slightly further in specifying how to have more than two places after the point, with something about the magnitude of numbers to follow later.
Number formatting.
Remember how in part one I introduced the concept of placing the point/period used in double numbers in such a way as to look like a proper decimal number to our human eyes?
Well thankfully TF79 contains such a method of making this a reality, consider the statement below for example...
: 2PAP <# # # 46 HOLD #S #> TYPE SPACE ;
This is a simple formatting routine to have two numbers after the point, useful for say currency work. Lets break it down.
: 2PAP
This is a colon definition not a direct console command and the only way number formatting in this manner is allowed so we start with the colon.
Next we give the definition a name, in this example 2PAP. A term I use to mean "TWO PLACES AFTER THE POINT." It thus becomes obvious that for say three numbers after the point one could call it "3PAP" and so on.
< # # #
The definition opens with a less than symbol then a hash. This tells Forth that number formatting has now begun. what follows next are two more hash symbols, this tells forth that we are working with two decimals after the point.
46 HOLD
The word HOLD tells Forth that it should display the symbol given by the ASCII value that precedes it, in this case 46 representing a full stop/point or period. You can of course use other values depending on what symbol you want as the delimiter, some prefer the colon for example.
#S #>
The final part of the formatting process consists of a hash followed immediately by the letter S without space, a space, then a hash then greater than symbol again no space in between them. The hash followed by the less than symbol opened up the formatting process so it must therefore be inferred that the hash greater than combination closes the same process, however the hash S combination requires some explanation of how forth works out and prints the decimal numbers concerned
#S
It is good time to remember Forth's reverse way of working, number formatting is no exception.
The definition for two places after the point we are working on has the two hash symbols representing the decimal places required for places AFTER the point at the beginning of the definition . This is because in Forth the routine works out the two places after the point first in any computation using it.
It then uses the HOLD command to place the delimiter, so what about the remaining numbers BEFORE the point? There may one, two or ten places before the point so how do we work that out?
The answer quite simply lies with the hash S operator which simply tells Forth, now that you have worked out and printed the two places after the point, the delimiter be it point or colon, what ever figures are left in the computation just place them BEFORE the point.
A most elegant way of working out what lies before the point without user intervention makes the hash S operator quite a powerful one.
TYPE SPACE ;
This simply prints out everything in the correct order and is explained more fully in Winfield's book as are all the above points.
The semi colon closes the definition. I have just tried to present things in a more simplistic fashion than the book, hopefully to encourage people to take up Forth and gain some understanding before delving deeper.
IMPORTANT TO NOTE.
When entering figures using this method it should be remembered that however many places there are after the point in the definition then any figures entered/used should also have the same amount of places after the point other wise spurious/incorrect results will apply even if it is a single number such as say 1 or 2.
EG for two places after the point enter figures as so...
1.00 or 2.00 not 1. 2.
The above is for TF79 0nly as H4th, surprisingly for a Forth that is supposedly based on FIG Forth doesn't contain the DPL variable nor the means of number formatting described, both of which are reputedly FIG Forth in origin.
Usage.
Example.
1.23 1.23 D+ 2PAP (enter/newline)
Next ( Interest not withstanding )
The magnitude of numbers.
Code: Select all
\ 2PAP
: 2PAP <# # # 46 HOLD #S #> TYPE SPACE ;