What is DBL_MAX in C++? [closed]

2019-01-25 17:12发布

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.

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;

3条回答
老娘就宠你
2楼-- · 2019-01-25 17:24

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.

DBL_MAX is maximum finite representable floating-point number.

http://en.cppreference.com/w/cpp/types/climits

查看更多
Emotional °昔
3楼-- · 2019-01-25 17:25

The maximum finite representable floating-point number.

Take a look here if you find anything similar.


查看更多
看我几分像从前
4楼-- · 2019-01-25 17:34

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

In C++ you can get the same value using class std::numeric_limits defined in header <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

1.79769e+308
1.79769e+308
查看更多
登录 后发表回答