Solved

# Trig lookup table

Posted on 2006-04-08
1,599 Views
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.
0
Question by:InteractiveMind

LVL 11

Assisted Solution

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

LVL 11

Accepted Solution

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

LVL 25

Author Comment

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

LVL 84

Assisted Solution

asin(x) = pi/2-acos(x)
0

LVL 25

Author Comment

Ah cool;
it there any relation for atan?
0

LVL 84

Assisted Solution

atan(x) = asin(±sqrt(x²/(1+x²)))
0

LVL 11

Expert Comment

Yeap, although I'm not sure if the creation of additional lookup arrays is not a better choice especially for atan.
0

LVL 25

Author Comment

Thanks very much.
0

## Featured Post

### Suggested Solutions

We are taking giant steps in technological advances in the field of wireless telephony. At just 10 years since the advent of smartphones, it is crucial to examine the benefits and disadvantages that have been report to us.
This is a research brief on the potential colonization of humans on Mars.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.