[gforth] / gforth / float.fs  

gforth: gforth/float.fs


1 : anton 1.1 \ High level floating point 14jan94py
2 :    
3 : anton 1.18 \ Copyright (C) 1995 Free Software Foundation, Inc.
4 :    
5 :     \ This file is part of Gforth.
6 :    
7 :     \ Gforth is free software; you can redistribute it and/or
8 :     \ modify it under the terms of the GNU General Public License
9 :     \ as published by the Free Software Foundation; either version 2
10 :     \ of the License, or (at your option) any later version.
11 :    
12 :     \ This program is distributed in the hope that it will be useful,
13 :     \ but WITHOUT ANY WARRANTY; without even the implied warranty of
14 :     \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 :     \ GNU General Public License for more details.
16 :    
17 :     \ You should have received a copy of the GNU General Public License
18 :     \ along with this program; if not, write to the Free Software
19 :     \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 :    
21 : anton 1.17 \ 1 cells 4 = [IF]
22 :     \ ' cells Alias sfloats
23 :     \ ' cell+ Alias sfloat+
24 :     \ ' align Alias sfalign
25 :     \ ' aligned Alias sfaligned
26 :     \ [ELSE]
27 :     \ : sfloats 2* 2* ;
28 :     \ : sfloat+ 4 + ;
29 :     \ : sfaligned ( addr -- addr' ) 3 + -4 and ;
30 :     \ : sfalign ( -- ) here dup sfaligned swap ?DO bl c, LOOP ;
31 :     \ [THEN]
32 : pazsan 1.6
33 : anton 1.17 \ 1 floats 8 = [IF]
34 :     \ ' floats Alias dfloats
35 :     \ ' float+ Alias dfloat+
36 :     \ ' falign Alias dfalign
37 :     \ ' faligned Alias dfaligned
38 :     \ [ELSE]
39 :     \ : dfloats 2* 2* 2* ;
40 :     \ : dfloat+ 8 + ;
41 :     \ : dfaligned ( addr -- addr' ) 7 + -8 and ;
42 :     \ : dfalign ( -- ) here dup dfaligned swap ?DO bl c, LOOP ;
43 :     \ [THEN]
44 :    
45 :     : sfalign ( -- ) \ float-ext s-f-align
46 :     here dup sfaligned swap ?DO bl c, LOOP ;
47 :     : dfalign ( -- ) \ float-ext d-f-align
48 :     here dup dfaligned swap ?DO bl c, LOOP ;
49 :    
50 :     1 sfloats constant sfloat+ ( sf-addr1 -- sf-addr2 ) \ float-ext s-float-plus
51 :     dofield: lastxt code-address! \ change the constant into a field
52 :    
53 :     1 dfloats constant dfloat+ ( df-addr1 -- df-addr2 ) \ float-ext d-float-plus
54 :     dofield: lastxt code-address! \ change the constant into a field
55 : pazsan 1.6
56 : anton 1.1 : f, ( f -- ) here 1 floats allot f! ;
57 :    
58 : anton 1.20 : fconstant ( r "name" -- ) \ float
59 : anton 1.13 Create f,
60 : anton 1.16 DOES> ( -- r )
61 :     f@ ;
62 : anton 1.1
63 :     : fdepth ( -- n ) f0 @ fp@ - [ 1 floats ] Literal / ;
64 :    
65 : pazsan 1.14 : FLit ( -- r ) r> dup f@ float+ >r ;
66 :     : FLiteral ( r -- )
67 :     BEGIN here cell+ dup faligned <> WHILE postpone noop REPEAT
68 :     postpone FLit f, ; immediate
69 : anton 1.1
70 : anton 1.13 &15 Value precision
71 : anton 1.1 : set-precision to precision ;
72 :    
73 :     : scratch ( r -- addr len )
74 :     pad precision - precision ;
75 :    
76 :     : zeros ( n -- ) 0 max 0 ?DO '0 emit LOOP ;
77 :    
78 :     : -zeros ( addr u -- addr' u' )
79 :     BEGIN dup WHILE 1- 2dup + c@ '0 <> UNTIL 1+ THEN ;
80 :    
81 : pazsan 1.4 : f$ ( f -- n ) scratch represent 0=
82 :     IF 2drop scratch 3 min type rdrop EXIT THEN
83 :     IF '- emit THEN ;
84 :    
85 :     : f. ( r -- ) f$ dup >r 0<
86 :     IF '0 emit
87 : anton 1.1 ELSE scratch r@ min type r@ precision - zeros THEN
88 :     '. emit r@ negate zeros
89 :     scratch r> 0 max /string 0 max -zeros type space ;
90 : benschop 1.3 \ I'm afraid this does not really implement ansi semantics wrt precision.
91 :     \ Shouldn't precision indicate the number of places shown after the point?
92 : anton 1.1
93 : pazsan 1.4 : fe. ( r -- ) f$ 1- s>d 3 fm/mod 3 * >r 1+ >r
94 : anton 1.1 scratch r@ min type '. emit scratch r> /string type
95 :     'E emit r> . ;
96 :    
97 : pazsan 1.4 : fs. ( r -- ) f$ 1-
98 :     scratch over c@ emit '. emit 1 /string type
99 :     'E emit . ;
100 :    
101 : anton 1.16 require debugging.fs
102 :    
103 : anton 1.19 : sfnumber ( c-addr u -- r true | false )
104 :     2dup [CHAR] e scan ( c-addr u c-addr2 u2 )
105 : anton 1.16 dup 0=
106 :     IF
107 : anton 1.19 2drop 2dup [CHAR] E scan ( c-addr u c-addr3 u3 )
108 : anton 1.16 THEN
109 :     nip
110 : anton 1.7 IF
111 : anton 1.19 >float
112 :     ELSE
113 :     2drop false
114 :     THEN ;
115 :    
116 :     :noname ( c-addr u -- )
117 :     2dup sfnumber
118 :     IF
119 :     2drop POSTPONE FLiteral
120 :     ELSE
121 :     defers compiler-notfound
122 :     ENDIF ;
123 :     IS compiler-notfound
124 : anton 1.1
125 : anton 1.19 :noname ( c-addr u -- r )
126 :     2dup sfnumber
127 :     IF
128 :     2drop
129 :     ELSE
130 :     defers interpreter-notfound
131 :     ENDIF ;
132 :     IS interpreter-notfound
133 : anton 1.13
134 : anton 1.20 : fvariable ( "name" -- ) \ float
135 : pazsan 1.15 Create 0.0E0 f, ;
136 : anton 1.13 \ does> ( -- f-addr )
137 : anton 1.1
138 : pazsan 1.15 1.0e0 fasin 2.0e0 f* fconstant pi
139 : pazsan 1.6
140 : pazsan 1.15 : f2* 2.0e0 f* ;
141 :     : f2/ 0.5e0 f* ;
142 :     : 1/f 1.0e0 fswap f/ ;
143 : pazsan 1.6
144 :    
145 : anton 1.10 \ We now have primitives for these, so we need not define them
146 : pazsan 1.6
147 : pazsan 1.15 \ : falog ( f -- 10^f ) [ 10.0e0 fln ] FLiteral f* fexp ;
148 : anton 1.10
149 : pazsan 1.15 \ : fsinh fexpm1 fdup fdup 1.0e0 f+ f/ f+ f2/ ;
150 : anton 1.10 \ : fcosh fexp fdup 1/f f+ f2/ ;
151 : pazsan 1.15 \ : ftanh f2* fexpm1 fdup 2.0e0 f+ f/ ;
152 : anton 1.10
153 : pazsan 1.15 \ : fatanh fdup f0< >r fabs 1.0e0 fover f- f/ f2* flnp1 f2/
154 : anton 1.10 \ r> IF fnegate THEN ;
155 : pazsan 1.15 \ : facosh fdup fdup f* 1.0e0 f- fsqrt f+ fln ;
156 :     \ : fasinh fdup fdup f* 1.0e0 f+ fsqrt f/ fatanh ;
157 : pazsan 1.9
158 : anton 1.16 \ !! factor out parts
159 :     : f~ ( f1 f2 f3 -- flag ) \ float-ext
160 :     fdup f0=
161 :     IF
162 :     fdrop f= EXIT
163 :     THEN
164 :     fdup f0>
165 :     IF
166 :     frot frot f- fabs fswap
167 :     ELSE
168 :     fnegate frot frot fover fabs fover fabs f+ frot frot
169 :     f- fabs frot frot f*
170 :     THEN
171 :     f< ;
172 : pazsan 1.11
173 : pazsan 1.9 : f.s ." <" fdepth 0 .r ." > " fdepth 0 max maxdepth-.s @ min dup 0
174 :     ?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