C Standard Library β <float.h>
Introduction
<float.h> is a header file in the C standard library that defines macros related to floating-point types (float, double, and long double). These macros provide information about the characteristics and limits of floating-point numbers, such as maximum and minimum values, precision, etc.
The C standard library's float.h header file contains a set of platform-dependent constants related to floating-point values. These constants were proposed by ANSI C, making programs more portable. Before discussing these constants, it's best to understand that a floating-point number consists of the following four components:
| Component | Description |
|---|---|
| S | Sign (+/-) |
| b | Base of exponent representation: 2 for binary, 10 for decimal, 16 for hexadecimal, etc. |
| e | Exponent, an integer between minimum value **e min** and maximum value **e max**. |
| p | Precision, the number of significant digits in base b. |
Based on these four components, the value of a floating-point number is given by:
floating-point = ( S ) p Γ b e
or
floating-point = (+/-) precision Γ base exponent
Library Macros
The following values are implementation-specific and defined using the #define directive. All values must not be lower than those specified below. Note that all instances of FLT refer to the float type, DBL refers to the double type, and LDBL refers to the long double type.
Single Precision Floating-Point (float)
FLT_RADIX: The radix (base) of the floating-point representation (usually 2).FLT_MANT_DIG: Number of significant digits in the mantissa for thefloattype.FLT_DIG: Decimal precision for thefloattypeβthe maximum number of decimal digits that can be preserved without loss of precision.FLT_MIN_EXP: Minimum negative exponent (in base) for thefloattype.FLT_MIN_10_EXP: Minimum negative decimal exponent for thefloattype.FLT_MAX_EXP: Maximum exponent (in base) for thefloattype.FLT_MAX_10_EXP: Maximum decimal exponent for thefloattype.FLT_MAX: The largest positive value representable by thefloattype.FLT_MIN: The smallest positive value representable by thefloattype.FLT_EPSILON: Relative error for thefloattypeβthe difference between 1.0 and the smallest floating-point number greater than 1.0.
Double Precision Floating-Point (double)
DBL_MANT_DIG: Number of significant digits in the mantissa for thedoubletype.DBL_DIG: Decimal precision for thedoubletype.DBL_MIN_EXP: Minimum negative exponent (in base) for thedoubletype.DBL_MIN_10_EXP: Minimum negative decimal exponent for thedoubletype.DBL_MAX_EXP: Maximum exponent (in base) for thedoubletype.DBL_MAX_10_EXP: Maximum decimal exponent for thedoubletype.DBL_MAX: The largest positive value representable by thedoubletype.DBL_MIN: The smallest positive value representable by thedoubletype.DBL_EPSILON: Relative error for thedoubletype.
Extended Precision Floating-Point (long double)
LDBL_MANT_DIG: Number of significant digits in the mantissa for thelong doubletype.LDBL_DIG: Decimal precision for thelong doubletype.LDBL_MIN_EXP: Minimum negative exponent (in base) for thelong doubletype.LDBL_MIN_10_EXP: Minimum negative decimal exponent for thelong doubletype.LDBL_MAX_EXP: Maximum exponent (in base) for thelong doubletype.LDBL_MAX_10_EXP: Maximum decimal exponent for thelong doubletype.LDBL_MAX: The largest positive value representable by thelong doubletype.LDBL_MIN: The smallest positive value representable by thelong doubletype.LDBL_EPSILON: Relative error for thelong doubletype.
Example
The following example demonstrates the use of some constants defined in the float.h file.
Example
#include <stdio.h>
#include <float.h>
int main(){
printf("Single precision (float):n");
printf("FLT_RADIX: %dn", FLT_RADIX);
printf("FLT_MANT_DIG: %dn", FLT_MANT_DIG);
printf("FLT_DIG: %dn", FLT_DIG);
printf("FLT_MIN_EXP: %dn", FLT_MIN_EXP);
printf("FLT_MIN_10_EXP: %dn", FLT_MIN_10_EXP);
printf("FLT_MAX_EXP: %dn", FLT_MAX_EXP);
printf("FLT_MAX_10_EXP: %dn", FLT_MAX_10_EXP);
printf("FLT_MAX: %en", FLT_MAX);
printf("FLT_MIN: %en", FLT_MIN);
printf("FLT_EPSILON: %en", FLT_EPSILON);
printf("n Double precision (double):n");
printf("DBL_MANT_DIG: %dn", DBL_MANT_DIG);
printf("DBL_DIG: %dn", DBL_DIG);
printf("DBL_MIN_EXP: %dn", DBL_MIN_EXP);
printf("DBL_MIN_10_EXP: %dn", DBL_MIN_10_EXP);
printf("DBL_MAX_EXP: %dn", DBL_MAX_EXP);
printf("DBL_MAX_10_EXP: %dn", DBL_MAX_10_EXP);
printf("DBL_MAX: %en", DBL_MAX);
printf("DBL_MIN: %en", DBL_MIN);
printf("DBL_EPSILON: %en", DBL_EPSILON);
printf("n Extended precision (long double):n");
printf("LDBL_MANT_DIG: %dn", LDBL_MANT_DIG);
printf("LDBL_DIG: %dn", LDBL_DIG);
printf("LDBL_MIN_EXP: %dn", LDBL_MIN_EXP);
printf("LDBL_MIN_10_EXP: %dn", LDBL_MIN_10_EXP);
printf("LDBL_MAX_EXP: %dn", LDBL_MAX_EXP);
printf("LDBL_MAX_10_EXP: %dn", LDBL_MAX_10_EXP);
printf("LDBL_MAX: %Len", LDBL_MAX);
printf("LDBL_MIN: %Len", LDBL_MIN);
printf("LDBL_EPSILON: %Len", LDBL_EPSILON);
return 0;
}
Let's compile and run the above program. This will produce the following output:
Single precision (float):
FLT_RADIX: 2
FLT_MANT_DIG: 24
FLT_DIG: 6
FLT_MIN_EXP: -125
FLT_MIN_10_EXP: -37
FLT_MAX_EXP: 128
FLT_MAX_10_EXP: 38
FLT_MAX: 3.402823e+38
FLT_MIN: 1.175494e-38
FLT_EPSILON: 1.192093e-07
Double precision (double):
DBL_MANT_DIG: 53
DBL_DIG: 15
DBL_MIN_EXP: -1021
DBL_MIN_10_EXP: -307
DBL_MAX_EXP: 1024
DBL_MAX_10_EXP: 308
DBL_MAX: 1.797693e+308
DBL_MIN: 2.225074e-308
DBL_EPSILON: 2.220446e-16
Extended precision (long double):
LDBL_MANT_DIG: 64
LDBL_DIG: 18
LDBL_MIN_EXP: -16381
LDBL_MIN_10_EXP: -4931
LDBL_MAX_EXP: 16384
LDBL_MAX_10_EXP: 4932
LDBL_MAX: 1.189731e+4932
LDBL_MIN: 3.362103e-4932
LDBL_EPSILON: 1.084202e-19
<float.h> provides a set of macros used to describe the characteristics and limits of floating-point types. By using these macros, we can write more robust code that handles edge cases and special conditions related to floating-point arithmetic.
YouTip