1: \ High level floating point 14jan94py
2:
3: \ 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: \ 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:
33: \ 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:
56: : f, ( f -- ) here 1 floats allot f! ;
57:
58: : fconstant ( r "name" -- ) \ float
59: Create f,
60: DOES> ( -- r )
61: f@ ;
62:
63: : fdepth ( -- n ) fp0 @ fp@ - [ 1 floats ] Literal / ;
64:
65: : 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:
70: &15 Value precision
71: : 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: : 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: 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: \ 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:
93: : fe. ( r -- ) f$ 1- s>d 3 fm/mod 3 * >r 1+ >r
94: scratch r@ min type '. emit scratch r> /string type
95: 'E emit r> . ;
96:
97: : fs. ( r -- ) f$ 1-
98: scratch over c@ emit '. emit 1 /string type
99: 'E emit . ;
100:
101: require debugs.fs
102:
103: : sfnumber ( c-addr u -- r true | false )
104: 2dup [CHAR] e scan ( c-addr u c-addr2 u2 )
105: dup 0=
106: IF
107: 2drop 2dup [CHAR] E scan ( c-addr u c-addr3 u3 )
108: THEN
109: nip
110: IF
111: >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:
125: :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:
134: : fvariable ( "name" -- ) \ float
135: Create 0.0E0 f, ;
136: \ does> ( -- f-addr )
137:
138: 1.0e0 fasin 2.0e0 f* fconstant pi
139:
140: : f2* 2.0e0 f* ;
141: : f2/ 0.5e0 f* ;
142: : 1/f 1.0e0 fswap f/ ;
143:
144:
145: \ We now have primitives for these, so we need not define them
146:
147: \ : falog ( f -- 10^f ) [ 10.0e0 fln ] FLiteral f* fexp ;
148:
149: \ : fsinh fexpm1 fdup fdup 1.0e0 f+ f/ f+ f2/ ;
150: \ : fcosh fexp fdup 1/f f+ f2/ ;
151: \ : ftanh f2* fexpm1 fdup 2.0e0 f+ f/ ;
152:
153: \ : fatanh fdup f0< >r fabs 1.0e0 fover f- f/ f2* flnp1 f2/
154: \ r> IF fnegate THEN ;
155: \ : facosh fdup fdup f* 1.0e0 f- fsqrt f+ fln ;
156: \ : fasinh fdup fdup f* 1.0e0 f+ fsqrt f/ fatanh ;
157:
158: \ !! 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:
173: : f.s ." <" fdepth 0 .r ." > " fdepth 0 max maxdepth-.s @ min dup 0
174: ?DO dup i - 1- floats fp@ + f@ f. LOOP drop ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>