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?
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?
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.
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
.
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
Maybe in your C++ environment you have float.h
, see http://www.gamedev.net/topic/392211-max-value-for-double-/ (DBL_MAX)
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
From Wikipedia:
0x 7ff0 0000 0000 0000 = Infinity
0x fff0 0000 0000 0000 = −Infinity
DBL_MAX can be used. This is found in float.h as follows
#define DBL_MAX 1.7976931348623158e+308 /* max value */
#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.
Wouldn't this work?
const double infinity = 1.0/0.0;