Trig lookup table

I've created a class which uses a lookup table to improve the speed that I can find the cos, sin and tan, of any angle.

What my program does, is it creates a float array, which stores the cos of each angle from 0 up to 90.

Then, when my program is given an angle, it casts it to an int, and can then use the information within this 91 element array to find the cosine of that angle.

Then, for calculating the sin and tan of an angle, I make use of the following identities, in order to use the cos lookup table to find them:

   sin a = cos (90 - a)
   tan a = sin a / cos a

This all works very very nicely; I get a result which is certainly accurate enough for what I'm doing at least, and it returns the value at least 3 times faster than the native methods can calculate them. So, it's all good.


Now however, I wish to do the same sort of thing, but for the inverse functions — acos, asin, and atan.

But, I haven't a clue how I'd do this... I'm happy to create another one or two lookup tables if necessary for this...

Any ideas?

<Points will go up to 500 if this can be done...>

Thanks.
LVL 25
InteractiveMindAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WelkinMazeCommented:
Hi,
I think this could be achieved with a bidimendional array for example or with two arrays.

The first array keeps the values that you already have in the array for 'cos' problem. Let's name it A.
The second ones keeps the values from 0 to 90. Let's name it B.

The mapping between the two arrays is done by the index. Thus the acos of A[X] will be the value in B[X].

And if you want to find tha value of acos(Y) you just do a binary search in the array A (since it is a sorted array). This way you find the closest value for Y in A. This value has an index of X. So the wanted acos will be B[X].

This will be a little slower than the solution for cos but has to be quite faster than the standard way.

I have not described it very detailed but I see you have a lot of expert points so I think you'll get it. :)
0
WelkinMazeCommented:
And here's a second suggestion.
It will save the binary search and it is just with one array, more similar to your solution for cos.

In order to use just one array you can divide the interval between 0 and 1 to 90 equal subintervals. Additionaly you have to compute in some standard or other way the values of acon(0), acon(1/90), acon(2/90), ..., acon(1). Put these values in an array with 91 elements. This way the value of acos(X) will be A[(int)(90*X)] where A is the array with the precomputed values.

Again the solution is not described in full details but I think it'll be enough for you.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
InteractiveMindAuthor Commented:
Heyyy Welkin, cheers for the suggestions.

Is there anyway of finding asin and atan without creating a lookup table for them also? (So, find asin and atan based on the acos lookup table alone..)

Like I say, with cos, sin, and tan, there are identites that can be used easily... but I don't know of any for the inverse's...

Thanks.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

ozoCommented:
asin(x) = pi/2-acos(x)
0
InteractiveMindAuthor Commented:
Ah cool;
it there any relation for atan?
0
ozoCommented:
atan(x) = asin(±sqrt(x²/(1+x²)))
0
WelkinMazeCommented:
Yeap, although I'm not sure if the creation of additional lookup arrays is not a better choice especially for atan.
0
InteractiveMindAuthor Commented:
Thanks very much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Math / Science

From novice to tech pro — start learning today.