views:

134

answers:

8

Are there any lower bounds for floating point types in C? Like there are lower bounds for integral types (int being at least 16 bits)?

A: 

AS far as I know, a float always has 32 bits (at least on x86 systems). Floats, on the same architecture, always have the same size.

x3ro
An IEEE754 single-precision floating point number is defined to be 32 bits. The x86 is IEEE754 compliant. Therefore any pedants who say you can't count on a float being 32 bits on the x86 are flat wrong.
Crashworks
Well then, wasn't sure about that though, just wrote it to be sure ;)
x3ro
@Crashworks: You're assuming that the C compiler in question is using native x86 32bit floats. There is no requirement for it to do so. For example it would be perfectly valid for a C compiler to define both the `float` and `double` types to be a 64bit IEEE double, or for that matter to use a float emulation library. I can't think of any good reason to do the latter on x86, except to emulate another architecture, which hardly counts as being "on x86" from the user's POV. But it's legal.
Steve Jessop
+1  A: 

Maybe this helps: float.h reference (it is C++, I'm not sure if it applies to plain C as well)

Konamiman
+2  A: 

float.h contains many macros describing various properties of the floating types (including FLT_MIN and DBL_MIN).

The description of the requirements of the limits infloat.h is given in the standard (C90 or C99 - 5.2.4.2.2 "Characteristics of floating types").

In particular, according to the standard any implementation must support a lower-bound of at least 1E-37 for float or double. But an implementation is free to do better than that (and indicate what it does in FLT_MIN and DBL_MIN).

See this question for information on where to get a copy of the standards documents if you need one:

Michael Burr
That wasn't the question. Geekhero is asking for the bounds which the language puts on those constants, not how to get the values in a particular implementation
Steve Jessop
The question can probably be reasonably interpreted either way. Regardless, a better answer would discuss both areas (they're still kind of the same answer - the documentation for `float.h` talks about the required limits for an implementation, the contents of a particular `float.h` describe a particular implementation's limits. I've updated the answer to try to better cover both angles.
Michael Burr
+3  A: 

Yes. float.h contains constants such as:

FLT_EPSILON, DBL_EPSILON, LDBL_EPSILON this is the least magnitude non-zero value which can be represented by float, double, and long double representations.

FLT_MAX and FLT_MIN represent the extreme positive and negative numbers which can be represented for float. Similar DBL_ and LDBL_ are available.

FLT_DIG, DBL_DIG, LDBL_DIG are defined as the number of decimal digits precision.

You are asking for either the xxx_MIN or the xxx_EPSILON value.

Along these lines, here is a question wherein I posted some code which displays the internals of a 64-bit IEEE-754 floating-point number.

Heath Hunnicutt
That wasn't the question. Geekhero is asking for the bounds which the language puts on those constants, not how to get the values in a particular implementation.
Steve Jessop
Surely you realize the bounds are not defined by the language standard, but by the implementation? And that most implementations adhere to IEEE-754? But the OP can't count on that because the answer will vary by platform?
Heath Hunnicutt
The language standard defines bounds *on the bounds*. The OP says "like ... int being at least 16 bits". Similarly, float represents at least 6 decimal places. If it represents more, then FLT_DIG will be greater than 6, but it must not be less.
Steve Jessop
Steve, your point of view might be a better answer to the OP's question. You should add an answer.
Heath Hunnicutt
Unless you would like to community wiki this answer I wrote, I'd be glad to change it, but usually when I transform to wiki, nobody participates in the edits but me.
Heath Hunnicutt
Roman, ndim, and Konamiman's answers are to the question that I think was intended: they all point to lists of the minimum values required for the constants you mention (although they each chose different references, so I suppose there's something to be said for one answer to rule them all...)
Steve Jessop
+3  A: 

To be strict and grounded:

ISO/IEC 9899:TC2: (WG14/N1124m May 6, 2005):

5.2.4.2.2, Characteristics of floating types <float.h>

Roman Nikitchenko
Also, Annex F of that draft proposes that floats should adhere to IEEE-754, but is not specific about the size of long double.
Heath Hunnicutt
+1  A: 

This Draft C99 standard (PDF) notes minimum values for floating point type precision in section 5.2.4.2.2.

(Found via Wikipedia on C99.)

ndim
A: 
Crashworks
A: 

Excerpts from the Standard draft (n1401.pdf)

                                      Annex F
                                    (normative)
                       IEC 60559 floating-point arithmetic
    F.1 Introduction
1   ... An implementation that defines _ _STDC_IEC_559_ _ shall conform to
    the specifications in this annex. ...

    F.2 Types
1   The C floating types match the IEC 60559 formats as follows:
    -- The float type matches the IEC 60559 single format.
    -- The double type matches the IEC 60559 double format.
    -- The long double type matches an IEC 60559 extended format ...

Wikipedia has an article about IEC 559 (or rather IEEE 754-1985) you might find interesting.

pmg