在c++中使用最小和最大函数。

时间:2021-06-30 22:49:43

From C++, are min and max preferable over fmin and fmax? For comparing two integers, do they provide basically the same functionality?

在c++中,min和max比fmin和fmax更合适吗?为了比较两个整数,它们是否提供了基本相同的功能?

Do you tend to use one of these sets of functions or do you prefer to write your own (perhaps to improve efficiency, portability, flexibility, etc.)?

您是否倾向于使用这些功能集之一,还是倾向于编写自己的功能(可能是为了提高效率、可移植性、灵活性等等)?

Notes:

注:

  1. The C++ Standard Template Library (STL) declares the min and max functions in the standard C++ algorithm header.

    c++标准模板库(STL)声明了标准c++算法头中的最小和最大函数。

  2. The C standard (C99) provides the fmin and fmax function in the standard C math.h header.

    C标准(C99)在标准C数学中提供fmin和fmax函数。h头。

Thanks in advance!

提前谢谢!

14 个解决方案

#1


87  

fmin and fmax are specifically for use with floating point numbers (hence the "f"). If you use it for ints, you may suffer performance or precision losses due to conversion, function call overhead, etc. depending on your compiler/platform.

fmin和fmax特别适用于浮点数(因此是“f”)。如果您将它用于int,您可能会由于转换、函数调用开销等原因而遭受性能或精度损失,这取决于您的编译器/平台。

std::min and std::max are template functions (defined in header <algorithm>) which work on any type with a less-than (<) operator, so they can operate on any data type that allows such a comparison. You can also provide your own comparison function if you don't want it to work off <.

std::min和std::max是模板函数(在header <算法> 中定义),它适用于小于(<)操作符的任何类型,因此它们可以对任何允许进行这种比较的数据类型进行操作。如果您不想让它工作,您也可以提供自己的比较函数。

This is safer since you have to explicitly convert arguments to match when they have different types. The compiler won't let you accidentally convert a 64-bit int into a 64-bit float, for example. This reason alone should make the templates your default choice. (Credit to Matthieu M & bk1e)

这是比较安全的,因为您必须显式地转换参数以匹配不同类型的参数。例如,编译器不会让您意外地将64位int转换为64位浮点数。仅这个原因就应该使模板成为您的默认选择。(贷给Matthieu M & bk1e)

Even when used with floats the template may win in performance. A compiler always has the option of inlining calls to template functions since the source code is part of the compilation unit. Sometimes it's impossible to inline a call to a library function, on the other hand (shared libraries, absence of link-time optimization, etc.).

即使使用浮动模板,模板也可以在性能上获得成功。由于源代码是编译单元的一部分,编译器总是可以选择对模板函数进行内联调用。有时候,不可能将一个调用内联到库函数,另一方面(共享库、缺少链接时间优化等)。

#2


14  

There is an important difference between std::min, std::max and fmin and fmax.

std::min, std::max, fmin, fmax之间有一个重要的区别。

std::min(-0.0,0.0) = -0.0
std::max(-0.0,0.0) = -0.0

whereas

fmin(-0.0, 0.0) = -0.0
fmax(-0.0, 0.0) =  0.0

So std::min is not a 1-1 substitute for fmin. The functions std::min and std::max are not commutative. To get the same result with doubles with fmin and fmax one should swap the arguments

所以std::min不是fmin的1-1替代。std::min和std::max是不可交换的。为了得到与fmin和fmax的相同的结果,应该交换参数。

fmin(-0.0, 0.0) = std::min(-0.0,  0.0)
fmax(-0.0, 0.0) = std::max( 0.0, -0.0)

But as far as I can tell all these functions are implementation defined anyway in this case so to be 100% sure you have to test how they are implemented.

但就我所知,所有这些函数都是在这个例子中定义的,所以要100%确定你必须测试它们是如何实现的。


There is another important difference. For x ! = NaN:

还有一个重要的区别。x !=南:

std::max(Nan,x) = NaN
std::max(x,NaN) = x
std::min(Nan,x) = NaN
std::min(x,NaN) = x

whereas

fmax(Nan,x) = x
fmax(x,NaN) = x
fmin(Nan,x) = x
fmin(x,NaN) = x

fmax can be emulated with the following code

fmax可以用下面的代码来模拟。

