Integer division

 
#property strict
#define SOME_LENGTH     5
#define TEMP            (int)NormalizeDouble(MathPow(10, SOME_LENGTH), 0)
#define DIVIDER         TEMP * 10
#define DIVIDER_CORRECT int(TEMP * 10)

void OnStart()
  {
   int someIntegerValue = 12345678;
   Alert("using DIVIDER: ", someIntegerValue / DIVIDER);
   Alert("using DIVIDER_CORRECT: ", someIntegerValue / DIVIDER_CORRECT);
  }

Can anybody please tell me why the results are different? What is the reason?

 

While the values are equal:

#property strict
#define SOME_LENGTH     5
#define TEMP            (int)NormalizeDouble(MathPow(10, SOME_LENGTH), 0)
#define DIVIDER         TEMP * 10
#define DIVIDER_CORRECT int(TEMP * 10)

void OnStart()
  {
   Alert("DIVIDER = ", DIVIDER);
   Alert("DIVIDER_CORRECT = ", DIVIDER_CORRECT);
   Alert("Are equal: ", DIVIDER == DIVIDER_CORRECT);
  }


 
Vladislav Boyko: Can anybody please tell me why the results are different? What is the reason?

The reason is because you are using macros without properly isolating your values and the rules of precedence take over

someIntegerValue / DIVIDER -> someIntegerValue / TEMP * 10 -> ( someIntegerValue / TEMP ) * 10

When using macros, always isolate your values.

#define DIVIDER         ( TEMP * 10 )
 
Your code
#define DIVIDER         TEMP * 10
Alert("using DIVIDER: ", someIntegerValue / DIVIDER);
This means
(12345678 / TEMP)*10
It does not mean
12345678 / (TEMP * 10)

If you insist on using #defines make sure the definition is one token

#define DIVIDER         (TEMP * 10)
 
I understand, thank you very much
 
Vladislav Boyko #:
I understand, thank you very much

And just to add to that, I suggest using variables in this case.

Macro definitions are useful in many situations, but variables have the advantage of being visible in the debugger during runtime, so less ambiguity/guesswork.

You can inspect the actual values like so :


 
R4tna C #:

And just to add to that, I suggest using variables in this case.

Macro definitions are useful in many situations, but variables have the advantage of being visible in the debugger during runtime, so less ambiguity/guesswork.

You can inspect the actual values like so :


Yes, that's what I did

I just use global "const int/string/double" instead of "#define"

Thanks

Reason: