max和min的算法?(objective - c)

时间:2021-02-28 22:51:34

This is a part of a book I'm reading to learn Objective-C.

这是我正在读的一本书的一部分,目的是学习Objective-C。

The following defines a macro called MAX that gives the maximum of two values: #define MAX(a,b) ( ((a) > (b)) ? (a) : (b) )

下面定义了一个名为MAX的宏,它给出了两个值的最大值:#define MAX(a,b) ((a) > (b)) ?(a)(b)):

And then there are some exercises in the book that asks the reader to define a macro (MIN) to find the minimum of two values and another that asks to define a macro called MAX3 that gives the maximum of 3 values. I think these two definitions will look similar to MAX, but I don't understand how the MAXformula finds the maximum value. I mean if I just did this

然后,书中有一些练习要求读者定义一个宏(MIN)来找到两个值的最小值,另一个要求定义一个名为MAX3的宏,它给出了3个值的最大值。我认为这两个定义看起来类似于MAX,但我不理解MAXformula如何找到最大值。我是说,如果我这么做了。

int limits = MAX (4,8)

It'll just assign limits the value of 8. What does that have to do with finding a variable's maximum value?

它只会赋值8的值。这与找到一个变量的最大值有什么关系?

4 个解决方案

#1


11  

I think you are confusing value and variable. The macro example you listed expands to a comparison between two values and returns the greater of the two values (i.e. which is greater, a or b). So you are right, int limits = MAX(4,8) just assigns 8 to limits and has nothing to do with finding the maximum value you can store in limits.

我认为你混淆了价值和变量。宏的例子你上市扩大到一个比较两个值,并返回两个值的大(即那个是更大的,或b)。所以你是对的,int限制= MAX(4、8)分配8到限制和无关与发现你可以存储的最大值限制。

The header limits.h defines many values like INT_MAX that will tell you information about the min/max values of variable types on your system.

头的限制。h定义了许多像INT_MAX这样的值,它将告诉您系统中变量类型的最小/最大值的信息。

#2


10  

To break it apart:

把它分开:

The declaration:

宣言:

#define MAX(a,b)

If a is greater than b, use a else use b:

如果a大于b,则使用b:

( ((a) > (b)) ? (a) : (b) )

Then to create a MIN expression, use a similar form:

然后创建一个最小表达式,使用类似的形式:

#define MIN(a,b) ( ((a) < (b)) ? (a) : (b) )
                        ^

Then to create a MAX3 expression, you can combine them:

然后创建一个MAX3表达式,您可以组合它们:

#define MAX3(a,b,c) ( MAX(a, MAX(b,c)) )

Specifically, this macro's intended to be used with scalars (C builtins) which can be compared using < or >. If you passed an objc variable, it would result in comparison of addresses and MAX would return the one with the higher address (it would be very rare if you actually wanted to compare addresses of objc instances).

具体地说,这个宏的目的是与标量(C builtins)一起使用,可以使用 <或> 进行比较。如果您传递了一个objc变量,它将会导致地址的比较,而MAX会返回一个具有更高地址的地址(如果您实际上想要比较objc实例的地址,这将非常罕见)。

Also note that this is the classic example of how macros can bite you. With macros, the preprocessor simply expands (textual copy/paste) the parameters in place, so: int limits = MAX (4,8) literally expands to int limits = (4 > 8 ? 4 : 8). If you write MAX(x,++y), then y will be incremented twice if y is greater than or equal to x because it expands to: int limits = (x > ++y ? x : ++y).

