double Type

This page was translated by a robot.

The type doublestores a real number , which is encoded as a floating point number with 8 bytes. A doublecan represent the value range of 2.225074e-308up 1.797693e+308to 15 decimal places, both positive and negative.

Details

The type doubleis a 64-bit floating point number with a 52-bit mantissa. This corresponds to the double 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 doubleon the author's system:





2.225074e-308
1.797693e+308
53
15
-1021
-307
1024
308
2.220446e-16
#include <stdio.h>
#include <float.h>

int main(){
  printf("%e\n",  DBL_MIN);         // smallest value
  printf("%e\n",  DBL_MAX);         // biggest value
  printf("%i\n",  DBL_MANT_DIG);    // radix digits of mantissa
  printf("%i\n",  DBL_DIG);         // accurate decimal digits
  printf("%i\n",  DBL_MIN_EXP);     // minimal radix exponent
  printf("%i\n",  DBL_MIN_10_EXP);  // minimal decimal exponent
  printf("%i\n",  DBL_MAX_EXP);     // maximal radix exponent
  printf("%i\n",  DBL_MAX_10_EXP);  // maximal decimal exponent
  printf("%e\n",  DBL_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 64 bits, see floating point coding

When specifying fixed values ​​in the source code, floating point numbers are accepted as values ​​by default double. By specifying a suffix, the compiler can be informed that it is a floatvalue or a long doublevalue .

float
double
long double
1.234f
1.234
1.234L

Arithmetic Conversion

In the case of assignments, initializations or casts to a doubletype, the compiler may perform arithmetic conversions . The conversion rules for the doubletype 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 doubleattempt is made to represent the number exactly whenever possible. However, since a doublecan map exactly 15 decimal places, all integer values ​​up to and including 16 decimal digits can be stored exactly. This is sufficient for most cases, but can lead to problems with long long-values :



 6543210987654321
76543210987654320
double d1 =  6543210987654321LL;
double d2 = 76543210987654321LL;
printf(" %1.0f\n", d1);
printf("%1.0f\n", d2);

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





1234.567800
inf
long double l1 = 1234.5678;
long double l2 = 1e400L;
double d1 = l1;
double d2 = l2;
printf("%f\n", d1);
printf("%f\n", d2);  return 0;

When converting one floatto one double, the value can be stored exactly in any case.