为什么FLT_MAX和FLT_MIN不是正负无穷大,它们的用途是什么? [英] Why are FLT_MAX and FLT_MIN not positive and negative infinity, and what is their use?

查看:2173
本文介绍了为什么FLT_MAX和FLT_MIN不是正负无穷大,它们的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

为什么, FLT_MAX FLT_MIN 没有设置?我明白这是标准所要求的。但是,使用可能会 FLT_MAX FLT_MIN float 的可表示数字范围的中间?其他数字限制有一些实用性,因为它们保证比较(例如没有INT可以测试大于INT_MAX)。没有这种保证,这些浮动限制的用途是什么?

Why, then, are FLT_MAX and FLT_MIN not set to them? 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? 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?

C ++的激励示例:

A motivating example for 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;
}

如果T是一个整数类型,浮点类型。这是烦人。 (是的,标准库提供 min_element ,但这不是重点,点是模式。)

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.)

推荐答案

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

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


FLT_MAX或FLT_MIN有什么用处,因为它们当前位于float的可表示数字范围的中间?

what use could FLT_MAX or FLT_MIN have as they currently lie in the middle of the representable numeric range of float?

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

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. You will get +INF.


如果T是一个整数类型,这个代码可以正常工作,但如果它是一个浮点类型,这是烦人。 (是的,标准库提供了min_element,但这不是关键点。)

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.)

它工作正常?它给你最小的价值。唯一没有工作正常的情况是表格只包含 + INF。即使在这种情况下,它会返回实际的数字,而不是错误代码。这可能是更好的选择反正。

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.

这篇关于为什么FLT_MAX和FLT_MIN不是正负无穷大,它们的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