C Standard Library - <float.h>


Introduction

The float.h header file of the C standard library 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 understand that floating point numbers are composed of the following four elements:

pPrecision, 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 macro

The following values ​​are specific Implemented and defined via #define directives, these values ​​must not be lower than the values ​​given below. Note that all instances of FLT refer to type float, DBL to type double, and LDBL to type long double.

ComponentComponent description
SSymbol (+/-)
bThe base represented by the exponent, 2 represents binary, 10 represents Decimal, 16 for hexadecimal, etc...
e exponent, one between the minimum emin# An integer between ## and the maximum emax.
These macros define the base The smallest negative integer value of the exponent when it is 10. #These macros define the exponent when the base is FLT_RADIX Maximum integer value. ##FLT_MAX_10_EXP +37FLT_MAX 1E+37FLT_EPSILON 1E-5FLT_MIN 1E-37Example
MacroDescription
FLT_ROUNDSDefine the rounding mode for floating point addition , which can be any of the following values:
  • -1 - Unable to determine

  • 0 - Approaching zero

  • 1 - Go to the nearest value

  • 2 - Go towards positive infinity

  • 3 - Go towards negative infinity

FLT_RADIX 2This macro defines the base for exponent representation. Base 2 represents binary, base 10 represents decimal, and base 16 represents hexadecimal.

FLT_MANT_DIG

DBL_MANT_DIG

LDBL_MANT_DIG

These macros define the number of digits in the FLT_RADIX base.

FLT_DIG 6

DBL_DIG 10

LDBL_DIG 10

These macros define Will change the maximum value of the decimal number represented (base 10).

FLT_MIN_EXP

DBL_MIN_EXP

LDBL_MIN_EXP

These macros define the exponent when the base is FLT_RADIX Minimum negative integer value.
##FLT_MIN_10_EXP -37

DBL_MIN_10_EXP -37

LDBL_MIN_10_EXP -37

FLT_MAX_EXP

DBL_MAX_EXP

LDBL_MAX_EXP

DBL_MAX_10_EXP +37

LDBL_MAX_10_EXP +37

These macros define the base The maximum integer value of the exponent when it is 10.

DBL_MAX 1E+37

LDBL_MAX 1E+37

These macros Defines the largest finite floating point value.

DBL_EPSILON 1E-9

LDBL_EPSILON 1E-9

These macros Defines the smallest representable digit.

DBL_MIN 1E-37

LDBL_MIN 1E-37

These macros Defines the minimum floating point value.

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

#include <stdio.h>#include <float.h>int main(){
   printf("The maximum value of float = %.10e\n", FLT_MAX);
   printf("The minimum value of float = %.10e\n", FLT_MIN);

   printf("The number of digits in the number = %.10e\n", FLT_MANT_DIG);}

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

The maximum value of float = 3.4028234664e+38The minimum value of float = 1.1754943508e-38The number of digits in the number = 7.2996655210e-312