double myfmax(double x, double y)
{
   // z > nan for z != nan is required by C the standard
   int xnan = isnan(x), ynan = isnan(y);
   if(xnan || ynan) {
        if(xnan && !ynan) return y;
        if(!xnan && ynan) return x;
        return x;
   }
   // +0 > -0 is preferred by C the standard 
   if(x==0 && y==0) {
       int xs = signbit(x), ys = signbit(y);
       if(xs && !ys) return y;
       if(!xs && ys) return x;
       return x;
   }
   return std::max(x,y);
}

This shows that std::max is a subset of fmax.

这表明std::max是fmax的一个子集。

Looking at the assembly shows that Clang uses builtin code for fmax and fmin whereas GCC calls them from a math library. The assembly for clang for fmax with -O3 is

查看程序集显示,Clang使用builtin代码为fmax和fmin,而GCC从一个数学库调用它们。使用-O3的fmax装配为clang。

movapd  xmm2, xmm0
cmpunordsd      xmm2, xmm2
movapd  xmm3, xmm2
andpd   xmm3, xmm1
maxsd   xmm1, xmm0
andnpd  xmm2, xmm1
orpd    xmm2, xmm3
movapd  xmm0, xmm2

whereas for std::max(double, double) it is simply

而std::max(double, double)很简单。

maxsd   xmm0, xmm1

However, for GCC and Clang using -Ofast fmax becomes simply

然而,对于GCC和Clang使用-Ofast fmax变得简单。

maxsd   xmm0, xmm1

So this shows once again that std::max is a subset of fmax and that when you use a looser floating point model which does not have nan or signed zero then fmax and std::max are the same. The same argument obviously applies to fmin and std::min.

所以这再次显示std::max是fmax的一个子集,当你使用一个更宽松的浮点模型时,它没有nan或符号为0,那么fmax和std::max是相同的。同样的道理也适用于fmin和std::min。

#3


13  

You're missing the entire point of fmin and fmax. It was included in C99 so that modern CPUs could use their native (read SSE) instructions for floating point min and max and avoid a test and branch (and thus a possibly mis-predicted branch). I've re-written code that used std::min and std::max to use SSE intrinsics for min and max in inner loops instead and the speed-up was significant.

你错过了fmin和fmax的全部。它被包含在C99中,以便现代的cpu可以使用它们的本机(read SSE)指令来执行浮点min和max,并避免测试和分支(因此可能是错误预测的分支)。我重写了使用std::min和std的代码::max在内部循环中使用SSE特性,而加速是非常重要的。

#4


6  

std::min and std::max are templates. So, they can be used on a variety of types that provide the less than operator, including floats, doubles, long doubles. So, if you wanted to write generic C++ code you'd do something like this:

std::min和std::max是模板。因此,它们可以用于提供小于运算符的各种类型,包括浮点数、双打和长双打。因此,如果你想编写通用的c++代码,你可以这样做:

template<typename T>
T const& max3(T const& a, T const& b, T const& c)
{
   using std::max;
   return max(max(a,b),c); // non-qualified max allows ADL
}

As for performance, I don't think fmin and fmax differ from their C++ counterparts.

至于性能,我不认为fmin和fmax与c++的不同。

#5


6  

If your implementation provides a 64-bit integer type, you may get a different (incorrect) answer by using fmin or fmax. Your 64-bit integers will be converted to doubles, which will (at least usually) have a significand that's smaller than 64-bits. When you convert such a number to a double, some of the least significant bits can/will be lost completely.

如果您的实现提供了一个64位的整数类型,您可能会使用fmin或fmax得到一个不同的(不正确的)答案。您的64位整数将被转换为double,这将(至少通常)具有显著性,小于64位。当你把这样一个数字转换成double时,一些最不重要的部分就会完全丢失。

This means that two numbers that were really different could end up equal when converted to double -- and the result will be that incorrect number, that's not necessarily equal to either of the original inputs.

这意味着,两个不同的数字在转化为double时可能会变成相等的,结果会是不正确的数字,这并不一定等于原始输入。

#6


4  

I would prefer the C++ min/max functions, if you are using C++, because they are type-specific. fmin/fmax will force everything to be converted to/from floating point.

如果你使用c++,我更喜欢c++的min/max函数,因为它们是特定类型的。fmin/fmax将强制将所有内容转换为/从浮点数转换。

Also, the C++ min/max functions will work with user-defined types as long as you have defined operator< for those types.

