# lookup

Started by Pseudo_Code, Feb 13 2000 06:05 AM

13 replies to this topic

Sponsor:

###
#2
Members - Reputation: **122**

Posted 13 February 2000 - 07:15 AM

Lookup tables are just pre-computed arrays that you use instead of computing everything in real-time. For example, you could create an array of 360 floats and fill it in with sin(0), sin(1), etc. and instead of having to call the sin function when you need it, you just reference this array.

Simple lookup tables like this can be created when your program starts, but more complicated lookup tables are usually stored on a file somewhere and read in.

Simple lookup tables like this can be created when your program starts, but more complicated lookup tables are usually stored on a file somewhere and read in.

###
#4
Members - Reputation: **349**

Posted 13 February 2000 - 07:20 AM

Say you wanted to create a lookup table for sin values from

0 - 360 it would be something like this(BTW this is in java but it's almost the same as C++)..

sinus = new float[360];

deg2rad = Math.PI/180;

for(int i=0; i<360; i++){

Sinus

Edited by - Wrathnut on 2/13/00 1:24:14 PM

0 - 360 it would be something like this(BTW this is in java but it's almost the same as C++)..

sinus = new float[360];

deg2rad = Math.PI/180;

for(int i=0; i<360; i++){

Sinus

*= (float)Math.sin(i*deg2rad);*

}

where deg2rad is assinged as a float also.

What this does is fill an array full of cosine values. the only thing you would need to do to find the cos(15) would be to use cos[15]. It's actually pretty slick cause it saves alot of computation time if you are doing a lot of trig.

Anyway I hope this helps.. if you need me to do something like this in C++ I could give you an example too.}

where deg2rad is assinged as a float also.

What this does is fill an array full of cosine values. the only thing you would need to do to find the cos(15) would be to use cos[15]. It's actually pretty slick cause it saves alot of computation time if you are doing a lot of trig.

Anyway I hope this helps.. if you need me to do something like this in C++ I could give you an example too.

Edited by - Wrathnut on 2/13/00 1:24:14 PM

###
#12
Members - Reputation: **122**

Posted 14 February 2000 - 11:54 AM

ok, but what about when you are using fractions and such and you want precision? i want to make a lookup table for sines of distances to a point. the distance function will frequently return a fraction. so how would i create a lookup table for fractions (e.g. sin(.88624132))?

_________________Gecko___

_________________Gecko___

###
#13
Moderators - Reputation: **9407**

Posted 14 February 2000 - 06:00 PM

In practice you wouldn''t create a lookup that would match .88624132 exactly. You''d have a lookup for .886 or .8862 maybe. Then when you''d want to index .886 you''d multiply the .886 by say a 1000 to get 886. And use the 886 to index your lookup table. How closely you want to match your fractions depends on how large you want your lookup-tables. The alternative would be to use a hash-table that accepted floating point arguments.

###
#14
Members - Reputation: **122**

Posted 15 February 2000 - 04:29 AM

If you want a little more precision at the cost

of a little speed, consider interpolating your

answer from the two closest lookup in the table.

For example, if you have a result that is 1.4 and

your lookup only shows answers for 1 and 2, your

result should be

lookup[1] + ((lookup[2] - lookup[1]) * 0.4)

This assumes that your functions is a "continuous"

function, but sine and cosine are.