File:  [gforth] / gforth / float.fs
Revision 1.24: download - view: text, annotated - select for diffs
Wed Feb 3 00:10:20 1999 UTC (25 years, 1 month ago) by crook
Branches: MAIN
CVS tags: HEAD
New "docclean" target for makefile (removes glossary dependencies when
rebuilding documentation). Changes to .fs files and prim are restricted
to glossary (\G) additions for the documentation; this has necessitated
the addition of new white-space in places to stop the \G stuff from
obscuring the code. Many additions to doc/gforth.ds - new sections
added, a few things moved and some sections re-written slightly. There
are a set of things to tidy up before this rev. is suitable for
release, and those will be my highest priority. I have also used
"@comment TODO" to highlight other sections I plan to work on, and
added a set of comments at the start to indicate other things I plan
to modify in the medium-term.

    1: \ High level floating point                            14jan94py
    2: 
    3: \ Copyright (C) 1995,1997 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 ) \ floating f-depth
   64:     \G +n is the current number of (floating-point) values on the
   65:     \G floating-point stack.
   66:     fp0 @ fp@ - [ 1 floats ] Literal / ;
   67: 
   68: : FLit ( -- r )  r> dup f@ float+ >r ;
   69: : FLiteral ( compilation r -- ; run-time -- r ) \ float
   70:     \G Compile appropriate code such that, at run-time, r is placed
   71:     \G on the (floating-point) stack. Interpretation semantics are undefined.
   72:     BEGIN  here cell+ dup faligned <>  WHILE  postpone noop  REPEAT
   73:     postpone FLit  f, ;  immediate
   74: 
   75: &15 Value precision ( -- u ) \ floating-ext
   76: \G u is the number of significant digits currently used by
   77: \G @code{F.} @code{FE.} and @code{FS.} 
   78: : set-precision ( u -- ) \ floating-ext
   79:     \G Set the number of significant digits currently used by
   80:     \G @code{F.} @code{FE.} and @code{FS.} to u.
   81:     to precision ;
   82: 
   83: : scratch ( r -- addr len )
   84:   pad precision - precision ;
   85: 
   86: : zeros ( n -- )   0 max 0 ?DO  '0 emit  LOOP ;
   87: 
   88: : -zeros ( addr u -- addr' u' )
   89:   BEGIN  dup  WHILE  1- 2dup + c@ '0 <>  UNTIL  1+  THEN ;
   90: 
   91: : f$ ( f -- n )  scratch represent 0=
   92:   IF  2drop  scratch 3 min type  rdrop  EXIT  THEN
   93:   IF  '- emit  THEN ;
   94: 
   95: : f.  ( r -- ) \ floating-ext f-dot
   96: \G Display (the floating-point number) r using fixed-point notation,
   97: \G followed by a space.
   98:   f$ dup >r 0<
   99:   IF    '0 emit
  100:   ELSE  scratch r@ min type  r@ precision - zeros  THEN
  101:   '. emit r@ negate zeros
  102:   scratch r> 0 max /string 0 max -zeros type space ;
  103: \ I'm afraid this does not really implement ansi semantics wrt precision.
  104: \ Shouldn't precision indicate the number of places shown after the point?
  105: 
  106: : fe. ( r -- ) \ floating-ext f-e-dot
  107: \G Display r using engineering notation, followed by a space.
  108:   f$ 1- s>d 3 fm/mod 3 * >r 1+ >r
  109:   scratch r@ min type '. emit  scratch r> /string type
  110:   'E emit r> . ;
  111: 
  112: : fs. ( r -- ) \ floating-ext f-s-dot
  113: \G Display r using scientific notation, followed by a space.
  114:   f$ 1-
  115:   scratch over c@ emit '. emit 1 /string type
  116:   'E emit . ;
  117: 
  118: require debugs.fs
  119: 
  120: : sfnumber ( c-addr u -- r true | false )
  121:     2dup [CHAR] e scan ( c-addr u c-addr2 u2 )
  122:     dup 0=
  123:     IF
  124: 	2drop 2dup [CHAR] E scan ( c-addr u c-addr3 u3 )
  125:     THEN
  126:     nip
  127:     IF
  128: 	>float
  129:     ELSE
  130: 	2drop false
  131:     THEN ;
  132: 
  133: :noname ( c-addr u -- )
  134:     2dup sfnumber
  135:     IF
  136: 	2drop POSTPONE FLiteral
  137:     ELSE
  138: 	defers compiler-notfound
  139:     ENDIF ;
  140: IS compiler-notfound
  141: 
  142: :noname ( c-addr u -- r )
  143:     2dup sfnumber
  144:     IF
  145: 	2drop
  146:     ELSE
  147: 	defers interpreter-notfound
  148:     ENDIF ;
  149: IS interpreter-notfound
  150: 
  151: : fvariable ( "name" -- ) \ float
  152:     Create 0.0E0 f, ;
  153:     \ does> ( -- f-addr )
  154: 
  155: 1.0e0 fasin 2.0e0 f* fconstant pi ( -- r ) \ gforth
  156: \G FCONSTANT: r is the value pi; the ratio of a circle's area
  157: \G to its diameter.
  158: 
  159: : f2* ( r1 -- r2 ) \ gforth
  160:     \G Multiply r1 by 2.0e0
  161:     2.0e0 f* ;
  162: 
  163: : f2/ ( r1 -- r2 ) \ gforth
  164:     \G Multiply r1 by 0.5e0
  165:     0.5e0 f* ;
  166: 
  167: : 1/f ( r1 -- r2 ) \ gforth
  168:     \G Divide 1.0e0 by r1
  169:     1.0e0 fswap f/ ;
  170: 
  171: 
  172: \ We now have primitives for these, so we need not define them
  173: 
  174: \ : falog ( f -- 10^f )  [ 10.0e0 fln ] FLiteral f* fexp ;
  175: 
  176: \ : fsinh    fexpm1 fdup fdup 1.0e0 f+ f/ f+ f2/ ;
  177: \ : fcosh    fexp fdup 1/f f+ f2/ ;
  178: \ : ftanh    f2* fexpm1 fdup 2.0e0 f+ f/ ;
  179: 
  180: \ : fatanh   fdup f0< >r fabs 1.0e0 fover f- f/  f2* flnp1 f2/
  181: \            r> IF  fnegate  THEN ;
  182: \ : facosh   fdup fdup f* 1.0e0 f- fsqrt f+ fln ;
  183: \ : fasinh   fdup fdup f* 1.0e0 f+ fsqrt f/ fatanh ;
  184: 
  185: \ !! factor out parts
  186: : f~ ( f1 f2 f3 -- flag ) \ float-ext
  187:     fdup f0=
  188:     IF
  189: 	fdrop f= EXIT
  190:     THEN
  191:     fdup f0>
  192:     IF
  193: 	frot frot f- fabs fswap
  194:     ELSE
  195: 	fnegate frot frot fover fabs fover fabs f+ frot frot
  196: 	f- fabs frot frot f*
  197:     THEN
  198:     f< ;
  199: 
  200: : f.s ( -- ) \ gforth f-dot-s
  201:     \G Display the number of items on the floating-point stack,
  202:     \G followed by a list of the items; TOS is the right-most item.
  203:     ." <" fdepth 0 .r ." > " fdepth 0 max maxdepth-.s @ min dup 0 
  204:     ?DO  dup i - 1- floats fp@ + f@ f.  LOOP  drop ; 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>