另外,只要定义了操作符 <用于那些类型,那么c++ min max函数将使用用户定义的类型。< p>

HTH

HTH

#7


2  

As you noted yourself, fmin and fmax were introduced in C99. Standard C++ library doesn't have fmin and fmax functions. Until C99 standard library gets incorporated into C++ (if ever), the application areas of these functions are cleanly separated. There's no situation where you might have to "prefer" one over the other.

正如您所注意到的,fmin和fmax是在C99中引入的。标准c++库没有fmin和fmax函数。在C99标准库被合并到c++(如果有的话)之前,这些函数的应用程序区域是完全分离的。在这种情况下,你可能不得不“喜欢”另一个。

You just use templated std::min/std::max in C++, and use whatever is available in C.

您只需使用模板std::min/std::max在c++中,并使用C中可用的任何内容。

#8


2  

As Richard Corden pointed, use C++ functions min and max defined in std namespace. They provide type safety, and help to avoid comparing mixed types (i.e. float point vs integer) what sometimes may be undesirable.

正如Richard Corden指出的,在std命名空间中使用c++函数min和max。它们提供类型安全,并帮助避免比较混合类型(即浮点数和整数),有时可能是不可取的。

If you find that C++ library you use defines min/max as macros as well, it may cause conflicts, then you can prevent unwanted macro substitution calling the min/max functions this way (notice extra brackets):

如果您发现您使用的c++库定义了min/max作为宏,那么它可能会导致冲突,那么您就可以防止不必要的宏替换,以这种方式调用min/max函数(注意额外的括号):

(std::min)(x, y)
(std::max)(x, y)

Remember, this will effectively disable Argument Dependant Lookup (ADL, also called Koenig lookup), in case you want to rely on ADL.

记住,这将有效地禁用参数依赖查找(ADL,也称为Koenig查找),以防止您依赖ADL。

#9


1  

fmin and fmax are only for floating point and double variables.

fmin和fmax仅用于浮点和双变量。

min and max are template functions that allow comparison of any types, given a binary predicate. They can also be used with other algorithms to provide complex functionality.

min和max是允许比较任何类型的模板函数,给定一个二元谓词。它们还可以与其他算法一起使用,以提供复杂的功能。

#10


1  

Use std::min and std::max.

使用std::min和std::马克斯。

If the other versions are faster then your implementation can add overloads for these and you'll get the benefit of performance and portability:

如果其他版本的速度更快,那么您的实现就可以为它们添加重载,您将获得性能和可移植性的好处:

template <typename T>
T min (T, T) {
  // ... default
}

inline float min (float f1, float f2) {
 return fmin( f1, f2);
}    

#11


1  

By the way, in cstdlib there are __min and __max you can use.

顺便说一下,在cstdlib中可以使用__min和__max。

For more: http://msdn.microsoft.com/zh-cn/library/btkhtd8d.aspx

更多信息:http://msdn.microsoft.com/zh-cn/library/btkhtd8d.aspx

#12


0  

I always use the min and max macros for ints. I'm not sure why anyone would use fmin or fmax for integer values.

我总是用最小值和最大宏表示ints。我不知道为什么有人会使用fmin或fmax来获取整数值。

The big gotcha with min and max is that they're not functions, even if they look like them. If you do something like:

min和max的大问题是它们不是函数,即使它们看起来像它们。如果你这样做:

min (10, BigExpensiveFunctionCall())

That function call may get called twice depending on the implementation of the macro. As such, its best practice in my org to never call min or max with things that aren't a literal or variable.

这个函数调用可能会被调用两次,这取决于宏的实现。因此,它在我的组织中最好的做法是,永远不要用非文字或变量的东西来调用min或max。

#13


0  

fmin and fmax, of fminl and fmaxl could be preferred when comparing signed and unsigned integers - you can take advantage of the fact that the entire range of signed and unsigned numbers and you don't have to worry about integer ranges and promotions.

在比较签名和无符号整数时,fmin和fmaxl可以优先考虑fmin和fmax——您可以利用这一事实,即所有签名和未签名的数字的范围,您不必担心整数范围和推广。

unsigned int x = 4000000000;
int y = -1;

int z = min(x, y);
z = (int)fmin(x, y);

#14


0  

Couldn't a C++ implementation targeted for processors with SSE instructions provide specializations of std::min and std::max for types float, double, and long double which do the equivalent of fminf, fmin, and fminl, respectively?

