Annotation of gforth/float.fs, revision 1.21

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