问题
The question is about modeling infinity in C++ for the double
data type. I need it in a header file, so we cannot use functions like numeric_limits
.
Is there a defined constant that represents the largest value?
回答1:
floating point numbers(such as doubles) can actually hold positive and negative infinity. The constant INFINITY should be in your math.h header.
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.
In Section 7.12 Mathematics <math.h>
Then of course you have the helper function isinf
to test for infinity(which is also in 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.
回答2:
numeric_limits
functions 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.
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 INFINITY
in cmath should work for you. It's actually the float
value for infinity, but it can be converted to a double
.
回答3:
Not sure why you can't use std::numeric_limits in a header file. But there is also this carried over from ANSI C:
#include <cfloat>
DBL_MAX
回答4:
Maybe in your C++ environment you have float.h
, see http://www.gamedev.net/topic/392211-max-value-for-double-/ (DBL_MAX)
回答5:
I thought the answer was "42.0" ;)
This article might be of interest:
http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
Or this:
http://www.cplusplus.com/reference/clibrary/cfloat/
MAXimum Maximum finite representable floating-point number:
FLT_MAX 1E+37
DBL_MAX 1E+37
LDBL_MAX 1E+37
回答6:
From Wikipedia:
0x 7ff0 0000 0000 0000 = Infinity
0x fff0 0000 0000 0000 = −Infinity
回答7:
DBL_MAX can be used. This is found in float.h as follows
#define DBL_MAX 1.7976931348623158e+308 /* max value */
回答8:
#include <cmath>
...
double d = INFINITY;
You can find INFINITY
defined in <cmath>
(math.h):
A constant expression of type
float
representing positive or unsigned infinity, if available; else a positive constant of typefloat
that overflows at translation time.
回答9:
Wouldn't this work?
const double infinity = 1.0/0.0;
来源:https://stackoverflow.com/questions/8640707/modeling-infinity-for-the-largest-double-value