float Type

This page was translated by a robot.

The type floatstores a real number , which is encoded as a floating point number with 4 bytes. A floatcan represent the value range of 1.175494e-38up 3.402823e+38to 6 decimal places, both positive and negative.

Details

The type floatis a 32-bit floating point number with a 23-bit mantissa. This corresponds to the single precision floating point type in the IEEE 754 standard. Some macros are defined in the floatslibrary , which more precisely specify the value range of floating-point types. The following program supplies the values ​​for the type floaton the author's system:





1.175494e-38
3.402823e+38
24
6
-125
-37
128
38
1.192093e-07
#include <stdio.h>
#include <float.h>

int main(){
  printf("%e\n",  FLT_MIN);         // smallest value
  printf("%e\n",  FLT_MAX);         // biggest value
  printf("%i\n",  FLT_MANT_DIG);    // radix digits of mantissa
  printf("%i\n",  FLT_DIG);         // accurate decimal digits
  printf("%i\n",  FLT_MIN_EXP);     // minimal radix exponent
  printf("%i\n",  FLT_MIN_10_EXP);  // minimal decimal exponent
  printf("%i\n",  FLT_MAX_EXP);     // maximal radix exponent
  printf("%i\n",  FLT_MAX_10_EXP);  // maximal decimal exponent
  printf("%e\n",  FLT_EPSILON);     // machine epsilon
  return 0;
}

Note that the mantissa number in the floatslibrary always adds an implicit bit. For more detailed information on what this bit is all about and how a floating point number is packed in 32 bits, see floating point coding ..

When specifying fixed values ​​in the source code, floatvalues ​​can be appended with a small for a large Fsuffix. Both suffixes mean the same thing, but the lowercase variant is usually used. Without this suffix, the compiler assumes it is a doublevalue ..

float
double
long double
1.234f
1.234
1.234L

Arithmetic Conversion

In the case of assignments, initializations or casts to a floattype, the compiler may perform arithmetic conversions . The conversion rules for the floattype are described below. In some cases, a compiler issues a warning for unsafe conversions, which can be suppressed if necessary using an explicit cast .

Arithmetic conversions of floating-point values ​​should be avoided whenever possible, regardless of whether they are implicitly or explicitly cast. They are still very expensive even in modern processors. It should also be noted that in certain situations floating point values ​​are automatically doubleconverted into a (see Promotion ).

When converting an integer type to an , an floatattempt is made to represent the number exactly whenever possible. However, since a floatcan only display 6 exact decimal places, only integer values ​​up to and including 7 decimal digits can be saved with guaranteed accuracy.



 7654321
87654320
float f1 =  7654321;
float f2 = 87654321;
printf(" %1.0f\n", f1);
printf("%1.0f\n", f2);

When a doubleor is converted long doubleto a , an floatattempt is also made to represent the number as accurately as possible. If the floating point number to be converted has no place in a float, then the maximum or minimum possible value is assumed:+-infinity





1234.567749
inf
double d1 = 1234.5678;
double d2 = 1e100;
float f1 = d1;
float f2 = d2;
printf("%f\n", f1);
printf("%f\n", f2);