Arc Tangent in Small Basic
- I have an equation that is relatively simply using the ArcTan function (angle between two points) and nearly impossible without it. Without accessing an external library, can someone give me a SmallBasic equivalent for ArcTan?
Answers
Using a convergent series, this gives a "close-enough" answer
Sub ArcTan
If x > 1 then
x = 1 / x
ArcTanInternal()
y = Math.Pi / 2 - y
Else
ArcTanInternal()
EndIf
EndSubSub ArcTanInternal
y = x - Math.Power(x, 3) / 3 + Math.Power(x, 5) / 5 - Math.Power(x, 7) / 7
EndSub
Sample program published as: QHL110- Marked As Answer byCoding CatAnswererTuesday, April 07, 2009 1:28 AM
All Replies
- Using radians
For |x| < 1 : arctan(x) ~ x/(1+0.28*x^2)
For |x| > 1 : arctan(x) ~ pi/2 - x/(x^2 + 0.28) Using a convergent series, this gives a "close-enough" answer
Sub ArcTan
If x > 1 then
x = 1 / x
ArcTanInternal()
y = Math.Pi / 2 - y
Else
ArcTanInternal()
EndIf
EndSubSub ArcTanInternal
y = x - Math.Power(x, 3) / 3 + Math.Power(x, 5) / 5 - Math.Power(x, 7) / 7
EndSub
Sample program published as: QHL110- Marked As Answer byCoding CatAnswererTuesday, April 07, 2009 1:28 AM
- The resulting angle is in Radians, you can use Math.GetDegrees to convert the angle to degrees.
Using a convergent series, this gives a "close-enough" answer
I don't find that a "close-enough" answer :)
It's only a rough approximation... This is the best thing we can do for the moment but it's not really a pretty way to do the thing.
Fremy - Developer in VB.NET, C# and JScript ... - Feel free to try my extension- Thank you.
And add the Arc-Trig functions to the wish list. - That convergent series can be run out there a long ways to get an even closer "close-enough" value.
- Here is a simple way if getting that arctan value. I just ran that Taylor series out to 99. It is the same as my TI-89 to at least 6 decimal places. We got men on the moon with less accuracy.
x = 1.5 ArcTan() TextWindow.WriteLine(y) x = 0.6 ArcTan() TextWindow.WriteLine(y) Sub ArcTan If x > 1 then x = 1 / x ArcTanInternal() y = Math.Pi / 2 - y Else ArcTanInternal() EndIf EndSub Sub ArcTanInternal 'y = x - Math.Power(x, 3) / 3 + Math.Power(x, 5) / 5 - Math.Power(x, 7) / 7 sign = -1 y = 0 For p = 1 To 99 Step 2 sign = - sign y = y + sign * Math.Power(x, p)/p EndFor EndSub


