anchor.diff:      3 of dnegate pi/2 d+ cosine         endof
     endcase
   
  -  2-foldable ;
  +2-foldable ;
   
   : widesine ( f-angle -- f-sine )
     dup >r \ Save sign
  
positive.code: \ Sine and Cosine with Cordic algorithm
  \ copied with minor mods from Mecrisp Stellaris "common/sine.txt"
  
  : numbertable <builds does> swap 2 lshift + @ ;
  
  numbertable e^ka
  hex
    C90FDAA2 , 76B19C15 , 3EB6EBF2 , 1FD5BA9A ,
    0FFAADDB , 07FF556E , 03FFEAAB , 01FFFD55 ,
    00FFFFAA , 007FFFF5 , 003FFFFE , 001FFFFF ,
    000FFFFF , 0007FFFF , 0003FFFF , 0001FFFF ,
    0000FFFF , 00007FFF , 00003FFF , 00001FFF ,
    00000FFF , 000007FF , 000003FF , 000001FF ,
    000000FF , 0000007F , 0000003F , 0000001F ,
    0000000F , 00000007 , 00000003 , 00000001 ,
  decimal
  
  : 2rshift 0 ?do d2/ loop ;
  : 2shuffle 2rot 2rot 2swap 2over ;
  
  : cordic ( f-angle -- f-error f-sine f-cosine )
  \ Angle between -Pi/2 and +Pi/2 !
    0 0 $9B74EDA8 0
    32 0 do
      2rot dup 0<
      if
        i e^ka 0 d+ 2rot 2rot
              2over i 2rshift 2shuffle
        i 2rshift
        d- 2rot 2rot d+
      else
        i e^ka 0 d- 2rot 2rot
              2over i 2rshift 2shuffle
        i 2rshift
        d+ 2rot 2rot 2swap d-
      then
    loop
  2-foldable ;
  
  : sine   ( f-angle -- f-sine )   cordic 2drop 2nip   2-foldable ;
  : cosine ( f-angle -- f-cosine ) cordic 2nip  2nip   2-foldable ;
  
  3,141592653589793  2constant pi
  pi 2,0 f/ 2constant pi/2
  \ pi 4,0 f/ cosine f. \ Displays cos(Pi/4)
  
  : widecosine ( f-angle -- f-cosine )
    dabs
  
negative.code:   endcase
  
    r> 0< if dnegate then
  
  2-foldable ;
  
  
  \ -----------------------------------------------------------------------------
  \  Integer XY vector --> Polar coordinates
  \ -----------------------------------------------------------------------------
  
  : cordic-vectoring ( x y -- d-r f-angle )
  
    \ The CORDIC algorithm on its own works fine with angles between -Pi/2 ... +Pi/2.
    \ Need to handle angles beyond, which translates to if x < 0,  by an additional step:
  
    over 0<  \ x < 0 ?
    if
      negate swap negate swap
  
      dup 0< if \ Now y < 0 ?
        pi         cordic-z 2!
      else
        pi dnegate cordic-z 2!
      then
    else
      0,0  cordic-z 2!
    then
  
    \ Improve accuracy by exploiting 64 bit dynamic range during calculations
  
    s>d 24 0 do d2* loop cordic-y 2!
    s>d 24 0 do d2* loop cordic-x 2!
  
    32 0 do
  
      cordic-y 2@ d0<
      if
        cordic-step-minus
      else
        cordic-step-plus
      then
  
    loop
  
    cordic-z 2@
    cordic-x 2@
  
  2-foldable ;
  
  : atan2     ( x y -- f-angle )             cordic-vectoring 2drop                2-foldable ;
  : magnitude ( x y -- d-magnitude )         cordic-vectoring 2nip  $0,9B74EDA8 f* 24 2arshift 2-foldable ;
  : xy>polar  ( x y -- f-angle d-magnitude ) cordic-vectoring       $0,9B74EDA8 f* 24 2arshift 2-foldable ;
  
  \ -----------------------------------------------------------------------------
  \   Small helpers for calculations
  \ -----------------------------------------------------------------------------
  
  : s>f ( n -- f ) 0 swap  inline 1-foldable ; \ Signed integer --> Fixpoint s31.32
  : f>s ( f -- n ) nip     inline 2-foldable ; \ Fixpoint s31.32 --> Signed integer
  