17

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?

Jamal
  • 763
  • 7
  • 22
  • 32
ashim
  • 24,380
  • 29
  • 72
  • 96

9 Answers9

26

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.

Community
  • 1
  • 1
  • 1
    @JamWaffles The C standards are actually surprisingly readable. It's the C++ standards which seem to be written by overzealous language lawyers. –  Dec 27 '11 at 02:58
  • 7
    Since the question is technically asking what for "C++" the #include header should be used instead of #include . – lefticus Dec 27 '11 at 05:18
21

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.

bames53
  • 86,085
  • 15
  • 179
  • 244
10

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
Gerald
  • 23,011
  • 10
  • 73
  • 102
2

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

    #define DBL_MAX         1.7976931348623158e+308 /* max value */
Ajit Vaze
  • 2,686
  • 2
  • 20
  • 24
1

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  
paulsm4
  • 114,292
  • 17
  • 138
  • 190
1

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

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
1

From Wikipedia:

0x 7ff0 0000 0000 0000   = Infinity
0x fff0 0000 0000 0000   = −Infinity
fge
  • 119,121
  • 33
  • 254
  • 329
  • Of course, the OP might be on a platform that isn't using IEEE floating point numbers. – Omnifarious Dec 27 '11 at 04:10
  • I know of no current platforms. But my old 8-bit Atari system did not (and yes, it had floating point). But it's bad form regardless. There's no saying that IEEE floating point will be how things are done in ten years. This invites the creation of another 'millenium bug'. – Omnifarious Dec 27 '11 at 17:09
0
#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 type float that overflows at translation time.

AusCBloke
  • 18,014
  • 6
  • 40
  • 44
-1

Wouldn't this work?

const double infinity =  1.0/0.0;
Daniel Fath
  • 16,453
  • 7
  • 47
  • 82
Pubby
  • 51,882
  • 13
  • 139
  • 180