Page 1 of 1

NXC: unexpected compiler error

Posted: 13 Jun 2011, 16:42
by HaWe
hi,
I don't know why - but I bet a compiler error with this code:

Code: Select all

// srand test

//********************************************
// randomize functions
//********************************************

// global variables and functions to plant a random seed and to randomize
// how to use:
// srand(x)                     // x = val. for repet. sequences
// myVar = rand_() % 100;       // assignes a random number


unsigned long _RAND_SEED_ = 1;  //  patched by srand()
unsigned long  START_SEED = abs(CurrentTick()*BatteryLevel());


unsigned long rand_()           // random function
{
  _RAND_SEED_ = _RAND_SEED_ * 1103515245 + 12345;
  return (_RAND_SEED_ % (RAND_MAX + 1));
}



void srand(int seed)            // seeds for a new random series
{
  _RAND_SEED_ = seed;           // patch
}

task main(){
  int irVal;
  
  srand(START_SEED);
  irVal=rand_()%100;
  printf("randVal=%3d", irVal);
  while(1) {
    irVal=rand_()%100;
    printf("randVal=%3d", irVal);
    Wait(500);
  }
}
# Error: Invalid constant expression
File "c:\Temp\temp.nxc" ; line 17
# u
#----------------------------------------------------------
I really have no idea why.
Any helping hand...?

Re: NXC: unexpected compiler error

Posted: 13 Jun 2011, 16:53
by spillerrec
You can only assign constants to variable declarations outside a task or function. So the declaration of START_SEED fails because you try to use functions.
I'm not sure if this is an limitation of NXC or if it is also so in C.

I'm not quite sure what you are attempting with this code though...

Re: NXC: unexpected compiler error

Posted: 13 Jun 2011, 17:16
by HaWe
thank you very much!
I rewrote my code, now it works!

the former code was only cut down from another one which I will use to get repetitve random series by srand() and rand_():
srand(0) "real" randomized seed for the random function
srand(1) repeats the series started before
srand(x) starts with a constant seed:

Code: Select all

// srand test

#define printf1( _x, _y, _format1, _value1) { \
  string sval1 = FormatNum(_format1, _value1); \
  TextOut(_x, _y, sval1); \
}


//********************************************
// randomize functions
//********************************************

// global variables and functions to plant a random seed and to randomize
// how to use:
// srand(x)                         // x = val. for repet. sequences
// myVar = rand_() % 100;           // assignes a random number


unsigned long _RAND_SEED_ = 1;      // 1= default value (program start)
unsigned long _OLD_SEED_  = 1;


unsigned long rand_()               // random function
{
  _RAND_SEED_ = _RAND_SEED_ * 1103515245 + 12345;
  return (_RAND_SEED_ % (RAND_MAX + 1));
}



void srand(unsigned long seed)      // seeds for a new random series
{

  if (seed==0)                      // 0: a "real" randomized random seed
    {seed = abs(CurrentTick()*BatteryLevel());}  // substitute to time(0)

  else
  if (seed==-1) {                   // -1: restore last random series
    seed = _OLD_SEED_;
  }

  _OLD_SEED_  = seed;
  _RAND_SEED_ = seed;               // patch for rand_ function
}



task main(){
  int irVal;
  
  printf1(0,56, "new1:%10d", _OLD_SEED_); // no srand: default=1
  irVal=rand_()%100;
  printf1( 0,48, "r1= %2d", irVal);
  irVal=rand_()%100;
  printf1(48,48, "r2= %2d", irVal);
  
  srand(0);                               // 0: real" randomized random seed
  printf1(0,40, "new0:%10d", _OLD_SEED_);
  irVal=rand_()%100;
  printf1( 0,32, "r1= %2d", irVal);
  irVal=rand_()%100;
  printf1(48,32, "r2= %2d", irVal);
  
  srand(-1);
  printf1(0,24, "-1re:%10d", _OLD_SEED_); // -1: again prev. random series
  irVal=rand_()%100;
  printf1( 0,16, "r1= %2d", irVal);
  irVal=rand_()%100;
  printf1(48,16, "r2= %2d", irVal);
  
  srand(0);
  printf1(0, 8, "new0:%10d", _OLD_SEED_); // 0: real" randomized random seed
  irVal=rand_()%100;
  printf1( 0, 0, "r1= %2d", irVal);
  irVal=rand_()%100;
  printf1(48, 0, "r2= %2d", irVal);
  

  while(1) {}

}

_RAND_SEED_ and _OLD_SEED_ are usually not needed to know, it's only for internal rand function use and in the example only showed for verifying.

Re: NXC: unexpected compiler error

Posted: 14 Jun 2011, 00:23
by muntoo
It's legal C++. This is a NXC limitation.