Annotation of gforth/float.fs, revision 1.7

1.1       anton       1: \ High level floating point                            14jan94py
                      2: 
1.6       pazsan      3: 1 cells 4 = [IF]
                      4: ' cells   Alias sfloats
                      5: ' cell+   Alias sfloat+
                      6: ' align   Alias sfalign
                      7: ' aligned Alias sfaligned
                      8: [ELSE]
                      9: : sfloats  4 * ;
                     10: : sfloat+  4 + ;
                     11: : sfaligned ( addr -- addr' )  3 + -4 and ;
                     12: : sfalign ( -- )  here dup sfaligned swap ?DO  bl c,  LOOP ;
                     13: [THEN]
                     14: 
                     15: 1 floats 8 = [IF]
                     16: ' floats   Alias dfloats
                     17: ' float+   Alias dfloat+
                     18: ' falign   Alias dfalign
                     19: ' faligned Alias dfaligned
                     20: [ELSE]
                     21: : dfloats  8 * ;
                     22: : dfloat+  8 + ;
                     23: : dfaligned ( addr -- addr' )  7 + -8 and ;
                     24: : dfalign ( -- )  here dup dfaligned swap ?DO  bl c,  LOOP ;
                     25: [THEN]
                     26: 
1.1       anton      27: : f, ( f -- )  here 1 floats allot f! ;
                     28: 
1.2       anton      29: \ !! have create produce faligned pfas
1.1       anton      30: : fconstant  ( r -- )
1.4       pazsan     31:   falign here f,  Create A,
                     32:   DOES>  @ f@ ;
1.1       anton      33: 
                     34: : fvariable
1.4       pazsan     35:   falign here 0. d>f f, AConstant ;
1.1       anton      36: 
                     37: : fdepth  ( -- n )  f0 @ fp@ - [ 1 floats ] Literal / ;
                     38: 
                     39: : FLit ( -- r )  r> faligned dup f@ float+ >r ;
                     40: : FLiteral ( r -- )  postpone FLit  falign f, ;  immediate
                     41: 
                     42: &16 Value precision
                     43: : set-precision  to precision ;
                     44: 
                     45: : scratch ( r -- addr len )
                     46:   pad precision - precision ;
                     47: 
                     48: : zeros ( n -- )   0 max 0 ?DO  '0 emit  LOOP ;
                     49: 
                     50: : -zeros ( addr u -- addr' u' )
                     51:   BEGIN  dup  WHILE  1- 2dup + c@ '0 <>  UNTIL  1+  THEN ;
                     52: 
1.4       pazsan     53: : f$ ( f -- n )  scratch represent 0=
                     54:   IF  2drop  scratch 3 min type  rdrop  EXIT  THEN
                     55:   IF  '- emit  THEN ;
                     56: 
                     57: : f.  ( r -- )  f$ dup >r 0<
                     58:   IF    '0 emit
1.1       anton      59:   ELSE  scratch r@ min type  r@ precision - zeros  THEN
                     60:   '. emit r@ negate zeros
                     61:   scratch r> 0 max /string 0 max -zeros type space ;
1.3       benschop   62: \ I'm afraid this does not really implement ansi semantics wrt precision.
                     63: \ Shouldn't precision indicate the number of places shown after the point?
1.1       anton      64: 
1.4       pazsan     65: : fe. ( r -- )  f$ 1- s>d 3 fm/mod 3 * >r 1+ >r
1.1       anton      66:   scratch r@ min type '. emit  scratch r> /string type
                     67:   'E emit r> . ;
                     68: 
1.4       pazsan     69: : fs. ( r -- )  f$ 1-
                     70:   scratch over c@ emit '. emit 1 /string type
                     71:   'E emit . ;
                     72: 
1.7     ! anton      73: : sfnumber ( c-addr u -- r / )
        !            74:     2dup >float
        !            75:     IF
        !            76:        2drop state @
        !            77:        IF
        !            78:            postpone FLiteral
        !            79:        THEN
        !            80:     ELSE
        !            81:        defers notfound
        !            82:     THEN ;
1.1       anton      83: 
1.7     ! anton      84: ' sfnumber IS notfound
1.1       anton      85: 
                     86: 1e0 fasin 2e0 f* fconstant pi
1.6       pazsan     87: 
                     88: : f2*  2e0 f* ;
                     89: : f2/  2e0 f/ ;
                     90: : 1/f  1e0 fswap f/ ;
                     91: 
                     92: : falog ( f -- 10^f )  [ 10e0 fln ] FLiteral f* fexp ;
                     93: 
                     94: : fsinh    fexpm1 fdup fdup 1e0 f+ f/ f+ f2/ ;
                     95: : fcosh    fexp fdup 1/f f+ f2/ ;
                     96: : ftanh    f2* fexpm1 fdup 2e0 f+ f/ ;
                     97: 
                     98: : fatanh   fdup f0< >r fabs 1e0 fover f- f/  f2* flnp1 f2/
                     99:            r> IF  fnegate  THEN ;
                    100: : facosh   fdup fdup f* 1e0 f- fsqrt f+ fln ;
                    101: : fasinh   fdup fdup f* 1e0 f+ fsqrt f/ fatanh ;

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