Wednesday, February 24, 2010

How to get the biggest value (retrieve the greater figure)

If you ever needed the greatest value of 2 variables and wrote an if-then-else statement, there is a simpler solution. Ax has a built-in function to determine the maximum value in a range of variables, namely max.

The use: max(anytype var1,anytype var2);

Where var can be an integer, real, ...

Example:


static void MyMaxJob(Args _args)
{ int test;
int var1=200,var2=300;
;

test = max (var1,var2);

info(int2str(test));
}


Now the documentation suggests this only works with 2 values (or figures), but in practice you can use more values as well. Like this:

static void MyMaxJob(Args _args)
{ int test;
;

test = max (100,1200,300,400,500);

info(int2str(test));
}

This sure gives nicer code that those if-then-else statements, not?

Of course, there is a counterpart function as well for finding the smallest (or lesser) values, min.

(Minus 200, or -200, is smaller than 100. So it's not the absolute value that is taken into account!)

No comments:

Post a Comment