对于带有SSE指令的处理器的c++实现不能提供std的专门化::min和std::max for types float, double, and long double,分别相当于fminf、fmin和fminl。

The specializations would provide better performance for floating-point types while the general template would handle non-floating-point types without attempting to coerce floating-point types into floating-point types that way the fmins and fmaxes would.

专门化将为浮点类型提供更好的性能,而通用模板将处理非浮点类型,而不试图强制将浮点类型转换为浮点类型,这样就可以使用fmins和fmaxes。

#1


87  

fmin and fmax are specifically for use with floating point numbers (hence the "f"). If you use it for ints, you may suffer performance or precision losses due to conversion, function call overhead, etc. depending on your compiler/platform.

fmin和fmax特别适用于浮点数(因此是“f”)。如果您将它用于int,您可能会由于转换、函数调用开销等原因而遭受性能或精度损失,这取决于您的编译器/平台。

std::min and std::max are template functions (defined in header <algorithm>) which work on any type with a less-than (<) operator, so they can operate on any data type that allows such a comparison. You can also provide your own comparison function if you don't want it to work off <.

std::min和std::max是模板函数(在header <算法> 中定义),它适用于小于(<)操作符的任何类型,因此它们可以对任何允许进行这种比较的数据类型进行操作。如果您不想让它工作,您也可以提供自己的比较函数。

This is safer since you have to explicitly convert arguments to match when they have different types. The compiler won't let you accidentally convert a 64-bit int into a 64-bit float, for example. This reason alone should make the templates your default choice. (Credit to Matthieu M & bk1e)

这是比较安全的,因为您必须显式地转换参数以匹配不同类型的参数。例如,编译器不会让您意外地将64位int转换为64位浮点数。仅这个原因就应该使模板成为您的默认选择。(贷给Matthieu M & bk1e)

Even when used with floats the template may win in performance. A compiler always has the option of inlining calls to template functions since the source code is part of the compilation unit. Sometimes it's impossible to inline a call to a library function, on the other hand (shared libraries, absence of link-time optimization, etc.).

即使使用浮动模板,模板也可以在性能上获得成功。由于源代码是编译单元的一部分,编译器总是可以选择对模板函数进行内联调用。有时候,不可能将一个调用内联到库函数,另一方面(共享库、缺少链接时间优化等)。

#2


14  

There is an important difference between std::min, std::max and fmin and fmax.

std::min, std::max, fmin, fmax之间有一个重要的区别。

std::min(-0.0,0.0) = -0.0
std::max(-0.0,0.0) = -0.0

whereas

fmin(-0.0, 0.0) = -0.0
fmax(-0.0, 0.0) =  0.0

So std::min is not a 1-1 substitute for fmin. The functions std::min and std::max are not commutative. To get the same result with doubles with fmin and fmax one should swap the arguments

所以std::min不是fmin的1-1替代。std::min和std::max是不可交换的。为了得到与fmin和fmax的相同的结果,应该交换参数。

fmin(-0.0, 0.0) = std::min(-0.0,  0.0)
fmax(-0.0, 0.0) = std::max( 0.0, -0.0)

But as far as I can tell all these functions are implementation defined anyway in this case so to be 100% sure you have to test how they are implemented.

但就我所知,所有这些函数都是在这个例子中定义的,所以要100%确定你必须测试它们是如何实现的。


There is another important difference. For x ! = NaN:

还有一个重要的区别。x !=南:

std::max(Nan,x) = NaN
std::max(x,NaN) = x
std::min(Nan,x) = NaN
std::min(x,NaN) = x

whereas

fmax(Nan,x) = x
fmax(x,NaN) = x
fmin(Nan,x) = x
fmin(x,NaN) = x

fmax can be emulated with the following code

fmax可以用下面的代码来模拟。

double myfmax(double x, double y)
{
   // z > nan for z != nan is required by C the standard
   int xnan = isnan(x), ynan = isnan(y);
   if(xnan || ynan) {
        if(xnan && !ynan) return y;
        if(!xnan && ynan) return x;
        return x;
   }
   // +0 > -0 is preferred by C the standard 
   if(x==0 && y==0) {
       int xs = signbit(x), ys = signbit(y);
       if(xs && !ys) return y;
       if(!xs && ys) return x;
       return x;
   }
   return std::max(x,y);
}

This shows that std::max is a subset of fmax.

