• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1707
  • Last Modified:

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.
0
InteractiveMind
Asked:
InteractiveMind
  • 3
  • 3
  • 2
4 Solutions
 
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
 
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
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now