paizza paizza - 2 months ago 6
C++ Question

Are define processed differently?

Why this code give to me value "4" instead of "0"?

#define PLUGIN_PPQ 96
#define MIDIENGINE_SCORE_LENGTH PLUGIN_PPQ * 4

int main ()
{
int mCurrentPatternPulse = 97;
int patternBar = (int)floor(mCurrentPatternPulse / MIDIENGINE_SCORE_LENGTH);
cout << "value: " << patternBar << " (" << MIDIENGINE_SCORE_LENGTH << ")";
}


97/384 (with/without floor) should give to me 0.

But it seems it divides by 96 instead of 384? Even if I print
MIDIENGINE_SCORE_LENGTH
is 384...

Answer

Imagine it being a "string-replace" and not a math operation.

So MIDIENGINE_SCORE_LENGTHis not 384 but 96 *4

so your code looks like:

floor(mCurrentPatternPulse / 96 *4 );

and the mCurrentPatternPulse / 96 will be evaluated first.

Just add some brackets:

floor(mCurrentPatternPulse / ( MIDIENGINE_SCORE_LENGTH ) );

Edit:

Or even better put them in the define directly:

#define MIDIENGINE_SCORE_LENGTH ( PLUGIN_PPQ * 4 )