C++ 中的 DBL_MAX 是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23278930/
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
What is DBL_MAX in C++?
提问by Christoph
I was looking at a program I found online and I see that the author used DBL_MAX in a few cases. I wasn't sure what it was so I researched a little, but there was not much explaining what it is and what its used for.
我正在查看我在网上找到的一个程序,我看到作者在一些情况下使用了 DBL_MAX。我不确定它是什么,所以我做了一些研究,但没有太多解释它是什么以及它的用途。
Can anyone explain what it is and why you should use it?
谁能解释一下它是什么以及为什么要使用它?
Some examples of use in the code were:
代码中的一些使用示例是:
localT.maxTemp = -DBL_MAX;
double avg = -DBL_MAX;
回答by Vlad from Moscow
As it was said by others DBL_MAX
defined in header <cfloat>
in C++ or <float.h>
in C is the value of maximum representable finite floating-point (double) number
正如在 C++ 或CDBL_MAX
中的头文件<cfloat>
中定义的其他人所说的<float.h>
是最大可表示的有限浮点(双)数的值
In C++ you can get the same value using class std::numeric_limits
defined in header <limits>
在 C++ 中,您可以使用std::numeric_limits
头文件中定义的类获得相同的值<limits>
std::numeric_limits<double>::max()
Here is an example of using the both approaches
这是使用这两种方法的示例
#include <iostream>
#include <cfloat>
#include <limits>
int main()
{
std::cout << DBL_MAX << std::endl;
std::cout << std::numeric_limits<double>::max() << std::endl;
return 0;
}
At www.ideone.com (on-line C++ compiler) the output is
在 www.ideone.com(在线 C++ 编译器)上,输出是
1.79769e+308
1.79769e+308
回答by 4pie0
It is a constant defined in float.h
or <cfloat>
. This header describes the characteristics of floating types for the specific system and compiler implemetation used.
它是在float.h
或 中定义的常量<cfloat>
。该头文件描述了所使用的特定系统和编译器实现的浮点类型的特征。
DBL_MAX
is maximum finite representable floating-point number.
DBL_MAX
是最大有限可表示的浮点数。