[gforth] / gforth / float.fs  

gforth: gforth/float.fs


1 : anton 1.1 \ High level floating point 14jan94py
2 :    
3 : pazsan 1.6 1 cells 4 = [IF]
4 :     ' cells Alias sfloats
5 :     ' cell+ Alias sfloat+
6 :     ' align Alias sfalign
7 :     ' aligned Alias sfaligned
8 :     [ELSE]
9 : pazsan 1.8 : sfloats 2* 2* ;
10 : pazsan 1.6 : sfloat+ 4 + ;
11 :     : sfaligned ( addr -- addr' ) 3 + -4 and ;
12 :     : sfalign ( -- ) here dup sfaligned swap ?DO bl c, LOOP ;
13 :     [THEN]
14 :    
15 :     1 floats 8 = [IF]
16 :     ' floats Alias dfloats
17 :     ' float+ Alias dfloat+
18 :     ' falign Alias dfalign
19 :     ' faligned Alias dfaligned
20 :     [ELSE]
21 : pazsan 1.8 : dfloats 2* 2* 2* ;
22 : pazsan 1.6 : dfloat+ 8 + ;
23 :     : dfaligned ( addr -- addr' ) 7 + -8 and ;
24 :     : dfalign ( -- ) here dup dfaligned swap ?DO bl c, LOOP ;
25 :     [THEN]
26 :    
27 : anton 1.1 : f, ( f -- ) here 1 floats allot f! ;
28 :    
29 :     : fconstant ( r -- )
30 : anton 1.13 Create f,
31 :     DOES> f@ ;
32 : anton 1.1
33 :     : fdepth ( -- n ) f0 @ fp@ - [ 1 floats ] Literal / ;
34 :    
35 :     : FLit ( -- r ) r> faligned dup f@ float+ >r ;
36 :     : FLiteral ( r -- ) postpone FLit falign f, ; immediate
37 :    
38 : anton 1.13 &15 Value precision
39 : anton 1.1 : set-precision to precision ;
40 :    
41 :     : scratch ( r -- addr len )
42 :     pad precision - precision ;
43 :    
44 :     : zeros ( n -- ) 0 max 0 ?DO '0 emit LOOP ;
45 :    
46 :     : -zeros ( addr u -- addr' u' )
47 :     BEGIN dup WHILE 1- 2dup + c@ '0 <> UNTIL 1+ THEN ;
48 :    
49 : pazsan 1.4 : f$ ( f -- n ) scratch represent 0=
50 :     IF 2drop scratch 3 min type rdrop EXIT THEN
51 :     IF '- emit THEN ;
52 :    
53 :     : f. ( r -- ) f$ dup >r 0<
54 :     IF '0 emit
55 : anton 1.1 ELSE scratch r@ min type r@ precision - zeros THEN
56 :     '. emit r@ negate zeros
57 :     scratch r> 0 max /string 0 max -zeros type space ;
58 : benschop 1.3 \ I'm afraid this does not really implement ansi semantics wrt precision.
59 :     \ Shouldn't precision indicate the number of places shown after the point?
60 : anton 1.1
61 : pazsan 1.4 : fe. ( r -- ) f$ 1- s>d 3 fm/mod 3 * >r 1+ >r
62 : anton 1.1 scratch r@ min type '. emit scratch r> /string type
63 :     'E emit r> . ;
64 :    
65 : pazsan 1.4 : fs. ( r -- ) f$ 1-
66 :     scratch over c@ emit '. emit 1 /string type
67 :     'E emit . ;
68 :    
69 : anton 1.7 : sfnumber ( c-addr u -- r / )
70 :     2dup >float
71 :     IF
72 :     2drop state @
73 :     IF
74 :     postpone FLiteral
75 :     THEN
76 :     ELSE
77 :     defers notfound
78 :     THEN ;
79 : anton 1.1
80 : anton 1.7 ' sfnumber IS notfound
81 : anton 1.13
82 :     : fvariable ( -- )
83 :     Create 0e0 f, ;
84 :     \ does> ( -- f-addr )
85 : anton 1.1
86 :     1e0 fasin 2e0 f* fconstant pi
87 : pazsan 1.6
88 :     : f2* 2e0 f* ;
89 : pazsan 1.12 : f2/ .5e0 f* ;
90 : pazsan 1.6 : 1/f 1e0 fswap f/ ;
91 :    
92 :    
93 : anton 1.10 \ We now have primitives for these, so we need not define them
94 : pazsan 1.6
95 : anton 1.10 \ : falog ( f -- 10^f ) [ 10e0 fln ] FLiteral f* fexp ;
96 :    
97 :     \ : fsinh fexpm1 fdup fdup 1e0 f+ f/ f+ f2/ ;
98 :     \ : fcosh fexp fdup 1/f f+ f2/ ;
99 :     \ : ftanh f2* fexpm1 fdup 2e0 f+ f/ ;
100 :    
101 :     \ : fatanh fdup f0< >r fabs 1e0 fover f- f/ f2* flnp1 f2/
102 :     \ r> IF fnegate THEN ;
103 :     \ : facosh fdup fdup f* 1e0 f- fsqrt f+ fln ;
104 :     \ : fasinh fdup fdup f* 1e0 f+ fsqrt f/ fatanh ;
105 : pazsan 1.9
106 : pazsan 1.11 : f~ ( f1 f2 f3 -- flag ) fdup f0= IF fdrop f= EXIT THEN
107 :     fdup f0> IF frot frot f- fabs fswap
108 :     ELSE fnegate frot frot fover fabs fover fabs f+ frot frot
109 :     f- fabs frot frot f* THEN f< ;
110 :    
111 : pazsan 1.9 : f.s ." <" fdepth 0 .r ." > " fdepth 0 max maxdepth-.s @ min dup 0
112 :     ?DO dup i - 1- floats fp@ + f@ f. LOOP drop ;

CVS Admin

Powered by ViewCVS 1.0-dev
(Powered by ViewCVS)

ViewCVS and CVS Help