### float and double

what are the valid values of float and double? I understood that integer can have numbers from 0 to 65536. but didn't understand the limitations of the float and double.
Minimum value for a float is 1.175494351 E – 38 (if you're not familiar with "E" it means "*10^"). Max float is 3.402823466 E + 38.

Min double is 2.2250738585072014 E – 308 and max double is 1.7976931348623158 E + 308.

If you're not understanding these limitations, keep in mind that the computer is storing them in binary. The number and all the decimal places take up a certain amount of memory in binary. These determine how precise the number is. Then the exponent (after the "E") takes up more memory. The allowable size of the exponent determines the allowable range of the type. Whether the exponent is positive or negative, it still takes up the same amount of memory. That's why you can't store anything between 0 and 2.2250738585072014 E – 308.

RAM is pretty cheap nowadays, so you should usually use double unless you really have a good reason to use float.
The values can vary depending on your system (hardware+compiler), so it's best to find out for yourself. Here's a quick example which displays some of the limits:
 ``123456789101112131415161718192021`` ``````#include #include #include using namespace std; int main() { cout << "Signed Integers\n" << endl; cout << "CHAR_MAX: " << CHAR_MAX << endl; cout << "SHRT_MAX: " << SHRT_MAX << endl; cout << "INT_MAX: " << INT_MAX << endl; cout << "LONG_MAX: " << LONG_MAX << endl; cout << "\n\nFloating Point\n" << endl; cout << "FLT_MAX: " << FLT_MAX << endl; cout << "DBL_MAX: " << DBL_MAX << endl; cout << "LDBL_MAX: " << LDBL_MAX << endl; return 0; }``````

Notice this gives the limits for signed integers. Because half the range is occupied by negative numbers, the largest value is about half that of unsigned integers. Example output, but please don't take my word for it, your machine may differ:

Signed Integers

CHAR_MAX: 127
SHRT_MAX: 32767
INT_MAX: 2147483647
LONG_MAX: 2147483647

Floating Point

FLT_MAX: 3.40282e+38
DBL_MAX: 1.79769e+308
LDBL_MAX: 1.18973e+4932

See these reference pages, there are values listed in the tables, but bear in mind your own system may give different results:
http://www.cplusplus.com/reference/clibrary/climits/
http://www.cplusplus.com/reference/clibrary/cfloat/
Or C++: `std::numeric_limits<>`
http://en.cppreference.com/w/cpp/types/numeric_limits

 ``123456789101112131415161718`` ``````#include #include template< typename T > void show_range( const char* type ) { typedef std::numeric_limits limits ; std::cout << "type: " << type << " min: " << limits::min() << " max: " << limits::max() << '\n' ; } #define SHOW_RANGE( TYPE ) show_range( #TYPE ) int main() { SHOW_RANGE(double) ; SHOW_RANGE(unsigned long long) ; // etc }``````
Topic archived. No new replies allowed.