12.22Floating point numbers (separate stack). 4tH 4tH Forth compiler
Reklama
Reklama
CHAPTER 12. STANDARD LIBRARIES
12.22
Floating point numbers (separate stack)
195
Warning!
This is really complex stuff, I cannot guarantee that it functions flawlessly. You may lose accuracy or get the wrong result. Don’t use any of this for any real life applications.
If you want something more sophisticated than zenfloat.4th you should try ansfloat.4th
. This library is a full ANS-Forth implementation. In ansfloat.4th mantissas are double-cell unsigned. Single-cell exponents contain the mantissa’s sign and a signed exponent. The format used is non-standard for simplicity. Exponents are almost a whole cell wide, leading to a wider dynamic range than most IEEE formats. Range and digits of precision versus cell width are listed below.
C
ELL WIDTH
D
IGITS OF PRECISION
R
ANGE
= 10
−x to 10 x
32
64 n
18
37
INT ((n − 1) ∗ 0.602) x
= 323196289 x = 1.3881175 ∗ 10
18 x = 0.301 ∗ (2 n
−2
)
Table 12.7: Range and digits of precision
Floating point arithmetic is easy to use, but be careful to watch where your inaccuracies are coming from. Floating point numbers are approximations. You can lose up to half a bit of precision in each operation. Differences between large numbers can be trouble spots. If you need transcendental functions, they can often be done in integer arithmetic since you don’t need floating point’s run-time auto-scaling. Floating point arithmetic uses a dedicated, shallow stack
20
. There is no depth checking, so underflows and overflows may occur unless error checking is added. Since floating point support is implemented in high level 4tH it is also rather slow and big.
So, how does it work. First of all, if you want to use floating point, you always need to include this library before any other floating point library: include lib/ansfloat.4th
\ floating point words
This will also create the floating point stack. If you want a larger stack, just create this constant accordingly before including the library:
32 CONSTANT FLOATING-STACK include lib/ansfloat.4th
\ size of float stack
\ floating point words
This will create a stack of 32 floating point items. You probably want to do some I/O, so let’s take care of that one too:
32 CONSTANT FLOATING-STACK include lib/ansfloat.4th
include lib/ansfpio.4th
\ size of float stack
\ floating point words
\ floating point I/O words
Then you probably need some variables. But hey, if floating point numbers take up more than a cell, you can’t use ordinary variables. That’s true. You will need small arrays:
20
ANS Forth defines a six item stack. A larger FP stack is an environmental dependency.
CHAPTER 12. STANDARD LIBRARIES 196
FLOAT array fVar1
FLOAT array fVar2
\ double variable one
\ double variable two
Finally, you have to initialize the library and specify the precision
21
: fclear
8 set-precision
\ initialize library
\ set precision to eight
And here comes the next problem. How do you enter floating point numbers? Depending on the size of the number, you can use two approaches. The easiest one is to convert a single number to a floating point number with ”S>F”.
500 s>f 60000 s>f f+ fdup f. cr fVar1 f!
\ add 500 and 60000
\ print the floating point number
\ store it in variable one
Note the double numbers library is loaded by default as well, so ”D>F” is available too.
Every single operation has a floating point counterpart:
S
INGLE
F
LOAT
+
/
* f+ f/ f* negate fnegate
.
f.
max min fmax fmin dup
@
!
fdup f@ f!
Table 12.8: Examples of single and floating point number counterparts
But what if you want to enter a floating point number right away? In that case you will have to convert a string to a double number with ”>FLOAT”.
s" 5000.575" >float drop fdup f. cr fVar1 f@ fmax fVar2 f!
\ convert a string to a float
\ drop the flag and print the number
\ save the largest in variable two
A drawback of defining floating point constants that way is that ”>FLOAT” is notoriously slow. However, there is a library that enables you to enter floating point numbers the
”mantissa + exponent” way
22
: include lib/ansfloat.4th
include lib/ansfpio.4th
include lib/fpow10.th
5000572 -3 me>f f. cr
314159265 -8 me>f f. cr
21
The number of significant digits used by F.
22
See section 12.21.
\ include the fp library
\ include the fp i/o library
\ include the F10** library
Preuzimanje
Reklama