views:

145

answers:

2
double x;
cin>>x;
if( x > 1.7976931348623157e+308 || x < -1.7976931348623157e+308 )
{
  cout<<"value not in range"<<endl;
  exit(1);
}

Is there like a DOUBLE_MAX or DOUBLE_MIN and would I need to include any header files?

+3  A: 

There are constants for the largest and smallest double types, but since x is of type double, x cannot be larger or smaller than these values! If you wish to compare an inputted value to these limits you'll need to parse the string yourself and check for overflow.

Stephen Nutt
Most implementations support long double which has a range typically of +- 10^4900. Constants representing the largest double would be useful in truncating from long double to double. I think I've seen some, but can't find them just now.
wallyk
+4  A: 

The header <cfloat> is equivalent to the C header <float.h> and contains DBL_MIN and DBL_MAX among many other things. The integer limits (<limits.h> in C) are held in <climits>.

This is detailed in 18.3.2 of the C++0x draft. But, as pointed out elsewhere, a double cannot hold values outside this range anyway. You would have to use a "larger" type such as long double (although, according to the standard, that could be the same size as double, so may not necessarily help).

Here's a full sample program for your enjoyment :-)

#include <iostream>
#include <cfloat>

int main (void) {
    long double bignum;
    std::cout << "Enter number: ";
    std::cin >> bignum;
    if ((bignum > (long double)DBL_MAX) || (bignum < (long double)DBL_MIN)) {
        std::cout << "Value not in range" << std::endl;
    } else {
        double x = bignum;
        std::cout << "Number was " << x << std::endl;
    }
    return 0;
}

with accompanying transcript:

$ ./qq.exe
Enter number: 1.7976931348623157e+308
Number was 1.79769e+308

$ ./qq.exe
Enter number: 1.7976931348623158e+308
Value not in range
paxdiablo
There's also `std::numeric_limits<double>::max()` in the `<limits>` header. However, calling that function is supposed to return the value for `DBL_MAX` from `<cfloat>`/`<float.h>`. The same is true of the accompanying `min()` function and the `DBL_MIN` constant. It's yet another way to accomplish the same task. :)
Dustin
`std::numeric_limits<double>min()` returns the smallest `double` value greater than zero. `std::numeric_limits<double>::lowest()` is the function the original questioner would be looking for.
Dennis Zickefoose
Crikey, the whole reason I gave up on Perl was because "there's too many ways to do it" :-) Now C++ is going the same way.
paxdiablo
I believe all [or at least most] of the macros provided in `climits` and `cfloat` are mirrored in `std::numeric_limits`, plus a few more features [such as the above mentioned `lowest` method]. So effectively, you can still just assume there's one way of doing it. These being methods instead of macros is occasionally problematic in C++98, but that gets fixed in C++0x.
Dennis Zickefoose
@paxdiablo: the headers *cfloat* and *float.h*, *climits* and *limits.h* are a C backwards compatibility thing (they define macros that are somewhat frown upon). The c++ way is including *limits* (no c, nor .h) that is what defines the `std::numeric_limits` template. Since most C code can compiler in a C++ compiler, there is usually at least two ways of doing anything, the C way and the pure C++ way.
David Rodríguez - dribeas