Author Topic: Weird values in constants  (Read 2702 times)

Offline hardyx

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 499
    • View Profile
Weird values in constants
« on: 2010-Nov-04 »
I use some constants in my game, including simple constant expressions with other constants. Like this:

Code: (glbasic) [Select]
CONSTANT SCR_WIDTH = 640
CONSTANT TSIDE = 32 // tile side
CONSTANT SCR_COLS = SCR_WIDTH / TSIDE

When I use the SCR_COLS constant in the code, I notice that the value is -1.INF (infinite). I was investigating the code generated and I saw this. When SCR_COLS is defined, TSIDE is initialized to 0. The correct value is set after, because the compiler "reorders" the constant lines :blink:

Code in gpc_tempg.cpp:
Code: (glbasic) [Select]
void __glb_init_globals(){
    SCR_WIDTH= 640;
    SCR_COLS= SCR_WIDTH / TSIDE;    // TSIDE is 0!!
    TSIDE = 32;
}

If I define the constant with a value works ok.

Code: (glbasic) [Select]
CONSTANT SCR_COLS = SCR_WIDTH / 32

I use the last GLBasic version 8.148, but I remember this (constant expressions) worked in a older version (8.0xx I think).
« Last Edit: 2010-Nov-04 by hardyx »

MrTAToad

  • Guest
Re: Weird values in constants
« Reply #1 on: 2010-Nov-04 »
Its not surprising - TSIDE is defined after the SRC_COLS line...

One thing to cant do is assuming the entered order of variables is the same when the program is compiled.

Offline hardyx

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 499
    • View Profile
Re: Weird values in constants
« Reply #2 on: 2010-Nov-04 »
But in my GLBasic code the constants are defined correctly. I know that the code generated can be different or optmized, but with the same effects as the source. If this is normal, then you can't use constants using other constants.

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 932
  • KodeSource
    • View Profile
    • KodeSource
Re: Weird values in constants
« Reply #3 on: 2010-Nov-04 »
I had kinda similar problems way back in version 7.341.
See this forum post:
http://www.glbasic.com/forum/index.php?topic=4575.msg34327#msg34327

But, I wasn't doing any calculations in my definition, just assigning one constant as the value of another.
I haven't checked since version 8 came out, may have been fixed.


Looking at your code in 'gpc_tempg.cpp', does GLBasic just convert our constants into standard C++ variables?
How do normal C++ constants work?  Do they take up memory?  Or just used as a direct text replacement on compile time, using no extra memory?

If using constants takes memory, then what advantages do they have? 
GLOBALs would work the same but without these problems.

GLOBALs are only global to the file they are defined in (unless it's the main project file).
But CONSTANTS are global project wide, no matter what file they are defined in? 
If so, I guess they have that working for them!
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Offline hardyx

  • Community Developer
  • Dr. Type
  • ******
  • Posts: 499
    • View Profile
Re: Weird values in constants
« Reply #4 on: 2010-Nov-04 »
Looking at your code in 'gpc_tempg.cpp', does GLBasic just convert our constants into standard C++ variables?
How do normal C++ constants work?  Do they take up memory?  Or just used as a direct text replacement on compile time, using no extra memory?

If using constants takes memory, then what advantages do they have? 
GLOBALs would work the same but without these problems.
GLBasic converts constants in special variables that you can't write.

C++ constants take memory too. But they say the optimizer "hey, I can't change", and the constant is replaced for the value many times. You use constants when you want to keep the type of the value, or to write cleaner code.

With constants in GLBasic you are sure that you don't change it. And you can use Globals and Constants in other file too.

MrTAToad

  • Guest
Re: Weird values in constants
« Reply #5 on: 2010-Nov-04 »
The result of trying to use contants to calculate a value for storage in a constant should be regarded as unknown...