Min/max value

Started by BlackCurtain, June 19, 2012, 07:43:53 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

BlackCurtain

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:

min_value = min(val1,val2,val3,...); //Return the lowest value
max_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.

FreeYourMind

with 2 simple for loops you can get these values easily...

BlackCurtain

#2
Quote from: FreeYourMind on June 19, 2012, 07:49:45 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




function maxValue(int val1, int val2, int val3, int val4)
private
int 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

SplinterGU

#3
forget more of 2 args

MIN y MAX in C are defines...

use this


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


for 4 args you can do


#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!
Download Lastest BennuGD Release: http://www.bennugd.org/node/2

BlackCurtain

Quote from: SplinterGU on June 19, 2012, 08:09:19 PM
forget more of 2 args

MIN y MAX in C are defines...

use this


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


for 4 args you can do


#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

Download Lastest BennuGD Release: http://www.bennugd.org/node/2