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、doubleandlong double) related macros. These macros provide characteristics and limitations of floating-point numbers, such as maximum value, minimum value, precision, etc.

of the C Standard Libraryfloat.hThe header file contains a set of platform-dependent constants related to floating-point values. These constants were introduced by ANSI C, which makes programs more portable. Before explaining these constants, it is best to first understand that floating-point numbers are composed of the following four elements:

Componentscomponent description
SSymbol ( +/- )
bThe radix of exponent representation, 2 for binary, 10 for decimal, 16 for hexadecimal, etc.
eexponent, one between the minimum valueeminand maximum valueemaxintegers between.
pprecision, the number of significant digits in base b

Based on the above 4 components, the value of a floating-point number is as follows:

floating-point = ( S ) p x be

or

floating-point = (+/-) precision x baseexponent

Library macros

The following values are implementation-specific and are defined via the #define directive. These values must not be lower than the values given below. Note that in all instances FLT refers to type float, DBL refers to type double, and LDBL refers to type long double.

Single-precision floating-point number (float)

  • FLT_RADIX: the base (or radix) of the floating-point number (usually 2).
  • FLT_MANT_DIG:floatNumber of significant digits for the type.
  • FLT_DIG:floatThe decimal precision of the type, i.e., the maximum number of decimal digits that can be guaranteed without loss.
  • FLT_MIN_EXP:floatThe minimum negative exponent of the type (in base radix).
  • FLT_MIN_10_EXP:floatMinimum negative decimal exponent for the type.
  • FLT_MAX_EXP:floatThe maximum exponent of the type (in base radix).
  • FLT_MAX_10_EXP:floatMaximum decimal exponent for the type.
  • FLT_MAX:floatMaximum positive value for the type.
  • FLT_MIN:floatMinimum positive value for the type.
  • FLT_EPSILON:floatThe relative error of the type, i.e., the difference between 1.0 and the smallest floating-point number greater than 1.0.

Double-precision floating-point number (double)

  • DBL_MANT_DIG:doubleNumber of significant digits for the type.
  • DBL_DIG:doubleDecimal precision for the type.
  • DBL_MIN_EXP:doubleThe minimum negative exponent of the type (in base radix).
  • DBL_MIN_10_EXP:doubleMinimum negative decimal exponent for the type.
  • DBL_MAX_EXP:doubleThe maximum exponent of the type (in base radix).
  • DBL_MAX_10_EXP:doubleMaximum decimal exponent for the type.
  • DBL_MAX:doubleMaximum positive value for the type.
  • DBL_MIN:doubleMinimum positive value for the type.
  • DBL_EPSILON:doubleRelative error for the type.

Extended-precision floating-point number (long double)

  • LDBL_MANT_DIG:long doubleNumber of significant digits for the type.
  • LDBL_DIG:long doubleDecimal precision for the type.
  • LDBL_MIN_EXP:long doubleThe minimum negative exponent of the type (in base radix).
  • LDBL_MIN_10_EXP:long doubleMinimum negative decimal exponent for the type.
  • LDBL_MAX_EXP:long doubleThe maximum exponent of the type (in base radix).
  • LDBL_MAX_10_EXP:long doubleMaximum decimal exponent for the type.
  • LDBL_MAX:long doubleMaximum positive value for the type.
  • LDBL_MIN:long doubleMinimum positive value for the type.
  • LDBL_EPSILON:long doubleRelative error for the type.

Example

The following example demonstrates the use of some constants defined in the float.h header file.

Example

#include <stdio.h>
#include <float.h>

int main() {
    printf("Single precision (float):\n");
    printf("FLT_RADIX: %d\n", FLT_RADIX);
    printf("FLT_MANT_DIG: %d\n", FLT_MANT_DIG);
    printf("FLT_DIG: %d\n", FLT_DIG);
    printf("FLT_MIN_EXP: %d\n", FLT_MIN_EXP);
    printf("FLT_MIN_10_EXP: %d\n", FLT_MIN_10_EXP);
    printf("FLT_MAX_EXP: %d\n", FLT_MAX_EXP);
    printf("FLT_MAX_10_EXP: %d\n", FLT_MAX_10_EXP);
    printf("FLT_MAX: %e\n", FLT_MAX);
    printf("FLT_MIN: %e\n", FLT_MIN);
    printf("FLT_EPSILON: %e\n", FLT_EPSILON);

    printf("\nDouble precision (double):\n");
    printf("DBL_MANT_DIG: %d\n", DBL_MANT_DIG);
    printf("DBL_DIG: %d\n", DBL_DIG);
    printf("DBL_MIN_EXP: %d\n", DBL_MIN_EXP);
    printf("DBL_MIN_10_EXP: %d\n", DBL_MIN_10_EXP);
    printf("DBL_MAX_EXP: %d\n", DBL_MAX_EXP);
    printf("DBL_MAX_10_EXP: %d\n", DBL_MAX_10_EXP);
    printf("DBL_MAX: %e\n", DBL_MAX);
    printf("DBL_MIN: %e\n", DBL_MIN);
    printf("DBL_EPSILON: %e\n", DBL_EPSILON);

    printf("\nExtended precision (long double):\n");
    printf("LDBL_MANT_DIG: %d\n", LDBL_MANT_DIG);
    printf("LDBL_DIG: %d\n", LDBL_DIG);
    printf("LDBL_MIN_EXP: %d\n", LDBL_MIN_EXP);
    printf("LDBL_MIN_10_EXP: %d\n", LDBL_MIN_10_EXP);
    printf("LDBL_MAX_EXP: %d\n", LDBL_MAX_EXP);
    printf("LDBL_MAX_10_EXP: %d\n", LDBL_MAX_10_EXP);
    printf("LDBL_MAX: %Le\n", LDBL_MAX);
    printf("LDBL_MIN: %Le\n", LDBL_MIN);
    printf("LDBL_EPSILON: %Le\n", LDBL_EPSILON);

    return 0;
}

Let us compile and run the above program, which will produce the following result:

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 for describing the characteristics and limitations of floating-point types. By using these macros, we can write more robust code to handle edge cases and special situations related to floating-point numbers.

other extensions