Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Faster Trigonometry
#4
Here's a full set of functions, with a slightly higher accuracy (at a slightly larger cost - this one is tight to within 2 degrees, and interpolates anything in  between. As a result it needs 46 bytes for the table instead of the 33 used further up.) I figured that to use things like TAN - which divides one by the other - it might not hurt to tighten it a little; since that multiplies the error up!

It's now accurate to about 0.25% on average over a full circle. Slightly less for TAN values for the reasons listed above.


Code:
FUNCTION Fsin(num as FIXED) as FIXED
DIM quad as byte
DIM est1,dif as uByte

while num>360
  num=num-360
end while

while num<0
num=num+360
end while

IF num>180 then
  quad=-1
  num=num-180
ELSE
  quad=1
END IF

IF num>90 then num=180-num: end if

num=num/2
dif=num : rem Cast to byte loses decimal
num=num-dif : rem so this is just the decimal bit


est1=PEEK (@sinetable+dif)
dif=PEEK (@sinetable+dif+1)-est1 : REM this is just the difference to the next up number.

num=est1+(num*dif): REM base +interpolate to the next value.

return (num/255)*quad


sinetable:
asm
DEFB 000,009,018,027,035,044,053,062
DEFB 070,079,087,096,104,112,120,127
DEFB 135,143,150,157,164,171,177,183
DEFB 190,195,201,206,211,216,221,225
DEFB 229,233,236,240,243,245,247,249
DEFB 251,253,254,254,255,255
end asm
END FUNCTION

Code:
FUNCTION Fcos(num as FIXED) as FIXED
return Fsin(90-num)
END FUNCTION

Code:
FUNCTION Ftan(num as FIXED) as FIXED
return Fsin(num)/Fsin(90-num)
END FUNCTION
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 2 Guest(s)