### Autor Tema: Min/max value  (Leído 3484 veces)

#### BlackCurtain

• Full Member
• Mensajes: 187
• Karma: 9
##### Min/max value
« en: Junio 19, 2012, 07:43:53 pm »
I was kinda shocked to realize that Bennu doesn't have any elementary math functions for returning min/max values between a set of values, like this:
Código: [Seleccionar]
`min_value = min(val1,val2,val3,...); //Return the lowest valuemax_value = max(val1,val2,val3,...); //Return the highest value`Sure it would be easy to write my own function for it, but it still seems like it's something that should exist.
« última modificación: Junio 19, 2012, 07:47:37 pm por BlackCurtain »

#### FreeYourMind

• Hero Member
• Mensajes: 5648
• Karma: 127
##### Re:Min/max value
« Respuesta #1 en: Junio 19, 2012, 07:49:45 pm »
with 2 simple for loops you can get these values easily...

#### BlackCurtain

• Full Member
• Mensajes: 187
• Karma: 9
##### Re:Min/max value
« Respuesta #2 en: Junio 19, 2012, 07:51:59 pm »
with 2 simple for loops you can get these values easily...
Yes, I know, but a native function for it would be even better that could take more than 2 arguments.

Here's my take at it if anyone is in need of a similar function

Código: [Seleccionar]
`function maxValue(int val1, int val2, int val3, int val4)privateint value_args[3];int i;int largest;end   begin            value_args[0] = val1;      value_args[1] = val2;      value_args[2] = val3;      value_args[3] = val4;            largest = value_args[0];            for(i = 0; i <= 3; i++)         if( largest < value_args[i] )            largest = value_args[i];         end      end            return largest;         end`
« última modificación: Junio 19, 2012, 08:10:11 pm por BlackCurtain »

#### SplinterGU

• Hero Member
• Mensajes: 12778
• Karma: 375
##### Re:Min/max value
« Respuesta #3 en: Junio 19, 2012, 08:09:19 pm »
forget more of 2 args

MIN y MAX in C are defines...

use this

Código: [Seleccionar]
`#define min(a,b) ((a)<(b)?(a):(b))#define max(a,b) ((a)>(b)?(a):(b))`
for 4 args you can do

Código: [Seleccionar]
`#define min4(a,b,c,d) min(min(a,b),min(c,d))#define max4(a,b,c,d) max(max(a,b),max(c,d))`
enjoy it!
« última modificación: Junio 19, 2012, 08:12:10 pm por SplinterGU »

#### BlackCurtain

• Full Member
• Mensajes: 187
• Karma: 9
##### Re:Min/max value
« Respuesta #4 en: Junio 19, 2012, 08:24:37 pm »
forget more of 2 args

MIN y MAX in C are defines...

use this

Código: [Seleccionar]
`#define min(a,b) ((a)<(b)?(a):(b))#define max(a,b) ((a)>(b)?(a):(b))`
for 4 args you can do

Código: [Seleccionar]
`#define min4(a,b,c,d) min(min(a,b),min(c,d))#define max4(a,b,c,d) max(max(a,b),max(c,d))`
enjoy it!
Thank you!

#### SplinterGU

• Hero Member
• Mensajes: 12778
• Karma: 375
##### Re:Min/max value
« Respuesta #5 en: Junio 19, 2012, 11:29:12 pm »
no problem...