C++ 为最大双精度值建模无穷大

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8640707/
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 18:55:22  来源:igfitidea点击:

Modeling infinity for the largest double value

c++doubleinfinity

提问by ashim

The question is about modeling infinity in C++ for the doubledata type. I need it in a header file, so we cannot use functions like numeric_limits.

问题是关于在 C++ 中为double数据类型建模无穷大。我在头文件中需要它,所以我们不能使用像numeric_limits.

Is there a defined constant that represents the largest value?

是否有一个定义的常数代表最大值?

回答by Lalaland

floating point numbers(such as doubles) can actually hold positive and negative infinity. The constant INFINITY should be in your math.h header.

浮点数(例如双精度数)实际上可以保持正无穷大和负无穷大。常量 INFINITY 应该在你的 math.h 头文件中。

Went standard diving and found the text:

去标准潜水,发现文字:

4 The macro INFINITY expands to a constant expression of type float representing positive or unsigned infinity, if available; else to a positive constant of type float that overflows at translation time.

4 宏 INFINITY 扩展为 float 类型的常量表达式,表示正无穷大或无符号无穷大(如果可用);else 为在翻译时溢出的 float 类型的正常量。

In Section 7.12 Mathematics <math.h>

在节 7.12 Mathematics <math.h>



Then of course you have the helper function isinfto test for infinity(which is also in math.h).

然后当然你有辅助函数isinf来测试无穷大(也在 math.h 中)。

7.12.3.3 The isinf macro

int isinf(real-floating x);

Description: The isinf macro determines whether its argument value is an infinity (positive or negative). First, an argument represented in a format wider than its semantic type is converted to its semantic type. Then determination is based on the type of the argument.

Returns: The isinf macro returns a nonzero value if and only if its argument has an infinite value.

7.12.3.3 isinf 宏

int isinf(real-floating x);

说明: isinf 宏确定其参数值是否为无穷大(正或负)。首先,以比其语义类型更宽的格式表示的参数被转换为其语义类型。然后确定是基于参数的类型。

返回: 当且仅当其参数具有无限值时,isinf 宏才返回非零值。

回答by bames53

numeric_limitsfunctions are all constexpr so they work just fine as compile time constants (assuming you're using the current version of C++). So std::numeric_limits<double>::infinity()ought to work in any context.

numeric_limits函数都是 constexpr,因此它们可以作为编译时常量正常工作(假设您使用的是当前版本的 C++)。所以std::numeric_limits<double>::infinity()应该在任何情况下工作。

Even if you're using an older version, this will still work anywhere that you don't require a compile time constant. It's not clear from your question if your use really needs a compile time constant or not; just being in a header doesn't necessarily require it.

即使您使用的是旧版本,这仍然适用于您不需要编译时间常数的任何地方。从您的问题中不清楚您的使用是否真的需要编译时常量;只是在标题中并不一定需要它。

If you are using an older version, and you really do need a compile time constant, the macro INFINITYin cmath should work for you. It's actually the floatvalue for infinity, but it can be converted to a double.

如果您使用的是旧版本,并且确实需要编译时常量,那么INFINITYcmath 中的宏应该适合您。它实际上是float无穷大的值,但它可以转换为double.

回答by Gerald

Not sure why you can't use std::numeric_limits in a header file. But there is also this carried over from ANSI C:

不知道为什么不能在头文件中使用 std::numeric_limits 。但也有从 ANSI C 继承而来的:

#include <cfloat>

DBL_MAX

回答by Joop Eggen

Maybe in your C++ environment you have float.h, see http://www.gamedev.net/topic/392211-max-value-for-double-/(DBL_MAX)

也许在您的 C++ 环境中float.h,请参阅http://www.gamedev.net/topic/392211-max-value-for-double-/(DBL_MAX)

回答by paulsm4

I thought the answer was "42.0" ;)

我以为答案是“ 42.0”;)

This article might be of interest:

这篇文章可能很有趣:

http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

Or this:

或这个:

http://www.cplusplus.com/reference/clibrary/cfloat/

http://www.cplusplus.com/reference/clibrary/cfloat/

MAXimum Maximum finite representable floating-point number:

MAXimum 最大有限可表示浮点数:

FLT_MAX  1E+37
DBL_MAX  1E+37
LDBL_MAX 1E+37  

回答by fge

From Wikipedia:

来自维基百科

0x 7ff0 0000 0000 0000   = Infinity
0x fff0 0000 0000 0000   = ?Infinity

回答by Ajit Vaze

DBL_MAX can be used. This is found in float.h as follows

可以使用 DBL_MAX。这是在 float.h 中找到的,如下所示

    #define DBL_MAX         1.7976931348623158e+308 /* max value */

回答by AusCBloke

#include <cmath>
...
double d = INFINITY;

You can find INFINITYdefined in <cmath>(math.h):

您可以INFINITY<cmath>( math.h) 中找到定义:

A constant expression of type floatrepresenting positive or unsigned infinity, if available; else a positive constant of type floatthat overflows at translation time.

float表示正无穷大或无符号无穷大的常量表达式(如果可用);elsefloat在翻译时溢出的类型的正常量。

回答by Pubby

Wouldn't this work?

这行不通?

const double infinity =  1.0/0.0;