Fast cosine / Fast sine (using symmetric modulo)

Share your advanced PureBasic knowledge/code with the community.
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Fast cosine / Fast sine (using symmetric modulo)

Post by eddy »

I update my previous version.
- Now this function uses the symmetric property of modolo operator.
- It works with a negative angle

Code: Select all

; ************
; fast cosine
; fast sine
; ************

#_COSINE_MAX_INDEX=$100000 ; <----------- symmetric modulo is enabled by the power of 2 
#_MATH_DEGREE_TO_INDEX=#_COSINE_MAX_INDEX/360.0
#_MATH_DEGREE_TO_RADIAN=#PI*2.0/360.0

Global Dim _SIN.f(#_COSINE_MAX_INDEX)
Global Dim _COS.f(#_COSINE_MAX_INDEX)

For i=0 To #_COSINE_MAX_INDEX
   _COS(i)=Cos(i*2.0*#PI/#_COSINE_MAX_INDEX)
   _SIN(i)=Sin(i*2.0*#PI/#_COSINE_MAX_INDEX)
Next

Macro FastCos(a) : _COS(Int(a*#_MATH_DEGREE_TO_INDEX)%#_COSINE_MAX_INDEX) : EndMacro
Macro FastSin(a) : _SIN(Int(a*#_MATH_DEGREE_TO_INDEX)%#_COSINE_MAX_INDEX) : EndMacro

; ************
; TEST
; ************

#TEST_MAX_ANGLE=11000000;9900000
Define.f x, y

t=ElapsedMilliseconds()
For a=0 To #TEST_MAX_ANGLE Step 7
   x=Cos(a*#_MATH_DEGREE_TO_RADIAN)
   y=Sin(a*#_MATH_DEGREE_TO_RADIAN)
Next
t1=ElapsedMilliseconds()-t

t=ElapsedMilliseconds()
For a=0 To #TEST_MAX_ANGLE Step 7
   x=FastCos(a)
   y=FastSin(a)
Next
t2=ElapsedMilliseconds()-t


result.s="NORMAL t="+Str(t1)
result+#CRLF$+"FAST t="+Str(t2)+" --> "+Str(100*t2/t1)+"%"
V=-90
result+#CRLF$+"Sin("+Str(V)+")="+StrF(FastSin(V))+" ~ "+StrF(Sin(V*#_MATH_DEGREE_TO_RADIAN))
V=90
result+#CRLF$+"Sin("+Str(V)+")="+StrF(FastSin(V))+" ~ "+StrF(Sin(V*#_MATH_DEGREE_TO_RADIAN))
V=180
result+#CRLF$+"Cos("+Str(V)+")="+StrF(FastCos(V))+" ~ "+StrF(Cos(V*#_MATH_DEGREE_TO_RADIAN))
V=111+360*10
result+#CRLF$+"Sin("+Str(V)+")="+StrF(FastSin(V))+" ~ "+StrF(Sin(V*#_MATH_DEGREE_TO_RADIAN))
OpenWindow(0, 0, 0, 300, 300, "FAST COS FAST SIN", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
EditorGadget(1, 5, 5, 290, 290)
SetGadgetText(1, result)
Repeat : Until WaitWindowEvent()=#PB_Event_CloseWindow
Last edited by eddy on Sun Sep 06, 2009 4:43 pm, edited 1 time in total.
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

oh... and have a nice day.
User avatar
eddy
Addict
Addict
Posts: 1479
Joined: Mon May 26, 2003 3:07 pm
Location: Nantes

Post by eddy »

Kaeru Gaman wrote:compare it with this one:
http://www.purebasic.fr/german/viewtopic.php?t=5884
Interesting... but mine supports the negative angles that's why I used the modulo operator. :D
Imagewin10 x64 5.72 | IDE | PB plugin | Tools | Sprite | JSON | visual tool
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post by Kaeru Gaman »

ermn... in fact it does.

-180 is $FFFFFF4C
when I AND it with 4095, I get $F4C, this is a valid index.
oh... and have a nice day.
Post Reply