Hi,
here are the words for fsin, fcos, ftan. It uses taylor series for
fcos.

\ fsin fcos ftan library
\ amforth 4.0
\ Pito 23-9-2010
\ based on taylor and fsin fcos from 4tH library by J.L. Bezemer
\ needs Leon's flib (and Pito's asm flib v 1.1 for speed if
required)
\ needs Pito's float constants ( _pi)

-fsincostan
marker -fsincostan

\ Table with factorials n!
\ could be inlined in amforth 4.2
2. 2constant d1                         \ 2!
24. 2constant d2                        \ 4!
720. 2constant d3                       \ 6!
40320. 2constant d4                     \ 8!
3628800. 2constant d5           \ 10!
479001600. 2constant d6         \ 12!
_pi f2/ fconstant _pihalf
\ the original (taylor) does not work, bug?
: >taylor fdup f* fover ;            \ setup for Taylor series
: _taylor fover f* frot fover ;
: +taylor d>f f/ f+ frot frot ;      \ add Taylor iteration
: -taylor d>f f/ f- frot frot ;      \ subtract Taylor iteration
\ puts x in RADIANS within the 2pi                                  
  
: >range                              
_pi fdup f+                          ( x pi2 )
fover fover f/                       ( x pi2 x/pi2 )
ffloor fover f*                      ( x pi2 mod )
frot fswap f-                        ( pi2 mod )
_pi fover                            ( pi2 mod pi mod )
f< if fswap f- else fnip then
;
\ calculate fcos
: fcos
1 s>f fswap >range >taylor           ( 1 x2 1 )
_taylor d1 -taylor                 ( 1-2 x2 x2 )
_taylor d2 +taylor                 ( 1+4 x2 x4 ) 
_taylor d3 -taylor                 ( 1-6 x2 x6 )
_taylor d4 +taylor                 ( 1+8 x2 x8 )
_taylor d5 -taylor                 ( 1-10 x2 x10 )
_taylor d6 +taylor                 ( 1+12 x2 x12 )
fdrop fdrop                          ( 1+12 )
;
\ calculate fsin 
: fsin _pihalf f- >range fcos ;
\ calculate ftan
: ftan fdup fsin fswap fcos f/ ;         \ ftan = fsin / fcos

******************************
Ex:

> 1 s>f fsin fs.
8.414711E-1  ok
> 1 s>f fcos fs.
5.4030232E-1  ok
> -1 s>f fcos fs.
5.4030232E-1  ok
> -1 s>f fsin fs.
-8.414648E-1  ok
> _pi fsin fs.
-1.24561954E-7  ok
> _pi fcos fs.
-9.9989967E-1  ok
> -1 s>f ftan fs.
-1.5573962  ok
> 1 s>f ftan fs.
1.55740795  ok
> -10 s>f ftan fs.
-6.4836473E-1  ok
>






------------------------------------------------------------------------------
Start uncovering the many advantages of virtual appliances
and start using them to simplify application deployment and
accelerate your shift to cloud computing.
http://p.sf.net/sfu/novell-sfdev2dev
_______________________________________________
Amforth-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/amforth-devel

Reply via email to