Trig Functions in Forth
Date: 04/07/2003 at 09:35:07 From: Tod Wulff Subject: Other Trig Functions from Sin, Cosin, SQRT, +, -,* , and / I am writing a program in the language Forth. I need to determine the two missing angles in a right triangle. I know all three sides, and of course, the 90 degree angle. I understand that Angle1 = Arcsin (opp/hyp) and that Angle2 = 90' - Angle1; the problem that I am facing is that the only functions provided in the basic library are the sin and cos functions, along with sqrt and of course + - * /. Is there a method I can write to implement a function to calculate arcsin using the supplied functions? What about other trig functions (tan/atan/csc/etc.)? Will tables be needed?
Date: 04/07/2003 at 11:28:20 From: Doctor Rick Subject: Re: Other Trig Functions from Sin, Cosin, SQRT, +, -,* , and / Hi, Tod. Are you sure that Forth does not have an ATAN function? It's been 20 years since I used Forth, and I didn't use it extensively, so my memory means little. But a bit of Internet searching at least suggests that some versions of Forth have an atan function. It doesn't make sense to me that languages that have sin and cos wouldn't have at least the one inverse trig function. From the sin and cos you can easily write expressions for the other *direct* trig functions: tan(x) = sin(x)/cos(x) cot(x) = cos(x)/sin(x) sec(x) = 1/cos(x) csc(x) = 1/sin(x) But you can't express *inverse* trig functions so easily. If the atan function really isn't available to you, it cannot be recreated by a closed-form expression. There are infinite series expressions for inverse tangent that can be evaluated as far as needed for a given accuracy. Depending on your need for accuracy, it may be that a simple lookup table is sufficient. I'll leave it at this until you can confirm the absence of an atan function and/or tell me something more about what you need this for. - Doctor Rick, The Math Forum http://mathforum.org/dr.math/
Date: 04/07/2003 at 15:40:05 From: Tod Wulff Subject: Other Trig Functions from Sin, Cosin, SQRT, +, -,* , and / Dr. Rick, Thanks for taking the time to review my query and respond. If you see below, you will see that the trig library only supports the sin and cosin functions. The platform that I am working on is a Palm Based Handheld device with Quartus Forth (www.Quartus.net) installed. I have win32forth installed on my desktop and looked at the asin function there and they implemented it in a machine language call, thus porting it over to Forth proper would be difficult as it has been many many years since I have dabbled in Assembly. I am still searching both far and wide for a pre-developed Forth proper implementation so that I do not have to reinvent the wheel. Regarding your comments on accuracy, merely to the 10th or 100th is acceptable. Actually, I can make what I want to do work with accuracies to the whole unit - it is for processing lines on a graphic LCD display. Palm does have a mathlib that can be called, but in order for someone to use my app, they would have to also have that optional component installed. I would like to make my app self sufficient, and besides, as you said, not having an *inverse* trig function seems surprising in a language. I would like to plug that little hole. Rick, again, Thank you! -Tod Wulff \ trig 99.12.10 11:46 am NAB : table create does> swap 2* + @ ; table tsin 0 , 175 , 349 , 523 , 698 , 872 , 1045 , 1219 , 1392 , 1564 , 1736 , 1908 , 2079 , 2250 , 2419 , 2588 , 2756 , 2924 , 3090 , 3256 , 3420 , 3584 , 3746 , 3907 , 4067 , 4226 , 4384 , 4540 , 4695 , 4848 , 5000 , 5150 , 5299 , 5446 , 5592 , 5736 , 5878 , 6018 , 6157 , 6293 , 6428 , 6561 , 6691 , 6820 , 6947 , 7071 , 7193 , 7314 , 7431 , 7547 , 7660 , 7771 , 7880 , 7986 , 8090 , 8192 , 8290 , 8387 , 8480 , 8572 , 8660 , 8746 , 8829 , 8910 , 8988 , 9063 , 9135 , 9205 , 9272 , 9336 , 9397 , 9455 , 9511 , 9563 , 9613 , 9659 , 9703 , 9744 , 9781 , 9816 , 9848 , 9877 , 9903 , 9925 , 9945 , 9962 , 9976 , 9986 , 9994 , 9998 , 10000 , : (sin) ( n -- n') dup 90 > if 180 swap - then tsin ; : sin ( n -- n') 360 /mod drop dup 0 < if 360 + then dup 180 > if 180 - (sin) negate else (sin) then ; : cos ( n -- n') 360 /mod drop 90 + sin ;
Date: 04/07/2003 at 17:58:38 From: Doctor Rick Subject: Re: Other Trig Functions from Sin, Cosin, SQRT, +, -,* , and / Hi, Tod. I did a little looking around the Web and I found an interesting page giving some rational approximations to atan(x). I know nothing about the site, but it looks interesting: StuChat #37: More Atan http://www.lightsoft.co.uk/PD/stu/stuchat37.html It says that, for -1 <= x <= 1, this function gives atan(x) accurate to 6 decimal places: atan(x) = (x + 0.43157974*x^3)/(1 + 0.76443945*x^2 + 0.05831938*x^4) I checked it with a spreadsheet and it looks good. A simpler rational expression is also given, said to be accurate to within 0.005, and faster than table lookup: atan(x) = x/(1 + 0.28*x^2) This is probably just the thing for you. For |x| > 1, you'll want to use atan(x) = pi/2 - x/(x^2 + 0.28) Let me know how it goes. - Doctor Rick, The Math Forum http://mathforum.org/dr.math/
Date: 04/11/2003 at 18:59:20 From: Tod Wulff Subject: Other Trig Functions from Sin, Cosin, SQRT, +, -,* , and / Rick, Thanks again for the help. Please follow this link to see how I addressed my problem, at least in the interim... <http://www.quartus.net/discus/messages/23/ 1924.html?1050101547#POST4258> Again, thanks.
Date: 04/11/2003 at 20:17:40 From: Doctor Rick Subject: Re: Other Trig Functions from Sin, Cosin, SQRT, +, -,* , and / Hi, Tod. So you chose the lookup table route after all. That was my first thought, and as I said then, a lot depends on the accuracy you need. But thanks for inspiring me to look around and discover that information on rational expression approximations to the arctangent. - Doctor Rick, The Math Forum http://mathforum.org/dr.math/
Search the Dr. Math Library:
Ask Dr. MathTM
© 1994- The Math Forum at NCTM. All rights reserved.