Annotation of gforth/float.fs, revision 1.51
1.1 anton 1: \ High level floating point 14jan94py
2:
1.50 anton 3: \ Copyright (C) 1995,1997,2003,2004,2005,2006 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
1.34 anton 19: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
1.18 anton 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.51 ! anton 54: (Field) sfloat+ ( sf-addr1 -- sf-addr2 ) \ float-ext s-float-plus
1.31 anton 55: \G @code{1 sfloats +}.
1.51 ! anton 56: 1 sfloats ,
1.17 anton 57:
1.51 ! anton 58: (Field) dfloat+ ( df-addr1 -- df-addr2 ) \ float-ext d-float-plus
1.31 anton 59: \G @code{1 dfloats +}.
1.51 ! anton 60: 1 dfloats ,
! 61:
1.26 crook 62: : f, ( f -- ) \ gforth
63: \G Reserve data space for one floating-point number and store
64: \G @i{f} in the space.
65: here 1 floats allot f! ;
1.1 anton 66:
1.29 crook 67: : fconstant ( r "name" -- ) \ float f-constant
1.13 anton 68: Create f,
1.16 anton 69: DOES> ( -- r )
70: f@ ;
1.1 anton 71:
1.29 crook 72: : fdepth ( -- +n ) \ float f-depth
1.28 crook 73: \G @i{+n} is the current number of (floating-point) values on the
1.24 crook 74: \G floating-point stack.
75: fp0 @ fp@ - [ 1 floats ] Literal / ;
1.1 anton 76:
1.29 crook 77: : FLiteral ( compilation r -- ; run-time -- r ) \ float f-literal
1.28 crook 78: \G Compile appropriate code such that, at run-time, @i{r} is placed
1.24 crook 79: \G on the (floating-point) stack. Interpretation semantics are undefined.
1.39 anton 80: BEGIN here cell+ cell+ dup faligned <> WHILE postpone noop REPEAT
81: postpone ahead here >r f, postpone then
82: r> postpone literal postpone f@ ; immediate
1.24 crook 83:
1.29 crook 84: &15 Value precision ( -- u ) \ float-ext
1.28 crook 85: \G @i{u} is the number of significant digits currently used by
1.24 crook 86: \G @code{F.} @code{FE.} and @code{FS.}
1.29 crook 87: : set-precision ( u -- ) \ float-ext
1.24 crook 88: \G Set the number of significant digits currently used by
1.28 crook 89: \G @code{F.} @code{FE.} and @code{FS.} to @i{u}.
1.24 crook 90: to precision ;
1.1 anton 91:
92: : scratch ( r -- addr len )
93: pad precision - precision ;
94:
95: : zeros ( n -- ) 0 max 0 ?DO '0 emit LOOP ;
96:
97: : -zeros ( addr u -- addr' u' )
98: BEGIN dup WHILE 1- 2dup + c@ '0 <> UNTIL 1+ THEN ;
99:
1.4 pazsan 100: : f$ ( f -- n ) scratch represent 0=
101: IF 2drop scratch 3 min type rdrop EXIT THEN
102: IF '- emit THEN ;
103:
1.29 crook 104: : f. ( r -- ) \ float-ext f-dot
1.32 anton 105: \G Display (the floating-point number) @i{r} without exponent,
1.24 crook 106: \G followed by a space.
1.35 anton 107: f$ dup >r 0<=
1.4 pazsan 108: IF '0 emit
1.1 anton 109: ELSE scratch r@ min type r@ precision - zeros THEN
110: '. emit r@ negate zeros
111: scratch r> 0 max /string 0 max -zeros type space ;
1.3 benschop 112: \ I'm afraid this does not really implement ansi semantics wrt precision.
113: \ Shouldn't precision indicate the number of places shown after the point?
1.1 anton 114:
1.33 anton 115: \ Why do you think so? ANS Forth appears ambiguous on this point. -anton.
116:
1.29 crook 117: : fe. ( r -- ) \ float-ext f-e-dot
1.32 anton 118: \G Display @i{r} using engineering notation (with exponent dividable
119: \G by 3), followed by a space.
1.24 crook 120: f$ 1- s>d 3 fm/mod 3 * >r 1+ >r
1.33 anton 121: scratch r@ tuck min tuck - >r type r> zeros
122: '. emit scratch r> /string type
1.1 anton 123: 'E emit r> . ;
124:
1.29 crook 125: : fs. ( r -- ) \ float-ext f-s-dot
1.32 anton 126: \G Display @i{r} using scientific notation (with exponent), followed
127: \G by a space.
1.24 crook 128: f$ 1-
1.4 pazsan 129: scratch over c@ emit '. emit 1 /string type
130: 'E emit . ;
131:
1.19 anton 132: : sfnumber ( c-addr u -- r true | false )
133: 2dup [CHAR] e scan ( c-addr u c-addr2 u2 )
1.16 anton 134: dup 0=
135: IF
1.19 anton 136: 2drop 2dup [CHAR] E scan ( c-addr u c-addr3 u3 )
1.16 anton 137: THEN
138: nip
1.7 anton 139: IF
1.19 anton 140: >float
141: ELSE
142: 2drop false
143: THEN ;
144:
1.47 anton 145: [ifundef] compiler-notfound1
146: defer compiler-notfound1
147: ' no.extensions IS compiler-notfound1
148:
149: :noname compiler-notfound1 execute ; is compiler-notfound
150:
151: defer interpreter-notfound1
152: ' no.extensions IS interpreter-notfound1
153:
154: :noname interpreter-notfound1 execute ; is interpreter-notfound
155: [then]
156:
157: :noname ( c-addr u -- ... xt )
1.19 anton 158: 2dup sfnumber
159: IF
1.47 anton 160: 2drop [comp'] FLiteral
1.19 anton 161: ELSE
1.47 anton 162: defers compiler-notfound1
1.19 anton 163: ENDIF ;
1.47 anton 164: IS compiler-notfound1
1.1 anton 165:
1.47 anton 166: :noname ( c-addr u -- ... xt )
1.19 anton 167: 2dup sfnumber
168: IF
1.47 anton 169: 2drop ['] noop
1.19 anton 170: ELSE
1.47 anton 171: defers interpreter-notfound1
1.19 anton 172: ENDIF ;
1.47 anton 173: IS interpreter-notfound1
1.13 anton 174:
1.29 crook 175: : fvariable ( "name" -- ) \ float f-variable
1.15 pazsan 176: Create 0.0E0 f, ;
1.13 anton 177: \ does> ( -- f-addr )
1.1 anton 178:
1.24 crook 179: 1.0e0 fasin 2.0e0 f* fconstant pi ( -- r ) \ gforth
1.28 crook 180: \G @code{Fconstant} -- @i{r} is the value pi; the ratio of a circle's area
1.24 crook 181: \G to its diameter.
182:
183: : f2* ( r1 -- r2 ) \ gforth
1.28 crook 184: \G Multiply @i{r1} by 2.0e0
1.24 crook 185: 2.0e0 f* ;
186:
187: : f2/ ( r1 -- r2 ) \ gforth
1.28 crook 188: \G Multiply @i{r1} by 0.5e0
1.24 crook 189: 0.5e0 f* ;
190:
191: : 1/f ( r1 -- r2 ) \ gforth
1.28 crook 192: \G Divide 1.0e0 by @i{r1}.
1.24 crook 193: 1.0e0 fswap f/ ;
1.6 pazsan 194:
1.36 pazsan 195: get-current environment-wordlist set-current
196: 1.7976931348623157e308 FConstant max-float
197: set-current
1.6 pazsan 198:
1.10 anton 199: \ We now have primitives for these, so we need not define them
1.6 pazsan 200:
1.15 pazsan 201: \ : falog ( f -- 10^f ) [ 10.0e0 fln ] FLiteral f* fexp ;
1.10 anton 202:
1.15 pazsan 203: \ : fsinh fexpm1 fdup fdup 1.0e0 f+ f/ f+ f2/ ;
1.10 anton 204: \ : fcosh fexp fdup 1/f f+ f2/ ;
1.15 pazsan 205: \ : ftanh f2* fexpm1 fdup 2.0e0 f+ f/ ;
1.10 anton 206:
1.15 pazsan 207: \ : fatanh fdup f0< >r fabs 1.0e0 fover f- f/ f2* flnp1 f2/
1.10 anton 208: \ r> IF fnegate THEN ;
1.15 pazsan 209: \ : facosh fdup fdup f* 1.0e0 f- fsqrt f+ fln ;
210: \ : fasinh fdup fdup f* 1.0e0 f+ fsqrt f/ fatanh ;
1.9 pazsan 211:
1.27 anton 212: : f~abs ( r1 r2 r3 -- flag ) \ gforth
213: \G Approximate equality with absolute error: |r1-r2|<r3.
214: frot frot f- fabs fswap f< ;
215:
216: : f~rel ( r1 r2 r3 -- flag ) \ gforth
217: \G Approximate equality with relative error: |r1-r2|<r3*|r1+r2|.
218: frot frot fover fabs fover fabs f+ frot frot
219: f- fabs frot frot f* f< ;
220:
1.29 crook 221: : f~ ( r1 r2 r3 -- flag ) \ float-ext f-proximate
1.31 anton 222: \G ANS Forth medley for comparing r1 and r2 for equality: r3>0:
223: \G @code{f~abs}; r3=0: bitwise comparison; r3<0: @code{fnegate f~rel}.
1.16 anton 224: fdup f0=
1.31 anton 225: IF \ bitwise comparison
1.41 anton 226: fp@ float+ 1 floats over float+ over str=
1.31 anton 227: fdrop fdrop fdrop
1.27 anton 228: EXIT
1.16 anton 229: THEN
230: fdup f0>
231: IF
1.27 anton 232: f~abs
1.16 anton 233: ELSE
1.27 anton 234: fnegate f~rel
235: THEN ;
1.44 anton 236:
237: \ proposals from Krishna Myeni in <cjsp2d$47l$1@ngspool-d02.news.aol.com>
238: \ not sure if they are a good idea
239:
240: : FTRUNC ( r1 -- r2 )
241: \ round towards 0
242: \ !! should be implemented properly
243: F>D D>F ;
244:
245: : FMOD ( r1 r2 -- r )
246: \ remainder of r1/r2
247: FOVER FOVER F/ ftrunc F* F- ;
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>