[英]Why are FLT_MAX and FLT_MIN not positive and negative infinity, and what is their use?
Logically speaking, given the nature of floating point values, the maximum and minimum representable values of a float
are positive and negative infinity, respectively. 从逻辑上说,给定的浮点值的性质,a的最大和最小可表示的值
float
是正和负无穷大,分别。
Why, then, are FLT_MAX
and FLT_MIN
not set to them? 那么,为什么没有设置
FLT_MAX
和FLT_MIN
? I understand that this is "just how the standard called for". 我知道这是“标准要求的方式”。 But then, what use could
FLT_MAX
or FLT_MIN
have as they currently lie in the middle of the representable numeric range of float
? 但是,
FLT_MAX
或FLT_MIN
有什么用途 ,因为它们目前位于float
的可表示数值范围的中间 ? Other numeric limits have some utility because they make guarantees about comparisons (eg "No INT can test greater than INT_MAX"). 其他数字限制具有一些实用性,因为它们保证了比较(例如“无INT可以测试大于INT_MAX”)。 Without that kind of guarantee, what use are these float limits at all?
没有这种保证,这些浮动限制有什么用?
A motivating example for C++: C ++的一个激励范例:
#include <vector>
#include <limits>
template<typename T>
T find_min(const std::vector<T> &vec)
{
T result = std::numeric_limits<T>::max();
for (std::vector<T>::const_iterator p = vec.start() ; p != vec.end() ; ++p)
if (*p < result) result = *p;
return result;
}
This code works fine if T is an integral type, but not if it is a floating point type. 如果T是整数类型,则此代码可以正常工作,但如果它是浮点类型则不行。 This is annoying.
这很烦人。 (Yes yes, the standard library provides
min_element
, but that is not the point. The point is the pattern .) (是的,标准库提供
min_element
,但这不是重点。重点是模式 。)
FLT_MAX
is defined in section 5.2.4.2.2(9) as FLT_MAX
在FLT_MAX
(9)节中定义为
maximum representable finite floating-point number
最大可表示的有限浮点数
Positive infinity is not finite. 正无穷大不是有限的。
FLT_MIN
is defined in section 5.2.4.2.2(10) as FLT_MIN
在FLT_MIN
(10)节中定义为
minimum normalized positive floating-point number
最小归一化正浮点数
Negative infinity is neither normalized nor positive. 负无穷大既不正常也不正。
The purpose of FLT_MIN
/ MAX
is to tell you what the smallest and largest representable floating-point numbers are. 的目的
FLT_MIN
/ MAX
是想告诉大家的最小和最大可表示浮点数是什么。 Infinity isn't a number; 无限不是一个数字; it's a limit.
这是一个限制。
what use could FLT_MAX or FLT_MIN have as they currently lie in the middle of the representable numeric range of float?
FLT_MAX或FLT_MIN有什么用途,因为它们目前位于float的可表示数值范围的中间?
They do not lie in the middle or the representable range. 它们不位于中间或可表示的范围内。 There is no positive float value
x
which you can add to FLT_MAX
and get a representable number. 没有正浮点值
x
,您可以将其添加到FLT_MAX
并获得可表示的数字。 You will get +INF. 你会得到+ INF。
This code works fine if T is an integral type, but not if it is a floating point type.
如果T是整数类型,则此代码可以正常工作,但如果它是浮点类型则不行。 This is annoying.
这很烦人。 (Yes yes, the standard library provides min_element, but that is not the point. The point is the pattern.)
(是的,标准库提供min_element,但这不是重点。重点是模式。)
And how doesn't it "work fine?" 它怎么不“工作正常?” It gives you the smallest value.
它为您提供最小的价值。 The only situation where it doesn't "work fine" is if the table contains only +INF.
它不能“正常工作”的唯一情况是表格只包含+ INF。 And even in that case, it returns an actual number , not an error-code.
即使在这种情况下,它也会返回一个实际数字 ,而不是错误代码。 Which is probably the better option anyway.
无论如何,这可能是更好的选择。
I would say the broken pattern you're seeing is only an artifact of poor naming in C, whereas in C++ with numeric_limits
and templates, it's an actual semantic flaw that breaks template code that wants to handle both integer and floating point values. 我会说你看到的破碎模式只是C中命名不佳的人工制品,而在带有
numeric_limits
和模板的C ++中,它是一个实际的语义缺陷,它破坏了想要处理整数和浮点值的模板代码。 Of course you can write a little bit of extra code to test if you have an integer or floating point type (eg if ((T)1/2) /* floating point */ else /* integer */
) and the problem goes away. 当然你可以编写一些额外的代码来测试你是否有整数或浮点类型(例如
if ((T)1/2) /* floating point */ else /* integer */
)并且问题是远。
As for why somebody would care about the values FLT_MIN
and FLT_MAX
give you, they're useful for avoiding underflow and overflow. 至于为什么有人会关心
FLT_MIN
和FLT_MAX
给你的值,它们对于避免下溢和溢出很有用。 For example, suppose I need to compute sqrt(x²-1)
. 例如,假设我需要计算
sqrt(x²-1)
。 This is well-defined for any floating point x
greater than or equal to 1, but performing the squaring, subtraction, and square root could easily overflow and render the result meaningless when x
is large. 这对于任何大于或等于1的浮点
x
都是明确定义的,但是当x
很大时,执行平方,减法和平方根很容易溢出并使结果无意义。 One might want to test whether x > FLT_MAX/x
and handle this case some other way (such as simply returning x
:-). 有人可能想测试
x > FLT_MAX/x
是否以其他方式处理这种情况(例如简单地返回x
:-)。
Unlike integer types, floating-point types are (almost?) universally symmetric about zero, and I think the C floating-point model requires this. 与整数类型不同,浮点类型(几乎?)普遍对称为零,我认为C浮点模型需要这个。
On two's-complement systems (ie, almost all modern systems), INT_MIN
is -INT_MAX-1
; 在二进制补码系统(即几乎所有现代系统)上,
INT_MIN
是-INT_MAX-1
; on other systems, it may be -INT_MAX
. 在其他系统上,它可能是
-INT_MAX
。 (Quibble: a two's-complement system can have INT_MIN
equal to -INT_MAX
if the lowest representable value is treated as a trap representation.) So INT_MIN
conveys information that INT_MAX
by itself doesn't. (Qibble:如果最低可表示值被视为陷阱表示,则二进制补码系统可以使
INT_MIN
等于-INT_MAX
。)因此INT_MIN
传达INT_MAX
本身不存在的信息。
And a macro for the smallest positive value would not be particularly useful; 最小正值的宏不会特别有用; that's just 1.
那只是1。
In floating-point, on the other hand, the negative value with the greatest magnitude is just -FLT_MAX
(or -DBL_MAX
, or -LDBL_MAX
). 另一方面,在浮点中,具有最大幅度的负值仅为
-FLT_MAX
(或-DBL_MAX
,或-LDBL_MAX
)。
As for why they're not Infinity, there's already a way to represent infinite values (at least in C99): the macro INFINITY
. 至于为什么他们没有无限,还有已经代表无限值(至少在C99)的方式:宏观
INFINITY
。 That might cause problems for some C++ applications, but these were defined for C, which doesn't have things like std::numeric_limits<T>::max()
. 这可能会导致某些C ++应用程序出现问题,但这些应用程序是为C定义的,它没有像
std::numeric_limits<T>::max()
。
Furthermore, not all floating-point systems have representations for infinity (or NaN). 此外,并非所有浮点系统都具有无穷大(或NaN)的表示。
If FLT_MAX
were INFINITY
(on systems that support it), then there would probably need to be another macro for the largest representable real value. 如果
FLT_MAX
是INFINITY
(在支持它的系统上),则可能需要另一个宏来表示最大的可表示实际值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.