还要注意的是,这是一个经典的例子,说明宏可以如何咬你。在宏中,预处理器只是扩展(文本复制/粘贴)参数,所以:int limit = MAX(4,8)实际上扩展到int limit = (4 > 8 ?如果你写的是最大值(x,++y),那么y大于等于x,那么y就会增加2倍,因为它会扩展到:int极限= (x > +y ?x:+ + y)。

#3


0  

generally, you will use a MAX() or MIN() macro to get whichever is the higher/lower of a pair of variables, or of a variable and a constant, or even a pair of macro constants or other non-literal constant expressions. you generally won't supply 2 literal constants as you have done in your question.

通常,您将使用MAX()或MIN()宏来获取两个变量的高/低,或变量和常量,甚至是一对宏常量或其他非文字常量表达式。你通常不会像在你的问题中那样提供2个文字常量。

#4


0  

Algorithm for max (Objective-C)

objective - c算法max()

 // get max value
- (float)maxValue:(NSArray *)arrValue
{
    float maxValue = 0.0;
    for (NSString *value in arrValue) {
        float compareValue = [value floatValue];

        if (compareValue > maxValue) {
            maxValue = compareValue;
        }

    }

    return maxValue;
}


NSArray *number=[NSArray arrayWithObjects:[NSNumber numberWithFloat:57.02], [NSNumber numberWithFloat:55.02], [NSNumber numberWithFloat:45.02], nil];

NSLog(@"%f", [self maxValue:number]);

result 57.020000

#1


11  

I think you are confusing value and variable. The macro example you listed expands to a comparison between two values and returns the greater of the two values (i.e. which is greater, a or b). So you are right, int limits = MAX(4,8) just assigns 8 to limits and has nothing to do with finding the maximum value you can store in limits.

我认为你混淆了价值和变量。宏的例子你上市扩大到一个比较两个值,并返回两个值的大(即那个是更大的,或b)。所以你是对的,int限制= MAX(4、8)分配8到限制和无关与发现你可以存储的最大值限制。

The header limits.h defines many values like INT_MAX that will tell you information about the min/max values of variable types on your system.

头的限制。h定义了许多像INT_MAX这样的值,它将告诉您系统中变量类型的最小/最大值的信息。

#2


10  

To break it apart:

把它分开:

The declaration:

宣言:

#define MAX(a,b)

If a is greater than b, use a else use b:

如果a大于b,则使用b:

( ((a) > (b)) ? (a) : (b) )

Then to create a MIN expression, use a similar form:

然后创建一个最小表达式,使用类似的形式:

#define MIN(a,b) ( ((a) < (b)) ? (a) : (b) )
                        ^

Then to create a MAX3 expression, you can combine them:

然后创建一个MAX3表达式,您可以组合它们:

#define MAX3(a,b,c) ( MAX(a, MAX(b,c)) )

Specifically, this macro's intended to be used with scalars (C builtins) which can be compared using < or >. If you passed an objc variable, it would result in comparison of addresses and MAX would return the one with the higher address (it would be very rare if you actually wanted to compare addresses of objc instances).

具体地说,这个宏的目的是与标量(C builtins)一起使用,可以使用 <或> 进行比较。如果您传递了一个objc变量,它将会导致地址的比较,而MAX会返回一个具有更高地址的地址(如果您实际上想要比较objc实例的地址,这将非常罕见)。

Also note that this is the classic example of how macros can bite you. With macros, the preprocessor simply expands (textual copy/paste) the parameters in place, so: int limits = MAX (4,8) literally expands to int limits = (4 > 8 ? 4 : 8). If you write MAX(x,++y), then y will be incremented twice if y is greater than or equal to x because it expands to: int limits = (x > ++y ? x : ++y).

还要注意的是,这是一个经典的例子,说明宏可以如何咬你。在宏中,预处理器只是扩展(文本复制/粘贴)参数,所以:int limit = MAX(4,8)实际上扩展到int limit = (4 > 8 ?如果你写的是最大值(x,++y),那么y大于等于x,那么y就会增加2倍,因为它会扩展到:int极限= (x > +y ?x:+ + y)。

#3


0  

generally, you will use a MAX() or MIN() macro to get whichever is the higher/lower of a pair of variables, or of a variable and a constant, or even a pair of macro constants or other non-literal constant expressions. you generally won't supply 2 literal constants as you have done in your question.

通常,您将使用MAX()或MIN()宏来获取两个变量的高/低,或变量和常量,甚至是一对宏常量或其他非文字常量表达式。你通常不会像在你的问题中那样提供2个文字常量。

#4


0  

Algorithm for max (Objective-C)

objective - c算法max()

 // get max value
- (float)maxValue:(NSArray *)arrValue
{
    float maxValue = 0.0;
    for (NSString *value in arrValue) {
        float compareValue = [value floatValue];

        if (compareValue > maxValue) {
            maxValue = compareValue;
        }

    }

    return maxValue;
}


NSArray *number=[NSArray arrayWithObjects:[NSNumber numberWithFloat:57.02], [NSNumber numberWithFloat:55.02], [NSNumber numberWithFloat:45.02], nil];

NSLog(@"%f", [self maxValue:number]);

result 57.020000