Annotation of gforth/float.fs, revision 1.18

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.16      anton      58: : fconstant  ( r -- ) \ 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.16      anton     101: require debugging.fs
                    102: 
1.7       anton     103: : sfnumber ( c-addr u -- r / )
1.16      anton     104:     2dup [CHAR] e scan
                    105:     dup 0=
                    106:     IF
                    107:        2drop 2dup [CHAR] E scan
                    108:     THEN
                    109:     nip
1.7       anton     110:     IF
1.16      anton     111:        2dup >float
1.7       anton     112:        IF
1.16      anton     113:            2drop state @
                    114:            IF
                    115:                POSTPONE FLiteral
                    116:            THEN
                    117:            EXIT
1.7       anton     118:        THEN
1.16      anton     119:     THEN
                    120:     defers notfound ;
1.1       anton     121: 
1.7       anton     122: ' sfnumber IS notfound
1.13      anton     123: 
                    124: : fvariable ( -- )
1.15      pazsan    125:     Create 0.0E0 f, ;
1.13      anton     126:     \ does> ( -- f-addr )
1.1       anton     127: 
1.15      pazsan    128: 1.0e0 fasin 2.0e0 f* fconstant pi
1.6       pazsan    129: 
1.15      pazsan    130: : f2*  2.0e0 f* ;
                    131: : f2/  0.5e0 f* ;
                    132: : 1/f  1.0e0 fswap f/ ;
1.6       pazsan    133: 
                    134: 
1.10      anton     135: \ We now have primitives for these, so we need not define them
1.6       pazsan    136: 
1.15      pazsan    137: \ : falog ( f -- 10^f )  [ 10.0e0 fln ] FLiteral f* fexp ;
1.10      anton     138: 
1.15      pazsan    139: \ : fsinh    fexpm1 fdup fdup 1.0e0 f+ f/ f+ f2/ ;
1.10      anton     140: \ : fcosh    fexp fdup 1/f f+ f2/ ;
1.15      pazsan    141: \ : ftanh    f2* fexpm1 fdup 2.0e0 f+ f/ ;
1.10      anton     142: 
1.15      pazsan    143: \ : fatanh   fdup f0< >r fabs 1.0e0 fover f- f/  f2* flnp1 f2/
1.10      anton     144: \            r> IF  fnegate  THEN ;
1.15      pazsan    145: \ : facosh   fdup fdup f* 1.0e0 f- fsqrt f+ fln ;
                    146: \ : fasinh   fdup fdup f* 1.0e0 f+ fsqrt f/ fatanh ;
1.9       pazsan    147: 
1.16      anton     148: \ !! factor out parts
                    149: : f~ ( f1 f2 f3 -- flag ) \ float-ext
                    150:     fdup f0=
                    151:     IF
                    152:        fdrop f= EXIT
                    153:     THEN
                    154:     fdup f0>
                    155:     IF
                    156:        frot frot f- fabs fswap
                    157:     ELSE
                    158:        fnegate frot frot fover fabs fover fabs f+ frot frot
                    159:        f- fabs frot frot f*
                    160:     THEN
                    161:     f< ;
1.11      pazsan    162: 
1.9       pazsan    163: : f.s  ." <" fdepth 0 .r ." > " fdepth 0 max maxdepth-.s @ min dup 0 
                    164:   ?DO  dup i - 1- floats fp@ + f@ f.  LOOP  drop ; 

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