Mindstorm
Mindstorm

Reputation: 443

Error when using comparison operators in conjunction with macros

And I'm using an expression in my code which is as follows:

max( close[i-1][ii], open[i-1][ii] ) < max( close[i-2][ii], open[i-2][ii] )

However, this returns the following warning:

warning: comparisons like X<=Y<=Z do not have their mathematical meaning

I believe the preprocessor is doing something of the sort of a<b<c replacement which might explain the warning from the compiler. How do I resolve this?

Upvotes: 1

Views: 1009

Answers (2)

Vaughn Cato
Vaughn Cato

Reputation: 64308

Macros just replace text so you need to parenthesize everything.

#define min(a,b) (((a)<(b))?(a):(b))
#define max(a,b) (((a)>(b))?(a):(b))

If you are using C++, there are much better alternatives though. Inline functions have the same performance as macros while being much better behaved.

Upvotes: 1

EvilTeach
EvilTeach

Reputation: 28882

use std::max and std::min instead

Your macros have side effects the way you are using them.

Upvotes: 2

Related Questions