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