[gforth] / gforth / float.fs  

gforth: gforth/float.fs


1 : anton 1.1 \ High level floating point 14jan94py
2 :    
3 : anton 1.23 \ Copyright (C) 1995,1997 Free Software Foundation, Inc.
4 : anton 1.18
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 : crook 1.26 \G If the data-space pointer is not single-float-aligned, reserve
47 :     \G enough space to align it.
48 : anton 1.17 here dup sfaligned swap ?DO bl c, LOOP ;
49 :     : dfalign ( -- ) \ float-ext d-f-align
50 : crook 1.26 \G If the data-space pointer is not double-float-aligned, reserve
51 :     \G enough space to align it.
52 : anton 1.17 here dup dfaligned swap ?DO bl c, LOOP ;
53 :    
54 : pazsan 1.30 1 sfloats (Field) sfloat+ , ( sf-addr1 -- sf-addr2 ) \ float-ext s-float-plus
55 : anton 1.31 \G @code{1 sfloats +}.
56 : anton 1.17
57 : pazsan 1.30 1 dfloats (Field) dfloat+ , ( df-addr1 -- df-addr2 ) \ float-ext d-float-plus
58 : anton 1.31 \G @code{1 dfloats +}.
59 : pazsan 1.6
60 : crook 1.26 : f, ( f -- ) \ gforth
61 :     \G Reserve data space for one floating-point number and store
62 :     \G @i{f} in the space.
63 :     here 1 floats allot f! ;
64 : anton 1.1
65 : crook 1.29 : fconstant ( r "name" -- ) \ float f-constant
66 : anton 1.13 Create f,
67 : anton 1.16 DOES> ( -- r )
68 :     f@ ;
69 : anton 1.1
70 : crook 1.29 : fdepth ( -- +n ) \ float f-depth
71 : crook 1.28 \G @i{+n} is the current number of (floating-point) values on the
72 : crook 1.24 \G floating-point stack.
73 :     fp0 @ fp@ - [ 1 floats ] Literal / ;
74 : anton 1.1
75 : pazsan 1.14 : FLit ( -- r ) r> dup f@ float+ >r ;
76 : crook 1.29 : FLiteral ( compilation r -- ; run-time -- r ) \ float f-literal
77 : crook 1.28 \G Compile appropriate code such that, at run-time, @i{r} is placed
78 : crook 1.24 \G on the (floating-point) stack. Interpretation semantics are undefined.
79 :     BEGIN here cell+ dup faligned <> WHILE postpone noop REPEAT
80 :     postpone FLit f, ; immediate
81 :    
82 : crook 1.29 &15 Value precision ( -- u ) \ float-ext
83 : crook 1.28 \G @i{u} is the number of significant digits currently used by
84 : crook 1.24 \G @code{F.} @code{FE.} and @code{FS.}
85 : crook 1.29 : set-precision ( u -- ) \ float-ext
86 : crook 1.24 \G Set the number of significant digits currently used by
87 : crook 1.28 \G @code{F.} @code{FE.} and @code{FS.} to @i{u}.
88 : crook 1.24 to precision ;
89 : anton 1.1
90 :     : scratch ( r -- addr len )
91 :     pad precision - precision ;
92 :    
93 :     : zeros ( n -- ) 0 max 0 ?DO '0 emit LOOP ;
94 :    
95 :     : -zeros ( addr u -- addr' u' )
96 :     BEGIN dup WHILE 1- 2dup + c@ '0 <> UNTIL 1+ THEN ;
97 :    
98 : pazsan 1.4 : f$ ( f -- n ) scratch represent 0=
99 :     IF 2drop scratch 3 min type rdrop EXIT THEN
100 :     IF '- emit THEN ;
101 :    
102 : crook 1.29 : f. ( r -- ) \ float-ext f-dot
103 : anton 1.32 \G Display (the floating-point number) @i{r} without exponent,
104 : crook 1.24 \G followed by a space.
105 :     f$ dup >r 0<
106 : pazsan 1.4 IF '0 emit
107 : anton 1.1 ELSE scratch r@ min type r@ precision - zeros THEN
108 :     '. emit r@ negate zeros
109 :     scratch r> 0 max /string 0 max -zeros type space ;
110 : benschop 1.3 \ I'm afraid this does not really implement ansi semantics wrt precision.
111 :     \ Shouldn't precision indicate the number of places shown after the point?
112 : anton 1.1
113 : crook 1.29 : fe. ( r -- ) \ float-ext f-e-dot
114 : anton 1.32 \G Display @i{r} using engineering notation (with exponent dividable
115 :     \G by 3), followed by a space.
116 : crook 1.24 f$ 1- s>d 3 fm/mod 3 * >r 1+ >r
117 : anton 1.32 scratch r@ min type '. emit scratch r> /string type
118 : anton 1.1 'E emit r> . ;
119 :    
120 : crook 1.29 : fs. ( r -- ) \ float-ext f-s-dot
121 : anton 1.32 \G Display @i{r} using scientific notation (with exponent), followed
122 :     \G by a space.
123 : crook 1.24 f$ 1-
124 : pazsan 1.4 scratch over c@ emit '. emit 1 /string type
125 :     'E emit . ;
126 :    
127 : anton 1.21 require debugs.fs
128 : anton 1.16
129 : anton 1.19 : sfnumber ( c-addr u -- r true | false )
130 :     2dup [CHAR] e scan ( c-addr u c-addr2 u2 )
131 : anton 1.16 dup 0=
132 :     IF
133 : anton 1.19 2drop 2dup [CHAR] E scan ( c-addr u c-addr3 u3 )
134 : anton 1.16 THEN
135 :     nip
136 : anton 1.7 IF
137 : anton 1.19 >float
138 :     ELSE
139 :     2drop false
140 :     THEN ;
141 :    
142 :     :noname ( c-addr u -- )
143 :     2dup sfnumber
144 :     IF
145 :     2drop POSTPONE FLiteral
146 :     ELSE
147 :     defers compiler-notfound
148 :     ENDIF ;
149 :     IS compiler-notfound
150 : anton 1.1
151 : anton 1.19 :noname ( c-addr u -- r )
152 :     2dup sfnumber
153 :     IF
154 :     2drop
155 :     ELSE
156 :     defers interpreter-notfound
157 :     ENDIF ;
158 :     IS interpreter-notfound
159 : anton 1.13
160 : crook 1.29 : fvariable ( "name" -- ) \ float f-variable
161 : pazsan 1.15 Create 0.0E0 f, ;
162 : anton 1.13 \ does> ( -- f-addr )
163 : anton 1.1
164 : crook 1.24 1.0e0 fasin 2.0e0 f* fconstant pi ( -- r ) \ gforth
165 : crook 1.28 \G @code{Fconstant} -- @i{r} is the value pi; the ratio of a circle's area
166 : crook 1.24 \G to its diameter.
167 :    
168 :     : f2* ( r1 -- r2 ) \ gforth
169 : crook 1.28 \G Multiply @i{r1} by 2.0e0
170 : crook 1.24 2.0e0 f* ;
171 :    
172 :     : f2/ ( r1 -- r2 ) \ gforth
173 : crook 1.28 \G Multiply @i{r1} by 0.5e0
174 : crook 1.24 0.5e0 f* ;
175 :    
176 :     : 1/f ( r1 -- r2 ) \ gforth
177 : crook 1.28 \G Divide 1.0e0 by @i{r1}.
178 : crook 1.24 1.0e0 fswap f/ ;
179 : pazsan 1.6
180 :    
181 : anton 1.10 \ We now have primitives for these, so we need not define them
182 : pazsan 1.6
183 : pazsan 1.15 \ : falog ( f -- 10^f ) [ 10.0e0 fln ] FLiteral f* fexp ;
184 : anton 1.10
185 : pazsan 1.15 \ : fsinh fexpm1 fdup fdup 1.0e0 f+ f/ f+ f2/ ;
186 : anton 1.10 \ : fcosh fexp fdup 1/f f+ f2/ ;
187 : pazsan 1.15 \ : ftanh f2* fexpm1 fdup 2.0e0 f+ f/ ;
188 : anton 1.10
189 : pazsan 1.15 \ : fatanh fdup f0< >r fabs 1.0e0 fover f- f/ f2* flnp1 f2/
190 : anton 1.10 \ r> IF fnegate THEN ;
191 : pazsan 1.15 \ : facosh fdup fdup f* 1.0e0 f- fsqrt f+ fln ;
192 :     \ : fasinh fdup fdup f* 1.0e0 f+ fsqrt f/ fatanh ;
193 : pazsan 1.9
194 : anton 1.27 : f~abs ( r1 r2 r3 -- flag ) \ gforth
195 :     \G Approximate equality with absolute error: |r1-r2|<r3.
196 :     frot frot f- fabs fswap f< ;
197 :    
198 :     : f~rel ( r1 r2 r3 -- flag ) \ gforth
199 :     \G Approximate equality with relative error: |r1-r2|<r3*|r1+r2|.
200 :     frot frot fover fabs fover fabs f+ frot frot
201 :     f- fabs frot frot f* f< ;
202 :    
203 : crook 1.29 : f~ ( r1 r2 r3 -- flag ) \ float-ext f-proximate
204 : anton 1.31 \G ANS Forth medley for comparing r1 and r2 for equality: r3>0:
205 :     \G @code{f~abs}; r3=0: bitwise comparison; r3<0: @code{fnegate f~rel}.
206 : anton 1.16 fdup f0=
207 : anton 1.31 IF \ bitwise comparison
208 :     fp@ float+ 1 floats over float+ -text 0=
209 :     fdrop fdrop fdrop
210 : anton 1.27 EXIT
211 : anton 1.16 THEN
212 :     fdup f0>
213 :     IF
214 : anton 1.27 f~abs
215 : anton 1.16 ELSE
216 : anton 1.27 fnegate f~rel
217 :     THEN ;
218 : pazsan 1.11
219 : crook 1.24 : f.s ( -- ) \ gforth f-dot-s
220 :     \G Display the number of items on the floating-point stack,
221 :     \G followed by a list of the items; TOS is the right-most item.
222 :     ." <" fdepth 0 .r ." > " fdepth 0 max maxdepth-.s @ min dup 0
223 :     ?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