这表明std::max是fmax的一个子集。

Looking at the assembly shows that Clang uses builtin code for fmax and fmin whereas GCC calls them from a math library. The assembly for clang for fmax with -O3 is

查看程序集显示,Clang使用builtin代码为fmax和fmin,而GCC从一个数学库调用它们。使用-O3的fmax装配为clang。

movapd  xmm2, xmm0
cmpunordsd      xmm2, xmm2
movapd  xmm3, xmm2
andpd   xmm3, xmm1
maxsd   xmm1, xmm0
andnpd  xmm2, xmm1
orpd    xmm2, xmm3
movapd  xmm0, xmm2

whereas for std::max(double, double) it is simply

而std::max(double, double)很简单。

maxsd   xmm0, xmm1

However, for GCC and Clang using -Ofast fmax becomes simply

然而,对于GCC和Clang使用-Ofast fmax变得简单。

maxsd   xmm0, xmm1

So this shows once again that std::max is a subset of fmax and that when you use a looser floating point model which does not have nan or signed zero then fmax and std::max are the same. The same argument obviously applies to fmin and std::min.

所以这再次显示std::max是fmax的一个子集,当你使用一个更宽松的浮点模型时,它没有nan或符号为0,那么fmax和std::max是相同的。同样的道理也适用于fmin和std::min。

#3


13  

You're missing the entire point of fmin and fmax. It was included in C99 so that modern CPUs could use their native (read SSE) instructions for floating point min and max and avoid a test and branch (and thus a possibly mis-predicted branch). I've re-written code that used std::min and std::max to use SSE intrinsics for min and max in inner loops instead and the speed-up was significant.

你错过了fmin和fmax的全部。它被包含在C99中,以便现代的cpu可以使用它们的本机(read SSE)指令来执行浮点min和max,并避免测试和分支(因此可能是错误预测的分支)。我重写了使用std::min和std的代码::max在内部循环中使用SSE特性,而加速是非常重要的。

#4


6  

std::min and std::max are templates. So, they can be used on a variety of types that provide the less than operator, including floats, doubles, long doubles. So, if you wanted to write generic C++ code you'd do something like this:

std::min和std::max是模板。因此,它们可以用于提供小于运算符的各种类型,包括浮点数、双打和长双打。因此,如果你想编写通用的c++代码,你可以这样做:

template<typename T>
T const& max3(T const& a, T const& b, T const& c)
{
   using std::max;
   return max(max(a,b),c); // non-qualified max allows ADL
}

As for performance, I don't think fmin and fmax differ from their C++ counterparts.

至于性能,我不认为fmin和fmax与c++的不同。

#5


6  

If your implementation provides a 64-bit integer type, you may get a different (incorrect) answer by using fmin or fmax. Your 64-bit integers will be converted to doubles, which will (at least usually) have a significand that's smaller than 64-bits. When you convert such a number to a double, some of the least significant bits can/will be lost completely.

如果您的实现提供了一个64位的整数类型,您可能会使用fmin或fmax得到一个不同的(不正确的)答案。您的64位整数将被转换为double,这将(至少通常)具有显著性,小于64位。当你把这样一个数字转换成double时,一些最不重要的部分就会完全丢失。

This means that two numbers that were really different could end up equal when converted to double -- and the result will be that incorrect number, that's not necessarily equal to either of the original inputs.

这意味着,两个不同的数字在转化为double时可能会变成相等的,结果会是不正确的数字,这并不一定等于原始输入。

#6


4  

I would prefer the C++ min/max functions, if you are using C++, because they are type-specific. fmin/fmax will force everything to be converted to/from floating point.

如果你使用c++,我更喜欢c++的min/max函数,因为它们是特定类型的。fmin/fmax将强制将所有内容转换为/从浮点数转换。

Also, the C++ min/max functions will work with user-defined types as long as you have defined operator< for those types.

另外,只要定义了操作符 <用于那些类型,那么c++ min max函数将使用用户定义的类型。< p>

HTH

HTH

#7


2  

As you noted yourself, fmin and fmax were introduced in C99. Standard C++ library doesn't have fmin and fmax functions. Until C99 standard library gets incorporated into C++ (if ever), the application areas of these functions are cleanly separated. There's no situation where you might have to "prefer" one over the other.

