C++ 为什么 FLT_MAX 和 FLT_MIN 不是正负无穷大,它们有什么用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7973737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:49:38  来源:igfitidea点击:

Why are FLT_MAX and FLT_MIN not positive and negative infinity, and what is their use?

c++c

提问by tenfour

Logically speaking, given the nature of floating point values, the maximum and minimum representable values of a floatare positive and negative infinity, respectively.

从逻辑上讲,鉴于浮点值的性质,a 的最大和最小可表示值float分别为正无穷大和负无穷大。

Why, then, are FLT_MAXand FLT_MINnot set to them? I understand that this is "just how the standard called for". But then, what usecould FLT_MAXor FLT_MINhave as they currently lie in the middle of the representable numeric range of float? Other numeric limits have some utility because they make guarantees about comparisons (e.g. "No INT can test greater than INT_MAX"). Without that kind of guarantee, what use are these float limits at all?

那么,为什么是FLT_MAXFLT_MIN没有设置呢?我明白这就是“标准所要求的”。但是,由于它们目前位于 的可表示数字范围的中间,因此可以具有什么用途FLT_MAXFLT_MINfloat?其他数字限制有一些效用,因为它们保证了比较(例如“没有 INT 可以测试大于 INT_MAX”)。没有这种保证,这些浮动限制有什么用?

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. This is annoying. (Yes yes, the standard library provides min_element, but that is not the point. The point is the pattern.)

如果 T 是整数类型,则此代码可以正常工作,但如果它是浮点类型,则不能正常工作。这很烦人。(是的,标准库提供了min_element,但这不是重点。重点是模式。)

回答by Nicol Bolas

The purpose of FLT_MIN/MAXis to tell you what the smallest and largest representable floating-point numbersare. Infinity isn't a number; it's a limit.

的目的FLT_MIN/MAX就是告诉你的最小和最大可表示浮点数什么号码是。无穷大不是一个数字;这是一个限制。

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 有什么用,因为它们目前位于浮点数可表示的数字范围的中间?

They do not lie in the middleor the representable range. There is no positive float value xwhich you can add to FLT_MAXand get a representable number. You will get +INF.

它们不在中间或可表示的范围内。没有x可以添加FLT_MAX并获得可表示数字的正浮点值。你会得到+INF。

This code works fine if T is an integral type, but not if it is a floating point type. This is annoying. (Yes yes, the standard library provides min_element, but that is not the point. The point is the pattern.)

如果 T 是整数类型,则此代码可以正常工作,但如果它是浮点类型,则不能正常工作。这很烦人。(是的,标准库提供了 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. And even in that case, it returns an actual number, not an error-code. Which is probably the better option anyway.

它怎么不“正常工作”?它给你最小的价值。它不能“正常工作”的唯一情况是表包含+INF。即使在这种情况下,它也会返回一个实际的number,而不是一个错误代码。无论如何,这可能是更好的选择。

回答by Raymond Chen

FLT_MAXis defined in section 5.2.4.2.2(9) as

FLT_MAX在第 5.2.4.2.2(9) 节中定义为

maximum representable finite floating-point number

最大可表示的有限浮点数

Positive infinity is not finite.

正无穷不是有限的。

FLT_MINis defined in section 5.2.4.2.2(10) as

FLT_MIN在第 5.2.4.2.2(10) 节中定义为

minimum normalized positive floating-point number

最小归一化正浮点数

Negative infinity is neither normalized nor positive.

负无穷大既不是归一化的,也不是正的。

回答by R.. GitHub STOP HELPING ICE

I would say the broken pattern you're seeing is only an artifact of poor namingin C, whereas in C++ with numeric_limitsand templates, it's an actual semantic flaw that breaks template code that wants to handle both integer and floating point values. Of course you can write a little bit of extra code to test if you have an integer or floating point type (e.g. if ((T)1/2) /* floating point */ else /* integer */) and the problem goes away.

我会说你看到的破坏模式只是C 中命名不当的产物,而在 C++numeric_limits和模板中,它是一个实际的语义缺陷,它破坏了想要处理整数和浮点值的模板代码。当然,您可以编写一些额外的代码来测试您是否有整数或浮点类型(例如if ((T)1/2) /* floating point */ else /* integer */),并且问题会消失。

As for why somebody would care about the values FLT_MINand FLT_MAXgive you, they're useful for avoiding underflow and overflow. For example, suppose I need to compute sqrt(x2-1). This is well-defined for any floating point xgreater than or equal to 1, but performing the squaring, subtraction, and square root could easily overflow and render the result meaningless when xis large. One might want to test whether x > FLT_MAX/xand handle this case some other way (such as simply returning x:-).

至于为什么有人会关心这些值FLT_MINFLT_MAX给你,它们对于避免下溢和溢出很有用。例如,假设我需要计算sqrt(x2-1). 对于任何x大于或等于 1 的浮点,这是明确定义的,但是执行平方、减法和平方根很容易溢出,并且当x它很大时使结果变得毫无意义。人们可能想要测试是否x > FLT_MAX/x并以其他方式处理这种情况(例如简单地返回x:-)。

回答by Keith Thompson

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 (i.e., almost all modern systems), INT_MINis -INT_MAX-1; on other systems, it may be -INT_MAX. (Quibble: a two's-complement system can have INT_MINequal to -INT_MAXif the lowest representable value is treated as a trap representation.) So INT_MINconveys information that INT_MAXby itself doesn't.

在二进制补码系统(即几乎所有现代系统)上,INT_MIN-INT_MAX-1; 在其他系统上,它可能是-INT_MAX. (狡辩:如果最低可表示值被视为陷阱表示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(or -DBL_MAX, or -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. 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().

至于为什么它们不是 Infinity,已经有一种表示无限值的方法(至少在 C99 中):宏INFINITY. 这可能会导致某些 C++ 应用程序出现问题,但这些是为 C 定义的,它没有像std::numeric_limits<T>::max().

Furthermore, not all floating-point systems have representations for infinity (or NaN).

此外,并非所有浮点系统都有无穷大(或 NaN)的表示。

If FLT_MAXwere INFINITY(on systems that support it), then there would probably need to be another macro for the largest representable real value.

如果FLT_MAXINFINITY(在支持它的系统上),那么可能需要另一个宏来获得最大的可表示实际值。