views:

327

answers:

3

As far as I know floating point values are of the form n * 2^e, with

  • float range being n = -(2^23-1) - (2^23-1), and e = -126 - 127,
  • double range being n = -(2^52-1) - (2^52-1), and e = -1022 - 1023

I was looking through the C++ standard, but failed to find the place where the standard specifies this, or mandates the association of the float, double and long double types with ranges defined in other (IEEE) standards. The only related thing I found in 3.9.1.8 is:

There are three floating point types: float, double, and long double. The type double provides at least as much precision as float, and the type long double provides at least as much precision as double. The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double. The value representation of floating-point types is implementation-defined.

And no mention of the minimum range provided by the type.

Where/how does the standard specify the (minimum?) value range of the floating point types? Or can a compiler freely choose any value range and still be standard compliant?

+7  A: 

What you've quoted is all that's guaranteed about the floating point types in C++. As it says, their representation is implementation-defined.

You can, though, query for information about the limits and whether the types are IEC 559 (IEEE 754) specified types using the std::numeric_limits templates in <limits>.

Charles Bailey
+2  A: 

The standard doesn't specify such things because they are often hardware dependent and change over time. While today 32 bits are considered a standard, in 10 years doing things in less than 64 bit may possibly seem distasteful.

shoosh
Size of floats is defined by IEEE, your compiler may choose to have it's own non-standard float defn but it would tell you.
Martin Beckett
I'm not sure I agree with the premise that 32 bit will become 'distasteful' as even now, 8 bit processors see substantial use in the embedded realm. 32 bit will probably stay mainstream in handset devices for a long time
TokenMacGuy
+1  A: 

Just like integer numberic limits, the limits for float, double and long double are imported from the C standard. The minimum value for constants FLT_MAX, DBL_MAX and LDBL_MAX is 1E+37. For their *_MIN variants the maximum value is 1E-37.

avakar
Thanks, just what I was looking for. The C standard does have certain minimum requirements for the float ranges. For example that float should be able to hold 1E-37 and 1E37 as you have said, and that the difference between 1 and the next greater representable float should be 1E-5 or less.
shojtsy