FlashForth: for PIC and Atmega Wiki
Brought to you by:
oh2aun
Here is a set of words to generate sin and cos values from a sin look-up table based on angles in degrees. The table values can be placed in ram, eeprom or flash. Flash seems to make the most sense if the table values will not be changed.
Written by Pete Zawasky
-sin_cos marker -sin_cos decimal ram \ Print to four decimal places. \ : .xxxx ( n -- ) dup >r abs 0 <# # # # # [char] . hold #s r> sign #> type space ; \ 16/16->16 bit division. n3=remainder. \ : mod ( n1 n2 -- n3 ) >r s>d r> 2dup xor >r over >r abs >r dabs r> um/mod swap r> ?negate swap r> ?negate drop ; \ Compile a word which creates n cell look-up tables. \ compile-time table <name> n1 , n2 , ... ni , \ run-time i <name> where i is the table index \ and i is zero based (0...i) : table ( -- ) create does> ( i -- n ) swap cells + @ ; \ \ SIN look-up table \ 0 --> 90 degrees flash \ set dataspace to flash table tSIN 0 , 175 , 349 , 523 , 698 , 872 , 1045 , 1219 , 1392 , 1564 , 1736 , 1908 , 2079 , 2250 , 2419 , 2588 , 2756 , 2924 , 3090 , 3256 , 3420 , 3584 , 3746 , 3907 , 4067 , 4226 , 4384 , 4540 , 4695 , 4848 , 5000 , 5150 , 5299 , 5446 , 5592 , 5736 , 5878 , 6018 , 6157 , 6293 , 6428 , 6561 , 6691 , 6820 , 6947 , 7071 , 7193 , 7314 , 7431 , 7547 , 7660 , 7771 , 7880 , 7986 , 8090 , 8192 , 8290 , 8387 , 8480 , 8572 , 8660 , 8746 , 8829 , 8910 , 8988 , 9063 , 9135 , 9205 , 9272 , 9336 , 9397 , 9455 , 9511 , 9563 , 9613 , 9659 , 9703 , 9744 , 9781 , 9816 , 9848 , 9877 , 9903 , 9925 , 9945 , 9962 , 9976 , 9986 , 9994 , 9998 , 10000 , \ set dataspace back to ram ram : (sin) ( deg -- sin ) dup 90 > if 180 swap - then tSIN ; : sin ( deg -- sin ) 360 mod dup 0 < if 360 + then dup 180 > if 180 - (sin) negate else (sin) then ; : cos ( deg -- cos ) 360 mod 90 + sin ; \ Print the sin of angle. : .sin ( deg -- ) sin .xxxx ; \ Print the sin of angle. : .cos ( deg -- ) cos .xxxx ; \ Print sin 0-->359 degree. : .sin360 ( -- ) cr 0 #360 for dup .sin cr 1+ next drop ; \ From the sin and cos you can easily write expressions for the other \ *direct* trig functions: \ \ tan(x) = sin(x)/cos(x) \ cot(x) = cos(x)/sin(x) \ sec(x) = 1/cos(x) \ csc(x) = 1/sin(x) ram hex