Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Global Constants (http://www.chiefdelphi.com/forums/showthread.php?t=24308)

Ryan Cumings 24-01-2004 13:58

Global Constants
 
I was wondering how I could make a global constant for a trig lookup table.

Adam Shapiro 24-01-2004 16:28

Re: Global Constants
 
You can use a #define statement in a globally-included header file to create global constants. You can also use extern to define global variables.

Ryan Cumings 24-01-2004 16:44

Re: Global Constants
 
When I use extern would the variable be placed in user memory? I have 2k of data for the lookup tables in the trig (this is very little for the 32k of rom space available) and don't want to put these non changing variables in the ram. A define doesn't help because this is an actual table containing data not just one macro that can be quickly changed.

Adam Shapiro 24-01-2004 16:49

Re: Global Constants
 
Unfortunately the extern will be placed in the user memory so, if there is no other way, you might have to try to minimize your table :( .

doy 24-01-2004 17:08

Re: Global Constants
 
couldnt you declare the table rom? look in the c18 compiler reference guide, i think you could just declare
Code:

const rom int trig_lookup[] = {/*info here*/}
and it will put it in rom... you should probably check on that though.

Ryan Cumings 24-01-2004 17:28

Re: Global Constants
 
Ahhh thank you, it appears that should work out well, now the table won't have to be minimized. Seeings as how I don't have the C Reference available and am not able to attend meetings for now due to Academic problems, would you declare this in the header or the C file?

doy 24-01-2004 17:41

Re: Global Constants
 
you would declare it and define it without extern in one .c file, and declare it extern in all other .c files that you want to use it in.

Ryan Cumings 24-01-2004 17:42

Re: Global Constants
 
Thank you for your time.

Guest 26-01-2004 20:48

Re: Global Constants
 
deltacoder already wrote some excellent trig lookup code:

Check the repository: http://nrg.chaosnet.org/repository/i...ral%20C%20Code

deltacoder1020 26-01-2004 21:02

Re: Global Constants
 
note - the code in the repository does not currently utilize the "rom" keyword - it's meant to be compiler/platform independent. I would recommend the addition of the rom and const keywords to the definition of the table array, like so:

const rom unsigned char SIN_LOOKUP[90]

Ryan Cumings 26-01-2004 21:08

Re: Global Constants
 
I think my trig code will run fine,
It has a lookup table with 1440 16-bit values and my sine and cosine functions take very little hit to performance. The angle I use is also in 16-bit straight from the gyro which makes this whole thing very high precision which will be increadably useful for the new drive style (Arcade) I developed and hopefully will get to test Wednesday.

The reason for so many values is that we have 32k of rom sitting there doing nothing. Infact it would be a much more efficient way to calculate out 8-bit values for your angles and multiply it by a 8-bit number for your hypontenuse then shift right by 8. That essentially does floating point for you without the performance hit. Doing it with 16-bits can be a bit trickier but the same principle applies.

deltacoder1020 26-01-2004 21:12

Re: Global Constants
 
essentially, that is what my code does - it stores 8-bit values for the sin function, and then returns those. for newer programmers however, there is also an implementation of the base sin() and cos() functions that return a floating point (basically, the lookup / 255).

Guest 26-01-2004 21:14

Re: Global Constants
 
Ryan:
Can you post your code at the repository? (You'll need to register)

Ryan Cumings 26-01-2004 23:00

Re: Global Constants
 
Posted the code but plum forgot to describe how the values in the table should run, basically they should run something like this, borrowed the table from deltacoder to show as an example. The table can be generated by using
ti=cos(PI / 2 * i / M) * 255
where ti is the table element, i is the element number and M is the number of table elements

cos_lookup_table[90] =
{ 0, 4, 9, 13, 18, 22, 27, 31, 35, 40,
44, 49, 53, 57, 62, 66, 70, 75, 79, 83,
87, 91, 96, 100,104,108,112,116,120,124,
128,131,135,139,142,146,150,153,157,160,
164,167,171,174,177,180,183,186,190,192,
195,198,201,204,206,209,211,214,216,219,
221,223,225,227,229,231,233,235,236,238,
240,241,243,244,245,246,247,248,249,250,
251,252,253,253,254,254,254,255,255,255 };

Darkman_X000 07-02-2004 10:51

Re: Global Constants
 
Quote:

Originally Posted by Adam Shapiro
You can use a #define statement in a globally-included header file to create global constants. You can also use extern to define global variables.

What exactly do you mean by "globally-included header file"? When you use #define to create a global constant, it does not have a type. Do you have to typecast it whenever you use it?

For example:
Code:

user_routines.c
#define axelWidth 1234
...
void fn(void)
{
...
x*=axelWidth;      //Can it be this?
x*=(int)axelWidth; //Or must it be this?
x=x*(axelWidth);  //This seems to be the only form that the compiler likes
...
}
...

In C++ you can declare global constants using const varType varName = initialValue; Is this acceptable in C?

Please clarify!!!


All times are GMT -5. The time now is 22:23.

Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi