Comparing with Orolo's code, his sine wave is almost perfect, remembering to insert a 0 at the beginning of the table. Here is the basic conversion with a plot:
' http://www.freebasic.net/
' for dos, win & linux
' sintable comparison generator by BrianHG.
const pi = 3.141592654 : Rem This is the same thing >>> atn(1) * 4
const xscale = 10
const xoffset = 50
const yscale = 30
const yoffset = 720
Declare Sub plot_sin_table()
dim Shared as unsigned integer x,y,n,cr,cg,cb,tmp,sx,cx,sa,ca,t,c
dim Shared sin_table(0 to 90) as integer
ScreenRes 1152,768,32,0,0
' Generate sine_table using the function sin(x).
for n=0 to 90
sin_table(n) = sin(pi*(n/90)/2) * 16384
next n
' Plot sin_table(n) in green
cr=0:cg=255:cb=0:plot_sin_table()
' approximate sine_table 'orolo's code.
' please excuse basic's limitation of shifting bits, I had to replace them with divide...
' It is expected that in the CPU, you would use the bit shifting for speed and code size.
sx = 1144 ' 0b0000010001111000;
cx = 65526 ' 0b1111111111110110;
sa = 0 ' 0;
ca = 65535 ' 0b1111111111111111;
sin_table(0) = 0
for c=1 to 90
t = (sa*cx + ca*sx)/65536
ca = (ca*cx - sa*sx)/65536
sa = t
t = (sa / 4) + ((sa and 2)/2)
sin_table(c) = t
next c
' Plot sin_table(n) in cyan
cr=0:cg=255:cb=255:plot_sin_table()
' Wait until a key if pressed before ending.
while (inkey$="") : sleep 1 : wend
END
Sub plot_sin_table()
for n=0 to 90
? sin_table(n),
next n
?:?
for n=0 to 89
line (n*xscale+xoffset, yoffset-(sin_table(n)/yscale)) - ((n+1)*xscale+xoffset, yoffset-(sin_table(n+1)/yscale) ), RGB(cr,cg,cb)
next n
End Sub
As you can see in the attached image, Orolo's code's cyan color output sits right on top of the original green sine wave with only a pixel deviation or 2 here and there. This is the best I've seen yet requiring very little processing power to generate a table.
If you don't want any trig functions with floats, use Orolo's code. No one will know the difference.
If you dont want to store the table and want to compute on the fly, quick random access the first ' (8100-(90-n)^2)>>1 ' trick I made will give you a quick 1 multiply, 2 subtracts + bit shift direct access to a curve, but nothing as accurate as Orolo's waveform.