Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Standard Library –

C Standard Library -

Introduction

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 the characteristics and limits of floating-point numbers, such as maximum value, minimum value, precision, etc.

The C standard library's float.h The 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 explaining these constants, it is best to understand that a floating-point number is composed of the following four elements:

Component Component description
S Sign ( +/- )
b The base of the exponent representation: 2 for binary, 10 for decimal, 16 for hexadecimal, and so on...
e The exponent, an integer between the minimum value emin and the maximum value emax .
p Precision, the number of significant digits in base b

Based on the above four 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 #define directives; none of these values may be lower than the values given below. Note that in all instances, FLT refers to the type float, DBL refers to the type double, and LDBL refers to the type long double.

Single-precision floating-point number (float)

Double-precision floating-point number (double)

Extended-precision floating-point number (long double)

Example

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

Example

#include

#include

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's 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

provides a set of macros for describing the characteristics and limits of floating-point types. By using these macros, we can write more robust code that handles boundary cases and special situations related to floating-point numbers.