如何可移植地找到最小值(INT_MAX, abs(INT_MIN))?

时间:2022-08-24 00:19:19

How can I portably find out the smallest of INT_MAX and abs(INT_MIN)? (That's the mathematical absolute value of INT_MIN, not a call to the abs function.)

我怎么能很容易地找到最小的INT_MAX和abs(INT_MIN)呢?(这是INT_MIN的数学绝对值,不是对abs函数的调用。)

It should be as same as INT_MAX in most systems, but I'm looking for a more portable way.

它应该和大多数系统中的INT_MAX一样,但是我正在寻找一种更便携的方式。

6 个解决方案

#1


22  

While the typical value of INT_MIN is -2147483648, and the typical value of INT_MAX is 2147483647, it is not guaranteed by the standard. TL;DR: The value you're searching for is INT_MAX in a conforming implementation. But calculating min(INT_MAX, abs(INT_MIN)) isn't portable.

INT_MIN的典型值为-2147483648,而INT_MAX的典型值为2147483647,则不受标准的保证。您正在搜索的值是符合标准的实现中的INT_MAX。但是计算min(INT_MAX, abs(INT_MIN))不是可移植的。


The possible values of INT_MIN and INT_MAX

INT_MIN and INT_MAX are defined by the Annex E (Implementation limits) 1 (C standard, C++ inherits this stuff):

INT_MIN和INT_MAX由附件E(实现限制)1 (C标准,c++继承此内容)定义:

The contents of the header are given below, in alphabetical order. The minimum magnitudes shown shall be replaced by implementation-defined magnitudes with the same sign. The values shall all be constant expressions suitable for use in #if preprocessing directives. The components are described further in 5.2.4.2.1.

标题的内容按字母顺序如下所示。所示的最小模量应由实作定义的模量替换为相同的符号。这些值都应该是适用于#if预处理指令的常量表达式。在5.2.4.2.1中进一步描述了这些组件。

[...]

[…]

#define INT_MAX +32767

#定义INT_MAX + 32767

#define INT_MIN -32767

#定义INT_MIN -32767

[...]

[…]

The standard requires the type int to be an integer type that can represent the range [INT_MIN, INT_MAX] (section 5.2.4.2.1.).

标准要求类型int是一个整数类型,可以表示范围[INT_MIN, INT_MAX](第5.2.4.2.1节)。

Then, 6.2.6.2. (Integer types, again part of the C standard), comes into play and further restricts this to what we know as two's or ones' complement:

然后,6.2.6.2。(整数类型,同样是C标准的一部分),开始发挥作用,并进一步将其限制为我们所知的二或一的补码:

For signed integer types, the bits of the object representation shall be divided into three groups: value bits, padding bits, and the sign bit. There need not be any padding bits; signed char shall not have any padding bits. There shall be exactly one sign bit. Each bit that is a value bit shall have the same value as the same bit in the object representation of the corresponding unsigned type (if there are M value bits in the signed type and N in the unsigned type, then M ≤ N). If the sign bit is zero, it shall not affect the resulting value. If the sign bit is one, the value shall be modified in one of the following ways:

对于有符号整数类型,对象表示形式的位应该分为三组:值位、填充位和符号位。不需要有任何填充位;签名字符不应有任何填充位。只有一个符号位。每个比特值一点一点有相同的值作为相应的对象表示无符号类型(如果有M值位签署类型和N的无符号类型,然后M≤N),如果符号位为零,不得影响结果值。如果符号位为1,则对其进行如下修改:

— the corresponding value with sign bit 0 is negated (sign and magnitude);

-符号位为0的对应值被否定(符号和幅度);

— the sign bit has the value −(2M) (two’s complement);

符号位的值−(2米)(二进制补码);

— the sign bit has the value −(2M − 1) (ones’ complement).

——符号位的值−−1(2米)(补充)。

Section 6.2.6.2. is also very important to relate the value representation of the signed integer types with the value representation of its unsigned siblings.

部分6.2.6.2。将带符号整数类型的值表示与其无符号兄弟类型的值表示相关联也是非常重要的。

This means, you either get the range [-(2^n - 1), (2^n - 1)] or [-2^n, (2^n - 1)], where n is typically 15 or 31.

这意味着,你可以把范围(-(2 ^ n - 1)(2 ^ n - 1)]或[2 ^ n(2 ^ n - 1)),其中n是通常15 - 31所示。

Operations on signed integer types

Now for the second thing: Operations on signed integer types, that result in a value that is not within the range [INT_MIN, INT_MAX], the behavior is undefined. This is explicitly mandated in C++ by Paragraph 5/4:

现在,对于第二件事:对有符号整数类型的操作,结果是一个不在范围内的值[INT_MIN, INT_MAX],行为是未定义的。在c++中,第5/4段明确规定了这一点:

If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.

如果在计算表达式的过程中,结果在数学上没有定义,或者在其类型的可表示值范围内没有定义,则行为是未定义的。

For C, 6.5/5 offers a very similar passage:

对于C, 6.5/5提供了一个非常相似的段落:

If an exceptional condition occurs during the evaluation of an expression (that is, if the result is not mathematically defined or not in the range of representable values for its type), the behavior is undefined.

如果在表达式的求值过程中发生异常情况(即,如果结果没有在数学上定义,或者在其类型的可表示值范围内没有定义),则该行为是未定义的。

So what happens if the value of INT_MIN happens to be less than the negative of INT_MAX (e.g. -32768 and 32767 respectively)? Calculating -(INT_MIN) will be undefined, the same as INT_MAX + 1.

那么,如果INT_MIN的值恰好小于INT_MAX的负值(分别为-32768和32767)会发生什么情况呢?计算-(INT_MIN)将未定义,与INT_MAX + 1相同。

So we need to avoid ever calculating a value that may isn't in the range of [INT_MIN, INT_MAX]. Lucky, INT_MAX + INT_MIN is always in that range, as INT_MAX is a strictly positive value and INT_MIN a strictly negative value. Hence INT_MIN < INT_MAX + INT_MIN < INT_MAX.

因此,我们需要避免计算可能不在[INT_MIN, INT_MAX]范围内的值。幸运的是,INT_MAX + INT_MIN始终在这个范围内,因为INT_MAX是一个严格的正值,INT_MIN是一个严格的负值。因此,INT_MIN < INT_MAX + INT_MIN < INT_MAX。

Now we can check, whether, INT_MAX + INT_MIN is equal to, less than, or greater than 0.

现在我们可以检查,INT_MAX + INT_MIN是否等于,小于或大于0。

`INT_MAX + INT_MIN`  |  value of -INT_MIN    | value of -INT_MAX 
------------------------------------------------------------------
         < 0         |  undefined            | -INT_MAX
         = 0         |  INT_MAX = -INT_MIN   | -INT_MAX = INT_MIN
         > 0         |  cannot occur according to 6.2.6.2. of the C standard

Hence, to determine the minimum of INT_MAX and -INT_MIN (in the mathematical sense), the following code is sufficient:

因此,要确定INT_MAX和-INT_MIN的最小值(在数学意义上),以下代码是充分的:

if ( INT_MAX + INT_MIN == 0 )
{
    return INT_MAX; // or -INT_MIN, it doesn't matter
}
else if ( INT_MAX + INT_MIN < 0 )
{
    return INT_MAX; // INT_MAX is smaller, -INT_MIN cannot be represented.
}
else // ( INT_MAX + INT_MIN > 0 )
{
    return -INT_MIN; // -INT_MIN is actually smaller than INT_MAX, may not occur in a conforming implementation.
}

Or, to simplify:

或者,为了简化:

return (INT_MAX + INT_MIN <= 0) ? INT_MAX : -INT_MIN;

The values in a ternary operator will only be evaluated if necessary. Hence, -INT_MIN is either left unevaluated (therefore cannot produce UB), or is a well-defined value.

三元运算符中的值仅在必要时才进行计算。因此,-INT_MIN要么是未计算的(因此不能生成UB),要么是定义良好的值。

Or, if you want an assertion:

或者,如果你想要一个断言:

assert(INT_MAX + INT_MIN <= 0);
return INT_MAX;

Or, if you want that at compile time:

或者,如果你想在编译时这样做:

static_assert(INT_MAX + INT_MIN <= 0, "non-conforming implementation");
return INT_MAX;

Getting integer operations right (i.e. if correctness matters)

If you're interested in safe integer arithmetic, have a look at my implementation of safe integer operations. If you want to see the patterns (rather than this lengthy text output) on which operations fail and which succeed, choose this demo.

如果您对安全整数算法感兴趣,请查看我的安全整数操作实现。如果您想查看操作失败和成功的模式(而不是这个冗长的文本输出),请选择这个演示。

Depending on the architecture, there may be other options to ensure correctness, such as gcc's option -ftrapv.

根据体系结构的不同,可能还有其他选项来确保正确性,比如gcc的选项-ftrapv。

#2


10  

INT_MAX + INT_MIN < 0 ? INT_MAX : -INT_MIN

Edited to add explanation: Of course the difficulty is that -INT_MIN or abs(INT_MIN) will be undefined if -INT_MIN is too big to fit in an int. So we need some way of checking whether this is the case. The condition INT_MAX + INT_MIN < 0 tests whether -INT_MIN is greater than INT_MAX. If it is, then INT_MAX is the smaller of the two absolute values. If not, then INT_MAX is the larger of the two absolute values, and -INT_MIN is the correct answer.

编辑添加说明:当然,难点在于-INT_MIN或abs(INT_MIN)如果-INT_MIN太大而不能放入int,那么就无法定义。所以我们需要一些检查是否为这个情况的方法。条件INT_MAX + INT_MIN < 0测试-INT_MIN是否大于INT_MAX。如果是,则INT_MAX是两个绝对值中较小的一个。如果不是,则INT_MAX是两个绝对值中较大的一个,而-INT_MIN是正确的答案。

#3


7  

In C99 and above, INT_MAX.

在C99和以上,INT_MAX。

Quoth the spec:

说规范:

For signed integer types, the bits of the object representation shall be divided into three groups: value bits, padding bits, and the sign bit. There need not be any padding bits; signed char shall not have any padding bits. There shall be exactly one sign bit. Each bit that is a value bit shall have the same value as the same bit in the object representation of the corresponding unsigned type (if there are M value bits in the signed type and N in the unsigned type, then M ≤ N). If the sign bit is zero, it shall not affect the resulting value. If the sign bit is one, the value shall be modified in one of the following ways:

对于有符号整数类型,对象表示形式的位应该分为三组:值位、填充位和符号位。不需要有任何填充位;签名字符不应有任何填充位。只有一个符号位。每个比特值一点一点有相同的值作为相应的对象表示无符号类型(如果有M值位签署类型和N的无符号类型,然后M≤N),如果符号位为零,不得影响结果值。如果符号位为1,则对其进行如下修改:

  • the corresponding value with sign bit 0 is negated (sign and magnitude);
  • 符号位为0的对应值被否定(符号和大小);
  • the sign bit has the value −(2^M) (two’s complement);
  • 符号位的值−(2 ^ M)(二进制补码);
  • the sign bit has the value −(2^M − 1) (ones’ complement).
  • 符号位的值−−1)(2 ^米(补充)。

(Section 6.2.6.2 of http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf)

(部分6.2.6.2 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf)

#4


1  

-INT_MAX is representable as an int in all C and C++ dialects, as far as I know. Therefore:

据我所知,在所有C和c++方言中,-INT_MAX都可以表示为int类型。因此:

-INT_MAX <= INT_MIN ? -INT_MIN : INT_MAX

#5


0  

On most systems, abs (INT_MIN) is not defined. For example, on typical 32 bit machines, INT_MAX = 2^31 - 1, INT_MIN = - 2^31, and abs (INT_MIN) cannot be 2^31.

在大多数系统中,没有定义abs (INT_MIN)。例如,在典型的32位机器上,INT_MAX = 2 ^ 31 - 1,INT_MIN = - 2 ^ 31,abs(INT_MIN)不能2 ^ 31。

#6


0  

abs(INT_MIN) will invoke undefined behavior. Standard says

abs(INT_MIN)将调用未定义的行为。标准说

7.22.6.1 The abs, labs and llabs functions:

The abs, labs, and llabs functions compute the absolute value of an integer j. If the result cannot be represented, the behavior is undefined.

abs、labs和llabs函数计算整数j的绝对值。如果结果无法表示,则行为未定义。

Try this instead :
Convert INT_MIN to unsignrd int. Since -ve numbers can't be represented as an unsigned int, INT_MAX will be converted to UINT_MAX + 1 + INT_MIN.

试试这个:将INT_MIN转换为unsignrd int.因为-ve数字不能表示为无符号int,所以INT_MAX将被转换为UINT_MAX + 1 + INT_MIN。

#include <stdio.h>
#include <stdlib.h>

unsigned min(unsigned a, unsigned b)
{
    return a < b ? a : b;
}

int main(void)
{
    printf("%u\n", min(INT_MAX, INT_MIN));
}  

#1


22  

While the typical value of INT_MIN is -2147483648, and the typical value of INT_MAX is 2147483647, it is not guaranteed by the standard. TL;DR: The value you're searching for is INT_MAX in a conforming implementation. But calculating min(INT_MAX, abs(INT_MIN)) isn't portable.

INT_MIN的典型值为-2147483648,而INT_MAX的典型值为2147483647,则不受标准的保证。您正在搜索的值是符合标准的实现中的INT_MAX。但是计算min(INT_MAX, abs(INT_MIN))不是可移植的。


The possible values of INT_MIN and INT_MAX

INT_MIN and INT_MAX are defined by the Annex E (Implementation limits) 1 (C standard, C++ inherits this stuff):

INT_MIN和INT_MAX由附件E(实现限制)1 (C标准,c++继承此内容)定义:

The contents of the header are given below, in alphabetical order. The minimum magnitudes shown shall be replaced by implementation-defined magnitudes with the same sign. The values shall all be constant expressions suitable for use in #if preprocessing directives. The components are described further in 5.2.4.2.1.

标题的内容按字母顺序如下所示。所示的最小模量应由实作定义的模量替换为相同的符号。这些值都应该是适用于#if预处理指令的常量表达式。在5.2.4.2.1中进一步描述了这些组件。

[...]

[…]

#define INT_MAX +32767

#定义INT_MAX + 32767

#define INT_MIN -32767

#定义INT_MIN -32767

[...]

[…]

The standard requires the type int to be an integer type that can represent the range [INT_MIN, INT_MAX] (section 5.2.4.2.1.).

标准要求类型int是一个整数类型,可以表示范围[INT_MIN, INT_MAX](第5.2.4.2.1节)。

Then, 6.2.6.2. (Integer types, again part of the C standard), comes into play and further restricts this to what we know as two's or ones' complement:

然后,6.2.6.2。(整数类型,同样是C标准的一部分),开始发挥作用,并进一步将其限制为我们所知的二或一的补码:

For signed integer types, the bits of the object representation shall be divided into three groups: value bits, padding bits, and the sign bit. There need not be any padding bits; signed char shall not have any padding bits. There shall be exactly one sign bit. Each bit that is a value bit shall have the same value as the same bit in the object representation of the corresponding unsigned type (if there are M value bits in the signed type and N in the unsigned type, then M ≤ N). If the sign bit is zero, it shall not affect the resulting value. If the sign bit is one, the value shall be modified in one of the following ways:

对于有符号整数类型,对象表示形式的位应该分为三组:值位、填充位和符号位。不需要有任何填充位;签名字符不应有任何填充位。只有一个符号位。每个比特值一点一点有相同的值作为相应的对象表示无符号类型(如果有M值位签署类型和N的无符号类型,然后M≤N),如果符号位为零,不得影响结果值。如果符号位为1,则对其进行如下修改:

— the corresponding value with sign bit 0 is negated (sign and magnitude);

-符号位为0的对应值被否定(符号和幅度);

— the sign bit has the value −(2M) (two’s complement);

符号位的值−(2米)(二进制补码);

— the sign bit has the value −(2M − 1) (ones’ complement).

——符号位的值−−1(2米)(补充)。

Section 6.2.6.2. is also very important to relate the value representation of the signed integer types with the value representation of its unsigned siblings.

部分6.2.6.2。将带符号整数类型的值表示与其无符号兄弟类型的值表示相关联也是非常重要的。

This means, you either get the range [-(2^n - 1), (2^n - 1)] or [-2^n, (2^n - 1)], where n is typically 15 or 31.

这意味着,你可以把范围(-(2 ^ n - 1)(2 ^ n - 1)]或[2 ^ n(2 ^ n - 1)),其中n是通常15 - 31所示。

Operations on signed integer types

Now for the second thing: Operations on signed integer types, that result in a value that is not within the range [INT_MIN, INT_MAX], the behavior is undefined. This is explicitly mandated in C++ by Paragraph 5/4:

现在,对于第二件事:对有符号整数类型的操作,结果是一个不在范围内的值[INT_MIN, INT_MAX],行为是未定义的。在c++中,第5/4段明确规定了这一点:

If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.

如果在计算表达式的过程中,结果在数学上没有定义,或者在其类型的可表示值范围内没有定义,则行为是未定义的。

For C, 6.5/5 offers a very similar passage:

对于C, 6.5/5提供了一个非常相似的段落:

If an exceptional condition occurs during the evaluation of an expression (that is, if the result is not mathematically defined or not in the range of representable values for its type), the behavior is undefined.

如果在表达式的求值过程中发生异常情况(即,如果结果没有在数学上定义,或者在其类型的可表示值范围内没有定义),则该行为是未定义的。

So what happens if the value of INT_MIN happens to be less than the negative of INT_MAX (e.g. -32768 and 32767 respectively)? Calculating -(INT_MIN) will be undefined, the same as INT_MAX + 1.

那么,如果INT_MIN的值恰好小于INT_MAX的负值(分别为-32768和32767)会发生什么情况呢?计算-(INT_MIN)将未定义,与INT_MAX + 1相同。

So we need to avoid ever calculating a value that may isn't in the range of [INT_MIN, INT_MAX]. Lucky, INT_MAX + INT_MIN is always in that range, as INT_MAX is a strictly positive value and INT_MIN a strictly negative value. Hence INT_MIN < INT_MAX + INT_MIN < INT_MAX.

因此,我们需要避免计算可能不在[INT_MIN, INT_MAX]范围内的值。幸运的是,INT_MAX + INT_MIN始终在这个范围内,因为INT_MAX是一个严格的正值,INT_MIN是一个严格的负值。因此,INT_MIN < INT_MAX + INT_MIN < INT_MAX。

Now we can check, whether, INT_MAX + INT_MIN is equal to, less than, or greater than 0.

现在我们可以检查,INT_MAX + INT_MIN是否等于,小于或大于0。

`INT_MAX + INT_MIN`  |  value of -INT_MIN    | value of -INT_MAX 
------------------------------------------------------------------
         < 0         |  undefined            | -INT_MAX
         = 0         |  INT_MAX = -INT_MIN   | -INT_MAX = INT_MIN
         > 0         |  cannot occur according to 6.2.6.2. of the C standard

Hence, to determine the minimum of INT_MAX and -INT_MIN (in the mathematical sense), the following code is sufficient:

因此,要确定INT_MAX和-INT_MIN的最小值(在数学意义上),以下代码是充分的:

if ( INT_MAX + INT_MIN == 0 )
{
    return INT_MAX; // or -INT_MIN, it doesn't matter
}
else if ( INT_MAX + INT_MIN < 0 )
{
    return INT_MAX; // INT_MAX is smaller, -INT_MIN cannot be represented.
}
else // ( INT_MAX + INT_MIN > 0 )
{
    return -INT_MIN; // -INT_MIN is actually smaller than INT_MAX, may not occur in a conforming implementation.
}

Or, to simplify:

或者,为了简化:

return (INT_MAX + INT_MIN <= 0) ? INT_MAX : -INT_MIN;

The values in a ternary operator will only be evaluated if necessary. Hence, -INT_MIN is either left unevaluated (therefore cannot produce UB), or is a well-defined value.

三元运算符中的值仅在必要时才进行计算。因此,-INT_MIN要么是未计算的(因此不能生成UB),要么是定义良好的值。

Or, if you want an assertion:

或者,如果你想要一个断言:

assert(INT_MAX + INT_MIN <= 0);
return INT_MAX;

Or, if you want that at compile time:

或者,如果你想在编译时这样做:

static_assert(INT_MAX + INT_MIN <= 0, "non-conforming implementation");
return INT_MAX;

Getting integer operations right (i.e. if correctness matters)

If you're interested in safe integer arithmetic, have a look at my implementation of safe integer operations. If you want to see the patterns (rather than this lengthy text output) on which operations fail and which succeed, choose this demo.

如果您对安全整数算法感兴趣,请查看我的安全整数操作实现。如果您想查看操作失败和成功的模式(而不是这个冗长的文本输出),请选择这个演示。

Depending on the architecture, there may be other options to ensure correctness, such as gcc's option -ftrapv.

根据体系结构的不同,可能还有其他选项来确保正确性,比如gcc的选项-ftrapv。

#2


10  

INT_MAX + INT_MIN < 0 ? INT_MAX : -INT_MIN

Edited to add explanation: Of course the difficulty is that -INT_MIN or abs(INT_MIN) will be undefined if -INT_MIN is too big to fit in an int. So we need some way of checking whether this is the case. The condition INT_MAX + INT_MIN < 0 tests whether -INT_MIN is greater than INT_MAX. If it is, then INT_MAX is the smaller of the two absolute values. If not, then INT_MAX is the larger of the two absolute values, and -INT_MIN is the correct answer.

编辑添加说明:当然,难点在于-INT_MIN或abs(INT_MIN)如果-INT_MIN太大而不能放入int,那么就无法定义。所以我们需要一些检查是否为这个情况的方法。条件INT_MAX + INT_MIN < 0测试-INT_MIN是否大于INT_MAX。如果是,则INT_MAX是两个绝对值中较小的一个。如果不是,则INT_MAX是两个绝对值中较大的一个,而-INT_MIN是正确的答案。

#3


7  

In C99 and above, INT_MAX.

在C99和以上,INT_MAX。

Quoth the spec:

说规范:

For signed integer types, the bits of the object representation shall be divided into three groups: value bits, padding bits, and the sign bit. There need not be any padding bits; signed char shall not have any padding bits. There shall be exactly one sign bit. Each bit that is a value bit shall have the same value as the same bit in the object representation of the corresponding unsigned type (if there are M value bits in the signed type and N in the unsigned type, then M ≤ N). If the sign bit is zero, it shall not affect the resulting value. If the sign bit is one, the value shall be modified in one of the following ways:

对于有符号整数类型,对象表示形式的位应该分为三组:值位、填充位和符号位。不需要有任何填充位;签名字符不应有任何填充位。只有一个符号位。每个比特值一点一点有相同的值作为相应的对象表示无符号类型(如果有M值位签署类型和N的无符号类型,然后M≤N),如果符号位为零,不得影响结果值。如果符号位为1,则对其进行如下修改:

  • the corresponding value with sign bit 0 is negated (sign and magnitude);
  • 符号位为0的对应值被否定(符号和大小);
  • the sign bit has the value −(2^M) (two’s complement);
  • 符号位的值−(2 ^ M)(二进制补码);
  • the sign bit has the value −(2^M − 1) (ones’ complement).
  • 符号位的值−−1)(2 ^米(补充)。

(Section 6.2.6.2 of http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf)

(部分6.2.6.2 http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf)

#4


1  

-INT_MAX is representable as an int in all C and C++ dialects, as far as I know. Therefore:

据我所知,在所有C和c++方言中,-INT_MAX都可以表示为int类型。因此:

-INT_MAX <= INT_MIN ? -INT_MIN : INT_MAX

#5


0  

On most systems, abs (INT_MIN) is not defined. For example, on typical 32 bit machines, INT_MAX = 2^31 - 1, INT_MIN = - 2^31, and abs (INT_MIN) cannot be 2^31.

在大多数系统中,没有定义abs (INT_MIN)。例如,在典型的32位机器上,INT_MAX = 2 ^ 31 - 1,INT_MIN = - 2 ^ 31,abs(INT_MIN)不能2 ^ 31。

#6


0  

abs(INT_MIN) will invoke undefined behavior. Standard says

abs(INT_MIN)将调用未定义的行为。标准说

7.22.6.1 The abs, labs and llabs functions:

The abs, labs, and llabs functions compute the absolute value of an integer j. If the result cannot be represented, the behavior is undefined.

abs、labs和llabs函数计算整数j的绝对值。如果结果无法表示,则行为未定义。

Try this instead :
Convert INT_MIN to unsignrd int. Since -ve numbers can't be represented as an unsigned int, INT_MAX will be converted to UINT_MAX + 1 + INT_MIN.

试试这个:将INT_MIN转换为unsignrd int.因为-ve数字不能表示为无符号int,所以INT_MAX将被转换为UINT_MAX + 1 + INT_MIN。

#include <stdio.h>
#include <stdlib.h>

unsigned min(unsigned a, unsigned b)
{
    return a < b ? a : b;
}

int main(void)
{
    printf("%u\n", min(INT_MAX, INT_MIN));
}