12.20Double numbers. 4tH 4tH Forth compiler
Reklama
Reklama
CHAPTER 12. STANDARD LIBRARIES
2OS TOS fraction fraction fraction cell cell fraction fraction fraction cell cell cell fraction fraction fraction fraction fraction
Word
V*
V*
V*
V/
V/
V/
+
-
Result fraction cell cell fraction fraction cell fraction fraction
Table 12.5: Fraction words
191
[needs lib/math.4th]
: .fp <# # [char] . hold #S #> type cr ;
650 100 * sqrt .fp
Which will print:
25.4
To acquire greater precision we have to scale it up even further, like 10,000. This will show us, that "25.49" brings us even closer to the correct answer. If you want to use the
”fraction.4th” library, you will have to scale it to the square of ’10K’, which means you’re restricted to numbers upto ”21.475”: include lib/math.4th
include lib/fraction.4th
10K 10K * constant 100M
21 100M * sqrt s>v v. cr
All these words and more are included in the library file ”math.4th”. You might not find the word you actually need, because we are not much of a mathematician. When we encounter new routines they will be added to 4tH. We would appreciate your input!
12.20
Double numbers
C’mon, indulge me, run this program: max-n . cr
You will probably see some fairly large number displayed on your screen. What is it? Well, it is the largest number that can fit in a cell. Larger numbers and 4tH will start to behave erratically: max-n 1+ . cr
Still, it is large enough to do the accounting for a reasonably sized enterprise. But it hasn’t been always like that. Early Forths could barely handle the accounting of an average schoolboy. In order to get some real work done they had to expand the range somehow.
CHAPTER 12. STANDARD LIBRARIES 192
And if one cell isn’t enough you simply take two cells. That is what double numbers are all about: they are numbers that are composed of two cells.
The problem is that Forths operators aren’t overloaded. If you try to add up two double numbers with ’+’ you will end up with one double number and the addition of the two parts of the first double number. So in order to add up two double numbers, a separate word had to be defined. If you need a special word for addition, you will also need one for multiplication, subtraction, division and negation.
It is no secret that Charles Moore, the inventor of Forth, thought that double numbers had become superfluous after the introduction of modern processors and modern Forth compilers. That is one of the reasons that 4tH doesn’t have a native double word implementation.
But should you need this vastly expanded range for one reason or another 4tH allows you to enter the murky world of double, unsigned and mixed numbers.
So, how does it work. First of all, if you need a full implementation you have to include these two libraries: include lib/todbl.4th
include lib/dbldot.4th
\ double number input
\ double number output
Then you probably need some variables. But hey, if double numbers take up two cells, you can’t use ordinary variables. That’s true. You will need small arrays:
2 array dVar1
2 array dVar2
\ double variable one
\ double variable two
And here comes the next problem. How do you enter double numbers? Depending on the size of the number, you can use two approaches. The easiest one is to convert a single number to a double number with ”U>D”. The only catch is this only works for positive numbers. If you want to enter a negative number, you have to negate it afterwards:
500 u>d 60000 u>d d+
2dup d. cr dVar1 2!
\ add 500 and 60000
\ print the double number
\ store it in variable one
Yes, every single operation has a double counterpart:
S
INGLE
D
OUBLE
+ negate
.
2/ max min dup
@
!
d+ dnegate d.
d2/ dmax dmin
2dup
2@
2!
Table 12.6: Examples of single and double number counterparts
But what if you want to enter a very large number right away? In that case you will have to convert a string to a double number with ”S>DOUBLE
14
”.
14
Note it will bomb out when the string isn’t a double number. If you want to play safe, use ”>DOUBLE”.
There is also a single number ”>NUMBER” word. In that case you can select the double number version by using its alias ”>DNUMBER”.
Atsisiųsti
Reklama