Annotation of gforth/float.fs, revision 1.66
1.1 anton 1: \ High level floating point 14jan94py
2:
1.64 anton 3: \ Copyright (C) 1995,1997,2003,2004,2005,2006,2007,2009,2010,2011 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
1.53 anton 9: \ as published by the Free Software Foundation, either version 3
1.18 anton 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
1.53 anton 18: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.18 anton 19:
1.17 anton 20: \ 1 cells 4 = [IF]
21: \ ' cells Alias sfloats
22: \ ' cell+ Alias sfloat+
23: \ ' align Alias sfalign
24: \ ' aligned Alias sfaligned
25: \ [ELSE]
26: \ : sfloats 2* 2* ;
27: \ : sfloat+ 4 + ;
28: \ : sfaligned ( addr -- addr' ) 3 + -4 and ;
29: \ : sfalign ( -- ) here dup sfaligned swap ?DO bl c, LOOP ;
30: \ [THEN]
1.6 pazsan 31:
1.17 anton 32: \ 1 floats 8 = [IF]
33: \ ' floats Alias dfloats
34: \ ' float+ Alias dfloat+
35: \ ' falign Alias dfalign
36: \ ' faligned Alias dfaligned
37: \ [ELSE]
38: \ : dfloats 2* 2* 2* ;
39: \ : dfloat+ 8 + ;
40: \ : dfaligned ( addr -- addr' ) 7 + -8 and ;
41: \ : dfalign ( -- ) here dup dfaligned swap ?DO bl c, LOOP ;
42: \ [THEN]
43:
44: : sfalign ( -- ) \ float-ext s-f-align
1.26 crook 45: \G If the data-space pointer is not single-float-aligned, reserve
46: \G enough space to align it.
1.17 anton 47: here dup sfaligned swap ?DO bl c, LOOP ;
48: : dfalign ( -- ) \ float-ext d-f-align
1.26 crook 49: \G If the data-space pointer is not double-float-aligned, reserve
50: \G enough space to align it.
1.17 anton 51: here dup dfaligned swap ?DO bl c, LOOP ;
52:
1.51 anton 53: (Field) sfloat+ ( sf-addr1 -- sf-addr2 ) \ float-ext s-float-plus
1.31 anton 54: \G @code{1 sfloats +}.
1.51 anton 55: 1 sfloats ,
1.17 anton 56:
1.51 anton 57: (Field) dfloat+ ( df-addr1 -- df-addr2 ) \ float-ext d-float-plus
1.31 anton 58: \G @code{1 dfloats +}.
1.51 anton 59: 1 dfloats ,
60:
1.26 crook 61: : f, ( f -- ) \ gforth
62: \G Reserve data space for one floating-point number and store
63: \G @i{f} in the space.
64: here 1 floats allot f! ;
1.1 anton 65:
1.29 crook 66: : fconstant ( r "name" -- ) \ float f-constant
1.13 anton 67: Create f,
1.16 anton 68: DOES> ( -- r )
69: f@ ;
1.1 anton 70:
1.29 crook 71: : fdepth ( -- +n ) \ float f-depth
1.28 crook 72: \G @i{+n} is the current number of (floating-point) values on the
1.24 crook 73: \G floating-point stack.
74: fp0 @ fp@ - [ 1 floats ] Literal / ;
1.1 anton 75:
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.
1.39 anton 79: BEGIN here cell+ cell+ dup faligned <> WHILE postpone noop REPEAT
80: postpone ahead here >r f, postpone then
81: r> postpone literal postpone f@ ; immediate
1.24 crook 82:
1.29 crook 83: &15 Value precision ( -- u ) \ float-ext
1.28 crook 84: \G @i{u} is the number of significant digits currently used by
1.24 crook 85: \G @code{F.} @code{FE.} and @code{FS.}
1.29 crook 86: : set-precision ( u -- ) \ float-ext
1.24 crook 87: \G Set the number of significant digits currently used by
1.28 crook 88: \G @code{F.} @code{FE.} and @code{FS.} to @i{u}.
1.24 crook 89: to precision ;
1.1 anton 90:
1.62 pazsan 91: : scratch ( -- addr len )
1.1 anton 92: pad precision - precision ;
93:
94: : zeros ( n -- ) 0 max 0 ?DO '0 emit LOOP ;
95:
96: : -zeros ( addr u -- addr' u' )
97: BEGIN dup WHILE 1- 2dup + c@ '0 <> UNTIL 1+ THEN ;
98:
1.4 pazsan 99: : f$ ( f -- n ) scratch represent 0=
100: IF 2drop scratch 3 min type rdrop EXIT THEN
101: IF '- emit THEN ;
102:
1.29 crook 103: : f. ( r -- ) \ float-ext f-dot
1.32 anton 104: \G Display (the floating-point number) @i{r} without exponent,
1.24 crook 105: \G followed by a space.
1.35 anton 106: f$ dup >r 0<=
1.4 pazsan 107: IF '0 emit
1.1 anton 108: ELSE scratch r@ min type r@ precision - zeros THEN
109: '. emit r@ negate zeros
110: scratch r> 0 max /string 0 max -zeros type space ;
1.3 benschop 111: \ I'm afraid this does not really implement ansi semantics wrt precision.
112: \ Shouldn't precision indicate the number of places shown after the point?
1.1 anton 113:
1.33 anton 114: \ Why do you think so? ANS Forth appears ambiguous on this point. -anton.
115:
1.29 crook 116: : fe. ( r -- ) \ float-ext f-e-dot
1.32 anton 117: \G Display @i{r} using engineering notation (with exponent dividable
118: \G by 3), followed by a space.
1.24 crook 119: f$ 1- s>d 3 fm/mod 3 * >r 1+ >r
1.33 anton 120: scratch r@ tuck min tuck - >r type r> zeros
121: '. emit scratch r> /string type
1.1 anton 122: 'E emit r> . ;
123:
1.29 crook 124: : fs. ( r -- ) \ float-ext f-s-dot
1.32 anton 125: \G Display @i{r} using scientific notation (with exponent), followed
126: \G by a space.
1.24 crook 127: f$ 1-
1.4 pazsan 128: scratch over c@ emit '. emit 1 /string type
129: 'E emit . ;
130:
1.65 pazsan 131: [IFDEF] fp-char
1.19 anton 132: : sfnumber ( c-addr u -- r true | false )
1.65 pazsan 133: fp-char @ >float1 ;
1.66 ! pazsan 134:
! 135: Create si-prefixes ," PTGMk.munpf"
! 136: si-prefixes count '.' scan drop Constant zero-exp
! 137:
! 138: : prefix-number ( c-addr u -- r true | false )
! 139: si-prefixes count bounds DO
! 140: 2dup I c@ scan nip 0<> IF
! 141: I c@ >float1
! 142: dup IF 1000 s>f zero-exp I - s>f f** f* THEN
! 143: UNLOOP EXIT THEN
! 144: LOOP
! 145: sfnumber ;
1.65 pazsan 146: [ELSE]
147: : sfnumber ( c-addr u -- r true | false )
148: >float ;
1.66 ! pazsan 149: : prefix-number sfnumber ;
1.65 pazsan 150: [THEN]
1.19 anton 151:
1.59 pazsan 152: [ifdef] recognizer:
1.63 pazsan 153: [IFDEF] 2lit,
154: : flit, postpone Fliteral ;
155: :noname ['] noop ;
156: :noname ['] flit, ;
157: [ELSE]
158: ' noop
159: :noname postpone Fliteral ;
160: [THEN]
1.59 pazsan 161: dup
162: recognizer: r:fnumber
163:
1.61 pazsan 164: : fnum-recognizer ( addr u -- float int-table | addr u r:fail )
1.66 ! pazsan 165: 2dup prefix-number
1.61 pazsan 166: IF
167: 2drop r:fnumber EXIT
168: THEN
169: r:fail ;
1.59 pazsan 170:
1.60 pazsan 171: ' fnum-recognizer
1.59 pazsan 172: forth-recognizer get-recognizers
173: 1+ forth-recognizer set-recognizers
174: [else]
1.47 anton 175: [ifundef] compiler-notfound1
176: defer compiler-notfound1
177: ' no.extensions IS compiler-notfound1
178:
179: :noname compiler-notfound1 execute ; is compiler-notfound
180:
181: defer interpreter-notfound1
182: ' no.extensions IS interpreter-notfound1
183:
184: :noname interpreter-notfound1 execute ; is interpreter-notfound
185: [then]
186:
187: :noname ( c-addr u -- ... xt )
1.19 anton 188: 2dup sfnumber
189: IF
1.47 anton 190: 2drop [comp'] FLiteral
1.19 anton 191: ELSE
1.47 anton 192: defers compiler-notfound1
1.19 anton 193: ENDIF ;
1.47 anton 194: IS compiler-notfound1
1.1 anton 195:
1.47 anton 196: :noname ( c-addr u -- ... xt )
1.19 anton 197: 2dup sfnumber
198: IF
1.47 anton 199: 2drop ['] noop
1.19 anton 200: ELSE
1.47 anton 201: defers interpreter-notfound1
1.19 anton 202: ENDIF ;
1.47 anton 203: IS interpreter-notfound1
1.59 pazsan 204: [then]
1.13 anton 205:
1.29 crook 206: : fvariable ( "name" -- ) \ float f-variable
1.15 pazsan 207: Create 0.0E0 f, ;
1.13 anton 208: \ does> ( -- f-addr )
1.1 anton 209:
1.24 crook 210: 1.0e0 fasin 2.0e0 f* fconstant pi ( -- r ) \ gforth
1.28 crook 211: \G @code{Fconstant} -- @i{r} is the value pi; the ratio of a circle's area
1.24 crook 212: \G to its diameter.
213:
214: : f2* ( r1 -- r2 ) \ gforth
1.28 crook 215: \G Multiply @i{r1} by 2.0e0
1.24 crook 216: 2.0e0 f* ;
217:
218: : f2/ ( r1 -- r2 ) \ gforth
1.28 crook 219: \G Multiply @i{r1} by 0.5e0
1.24 crook 220: 0.5e0 f* ;
221:
222: : 1/f ( r1 -- r2 ) \ gforth
1.28 crook 223: \G Divide 1.0e0 by @i{r1}.
1.24 crook 224: 1.0e0 fswap f/ ;
1.6 pazsan 225:
1.36 pazsan 226: get-current environment-wordlist set-current
227: 1.7976931348623157e308 FConstant max-float
228: set-current
1.6 pazsan 229:
1.10 anton 230: \ We now have primitives for these, so we need not define them
1.6 pazsan 231:
1.15 pazsan 232: \ : falog ( f -- 10^f ) [ 10.0e0 fln ] FLiteral f* fexp ;
1.10 anton 233:
1.15 pazsan 234: \ : fsinh fexpm1 fdup fdup 1.0e0 f+ f/ f+ f2/ ;
1.10 anton 235: \ : fcosh fexp fdup 1/f f+ f2/ ;
1.15 pazsan 236: \ : ftanh f2* fexpm1 fdup 2.0e0 f+ f/ ;
1.10 anton 237:
1.15 pazsan 238: \ : fatanh fdup f0< >r fabs 1.0e0 fover f- f/ f2* flnp1 f2/
1.10 anton 239: \ r> IF fnegate THEN ;
1.15 pazsan 240: \ : facosh fdup fdup f* 1.0e0 f- fsqrt f+ fln ;
241: \ : fasinh fdup fdup f* 1.0e0 f+ fsqrt f/ fatanh ;
1.9 pazsan 242:
1.27 anton 243: : f~abs ( r1 r2 r3 -- flag ) \ gforth
244: \G Approximate equality with absolute error: |r1-r2|<r3.
245: frot frot f- fabs fswap f< ;
246:
247: : f~rel ( r1 r2 r3 -- flag ) \ gforth
248: \G Approximate equality with relative error: |r1-r2|<r3*|r1+r2|.
249: frot frot fover fabs fover fabs f+ frot frot
250: f- fabs frot frot f* f< ;
251:
1.29 crook 252: : f~ ( r1 r2 r3 -- flag ) \ float-ext f-proximate
1.31 anton 253: \G ANS Forth medley for comparing r1 and r2 for equality: r3>0:
254: \G @code{f~abs}; r3=0: bitwise comparison; r3<0: @code{fnegate f~rel}.
1.16 anton 255: fdup f0=
1.31 anton 256: IF \ bitwise comparison
1.41 anton 257: fp@ float+ 1 floats over float+ over str=
1.31 anton 258: fdrop fdrop fdrop
1.27 anton 259: EXIT
1.16 anton 260: THEN
261: fdup f0>
262: IF
1.27 anton 263: f~abs
1.16 anton 264: ELSE
1.27 anton 265: fnegate f~rel
266: THEN ;
1.44 anton 267:
1.57 anton 268: -0e 8 0 [do] fp@ [i] + c@ $80 = [if] [i] constant fsign-offset [then] [loop]
1.54 anton 269:
270: : fcopysign ( r1 r2 -- r3 ) \ gforth
271: \G r3 takes its absolute value from r1 and its sign from r2
272: \ !! implementation relies on IEEE DP format
1.55 anton 273: fp@ fsign-offset + dup c@ $80 and >r ( r1 r2 addr-r1sign )
274: float+ dup c@ $7f and r> or swap c!
1.54 anton 275: fdrop ;
276:
1.44 anton 277: \ proposals from Krishna Myeni in <cjsp2d$47l$1@ngspool-d02.news.aol.com>
278: \ not sure if they are a good idea
279:
1.54 anton 280: : ftrunc ( r1 -- r2 ) \ X:ftrunc
1.44 anton 281: \ round towards 0
1.54 anton 282: fdup fabs floor fswap fcopysign ;
1.44 anton 283:
284: : FMOD ( r1 r2 -- r )
285: \ remainder of r1/r2
286: FOVER FOVER F/ ftrunc F* F- ;
1.54 anton 287:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>