Friday, June 25, 2010

Calculating the limits of float and double in C

The range for float data type in C language is : +/- 3.4e +/- 38 (~7 digits)
and for double : +/- 1.7e +/- 308 (~15 digits)
How does one compute these limits?
I.e., given that float uses 4 bytes, and double 8 bytes, how 
can I compute the above values?

Ans : 
You were given the exact calculation, that gets the desired numbers.]

/*
 * ISO C Standard:  5.2.4.2.2  Characteristics of floating types <float.h>
 */


/* Radix of exponent representation, b. */
#undef FLT_RADIX
#define FLT_RADIX       __FLT_RADIX__

/* Maximum int x such that FLT_RADIX**(x-1) is a representable float, emax.  */

#define FLT_MAX_EXP     __FLT_MAX_EXP__

/* Maximum representable finite floating-point number,

        (1 - b**-p) * b**emax
*/

/* Minimum normalized positive floating-point number, b**(emin - 1).  */

No comments:

Blog Archive