Find the largest number from a set of define and set a define from the answer

I have the following as definitions

#define numA 300
#define numB 200
#define numC 100
#define numD 600

and i would like to find the maximum number and set it to another define variable

#define highestNum

I would like this to be done on compile time so how do i find the largest number using a macro? It could be argued that the user might input it themself since they are setting definitions anyway, but i would like to make it so that the user will only define as little variables as possible

Why not use variables instead of #defines ?

The highestNum is going to be used as part of a preprocessor check of a library im using documentation says i have to define it (dont know why not include it in the library and let the user input it). Im also not aware of a way if a macro can use a variable, but im just following the guide

Something like this maybe

#define highestNum \
(\
	numA > numB ?\
		numA > numC ?\
			numA > numD ?\
				numA :\
			numD :\
		numC :\
	numB > numC ?\
		numB > numD ?\
			numB :\
		numD :\
	numC > numD ?\
		numC :\
	numD \
)

:laughing:

1 Like

If experts would generalize their, um, expertise it would be hekpful.

What and how much can be done along these lines at compile time?

I once heard of a macro pre-processor so able it could be hacked to play a game of tick-tax-tow with the user who invoked the assembler on it. Dunno if the program it made was significant.

Probably beyond the C/C++ preprocessors, but it reminds me that it can do many things I haven’t even dreamt of.

TIA

a7

#define numA 300
#define numB 600
#define numC 100
#define numD 200

#define Max(x,y)    ((x)>(y) ? (x) : (y))

#define Nummax(a,b,c,d) Max((a),Max((b),Max((c),(d))))
1 Like

What is the ' \ ' mean ?

I tested it out and it worked ! May i know the limitations to this? like is there a maximum number ?

It also works with negative numbers which is nice.

the answer must be a difinition just like what guix suggested also

I did change it to

#define numA 300
#define numB 600
#define numC 100
#define numD 200


#define Max(x,y)    ((x)>(y) ? (x) : (y))
#define Nummax Max((numA),Max((numB),Max((numC),(numD))))

and it worked!

Is still possible to shorten it even further to make the sketch looks cleaner?

There is a macro for that:
#define highestNum max(numA, max(numB, max(numC, numD)))

wow that worked too, but where is this ' max ' coming from? its just built in?

Arduino.h (included automatically in your sketch)

Not sure you'll call this cleaner or simpler...:slight_smile: :scream: :innocent: but thought I would throw that out there for the sake of fun

C++ offers great evaluation (even at compile time) capabilities with constexpr and you can put that to work to your benefit by using templated recursive calls..

here is an example:

template <typename T> constexpr T maximum(T a) {return a ;}
template <typename T> constexpr T maximum(T a, T b) {return a > b ? a : b;}
template <typename T, typename... Targs> constexpr T maximum(T first, Targs... rest) {return maximum(first, maximum(rest...));}

#define numA 300
#define numB 600
#define numC 100
#define numD 200
#define numE 400
#define numF 500
#define numG 42

constexpr int numMax = maximum(numA, numB, numC, numD, numE, numF, numG);

void setup() {
  Serial.begin(115200); Serial.println();
  Serial.print(F("Max = ")); Serial.println(numMax);
}

void loop() {}

the great thing about this is — as demonstrated above — that you are no longer obliged to modify the macro if you have more or less than 4 values as the maximum() function takes a variable number of arguments ➜ you now can pass any number (1 or more) of constexpr arguments.

The other nice thing about it is the code size impact➜ NO code has been generated. It's evaluated at compile time for you by the compiler because everything is constexpr.

(of course I would discourage you to use the #define and go for constexpr as well for your numA, numB, ...) as strong typing always helps the compiler understand your intent.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.