Code:
' Trig routines courtesy Tracy Allen, PhD. (www.emesystems.com)
Arccosine:
disp = disp */ 983 / 3 ' normalize input to 127
angle = 63 - (disp / 2) ' approximate angle
DO ' find angle
IF (COS angle <= disp) THEN EXIT
angle = angle + 1
LOOP
angle = angle */ 360 ' convert brads to degrees
RETURN
Arcsine:
GOSUB Arccosine
angle = 90 - angle
RETURN
It's actually for a
dual-axis accelerometer manufactured by Memsic but made by parallax. The source code is at the bottom of the page. It's the last thing in the "Dual.bs2" source file.
If we follow the link we get:
this.
Code:
arctangent
'The arctangent uses the vectoring algorithm, where the value of
'y instead of z is driven to zero as a result of the decision in each
'time through the loop. That is, by driving the tangent of the angle
'to zero, we drive up the angle up to the value we want to find,
'given the initial tangent of that angle.
tans data word 32768 ' arctan 1
data word ..... ' arctan 1/2
data word ..... ' arctan 1/4
...
data word ..... ' arctan 1/32678
var x word ' accumulated x component (cos)
var x0 word ' a helper variable
var y word ' accumulated y component (sin)
var z word ' this will be the accumulated angle
var i nib ' loop counter
var A word ' angle read from tans table
ys var y.bit15
xs var x.bit15
zs var z.bit15
y=y0 ' we will compute the arctan
x=x0 ' based on tan theta = y0/x0
for i=0 to 15
read 2*i,A..byte0
read 2*i+1, A.byte1
x=x0 ' a copy of the current value of x
x0 = x + (-ys^(-ys^(abs y>>i) + ys)+ys)
y = y - (-ys^(-xs ^(abs x>>i) + xs)+ys)
z = z + (-ys^A+zs
next
donecalc:
x=x0
'The formula for x0 can doubtless be simplified by quite a bit, as it
'involves double negatives.
x0 = x + (-ys^(-ys^(abs y>>i) + ys)+ys)
^^^^^^^^^^^^^^^^^^^------divide the abs y by 2^i
and restore the sign
^^^^^^^^^^^^^^^^^^^^^^^^^^^^---and negate if y is negative
so the result is positive no matter what.:
x0 = x + (abs y>>i)
Note that you need to fill in the tans table when you write your code for the algoritm to calculate properly.