开发者

Compare 4 float values for the highest value

Having trouble comparing 4 float values in objective c, I would like to return the highest or equal values of these 4 integers. As you can see two are the same here.

This is the NSLOG output

2011-08-1开发者_如何转开发7 17:17:30.328 [21087:c203] 1 = 6.000000
2011-08-17 17:17:30.328 [21087:c203] 2 = 4.400000
2011-08-17 17:17:30.352 [21087:c203] 3 = 0.000000
2011-08-17 17:17:30.353 [21087:c203] 4 = 6.000000

Lets say 1-4

I have tried the MAX(1,2) Statement Also checked >= value using if and statements too..

I am able to write my own code for comparing the values using an algorythm but I wanted to check to see if there is an easier way to do it and save me some headache??

Perhaps creating a float with a really high value and then comparing which one was the closest to it??

I have tried reading this site and its just confusing me

I am kind of new to objective c, have done it for 2 years but still sometimes it stumps you!!

Edit - Solved by brain!..

2011-08-18 12:10:26.798 iT[2093:c203] 1 = 6.710000 
2011-08-18 12:10:26.798 iT[2093:c203] 2 = 0.000000 
2011-08-18 12:10:26.799 iT[2093:c203] 3 = 7.000000 
2011-08-18 12:10:26.800 iT[2093:c203] 4 is 9.000000 

float max = MAX(1,MAX(2,MAX(3,4))); NSLog(@"Max is %f", max); 

NSLog Output "Max is 9.000000"

Such an easy way to solve it, Brilliant!


Seems pretty simple to me to just nest the max calls:

max(1,max(2,max(3,4)))

Where 1,2,3,4 refer to the variable names.

Usually this sort of thing is done over collections, in which case you can so something like this (in pseudocode):

float max = Float.Min_Value;
for (float f in float_array) {
    if (f > max) {
        max = f;
    }
}


Do you mean MAX(MAX(6.0000,4.0000),MAX(0.0000,6.00000))? this should return 6.0000.

EDIT - some examples:

int i = MAX(1,2);

in this case i would be set to 2.

int j = MAX(MAX(1,2),MAX(3,3));

This is simply nesting the functions, so would equate to:

MAX(2,3);

and j would be set to 3. As you can see there is no concept of equal values.

HTH Dave

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