正如您所注意到的,fmin和fmax是在C99中引入的。标准c++库没有fmin和fmax函数。在C99标准库被合并到c++(如果有的话)之前,这些函数的应用程序区域是完全分离的。在这种情况下,你可能不得不“喜欢”另一个。

You just use templated std::min/std::max in C++, and use whatever is available in C.

您只需使用模板std::min/std::max在c++中,并使用C中可用的任何内容。

#8


2  

As Richard Corden pointed, use C++ functions min and max defined in std namespace. They provide type safety, and help to avoid comparing mixed types (i.e. float point vs integer) what sometimes may be undesirable.

正如Richard Corden指出的,在std命名空间中使用c++函数min和max。它们提供类型安全,并帮助避免比较混合类型(即浮点数和整数),有时可能是不可取的。

If you find that C++ library you use defines min/max as macros as well, it may cause conflicts, then you can prevent unwanted macro substitution calling the min/max functions this way (notice extra brackets):

如果您发现您使用的c++库定义了min/max作为宏,那么它可能会导致冲突,那么您就可以防止不必要的宏替换,以这种方式调用min/max函数(注意额外的括号):

(std::min)(x, y)
(std::max)(x, y)

Remember, this will effectively disable Argument Dependant Lookup (ADL, also called Koenig lookup), in case you want to rely on ADL.

记住,这将有效地禁用参数依赖查找(ADL,也称为Koenig查找),以防止您依赖ADL。

#9


1  

fmin and fmax are only for floating point and double variables.

fmin和fmax仅用于浮点和双变量。

min and max are template functions that allow comparison of any types, given a binary predicate. They can also be used with other algorithms to provide complex functionality.

min和max是允许比较任何类型的模板函数,给定一个二元谓词。它们还可以与其他算法一起使用,以提供复杂的功能。

#10


1  

Use std::min and std::max.

使用std::min和std::马克斯。

If the other versions are faster then your implementation can add overloads for these and you'll get the benefit of performance and portability:

如果其他版本的速度更快,那么您的实现就可以为它们添加重载,您将获得性能和可移植性的好处:

template <typename T>
T min (T, T) {
  // ... default
}

inline float min (float f1, float f2) {
 return fmin( f1, f2);
}    

#11


1  

By the way, in cstdlib there are __min and __max you can use.

顺便说一下,在cstdlib中可以使用__min和__max。

For more: http://msdn.microsoft.com/zh-cn/library/btkhtd8d.aspx

更多信息:http://msdn.microsoft.com/zh-cn/library/btkhtd8d.aspx

#12


0  

I always use the min and max macros for ints. I'm not sure why anyone would use fmin or fmax for integer values.

我总是用最小值和最大宏表示ints。我不知道为什么有人会使用fmin或fmax来获取整数值。

The big gotcha with min and max is that they're not functions, even if they look like them. If you do something like:

min和max的大问题是它们不是函数,即使它们看起来像它们。如果你这样做:

min (10, BigExpensiveFunctionCall())

That function call may get called twice depending on the implementation of the macro. As such, its best practice in my org to never call min or max with things that aren't a literal or variable.

这个函数调用可能会被调用两次,这取决于宏的实现。因此,它在我的组织中最好的做法是,永远不要用非文字或变量的东西来调用min或max。

#13


0  

fmin and fmax, of fminl and fmaxl could be preferred when comparing signed and unsigned integers - you can take advantage of the fact that the entire range of signed and unsigned numbers and you don't have to worry about integer ranges and promotions.

在比较签名和无符号整数时,fmin和fmaxl可以优先考虑fmin和fmax——您可以利用这一事实,即所有签名和未签名的数字的范围,您不必担心整数范围和推广。

unsigned int x = 4000000000;
int y = -1;

int z = min(x, y);
z = (int)fmin(x, y);

#14


0  

Couldn't a C++ implementation targeted for processors with SSE instructions provide specializations of std::min and std::max for types float, double, and long double which do the equivalent of fminf, fmin, and fminl, respectively?

对于带有SSE指令的处理器的c++实现不能提供std的专门化::min和std::max for types float, double, and long double,分别相当于fminf、fmin和fminl。

The specializations would provide better performance for floating-point types while the general template would handle non-floating-point types without attempting to coerce floating-point types into floating-point types that way the fmins and fmaxes would.

专门化将为浮点类型提供更好的性能,而通用模板将处理非浮点类型,而不试图强制将浮点类型转换为浮点类型,这样就可以使用fmins和fmaxes。