w3resource

C programming: Difference between float and double

What is the difference between float and double in C?

There are three data types (float, double, long double) that represent fractional numbers. While the sizes and ranges of these types are consistent across most computer systems in use today, historically the sizes of these types varied from system to system.

float:

The float data type is the smallest of the three floating point types, if they differ in size at all. Its minimum value is stored in the FLT_MIN, and should be no greater than 1e-37. Its maximum value is stored in FLT_MAX, and should be no less than 1e37.

  • Floats are the most basic type of floating point number.
  • In accordance with IEEE standards, a single precision floating point number consists of exactly 32 bits long:
    • one bit for indicating the sign.
    • 23 bits ( plus one implied ) for recording the digits as a 24-bit integer. It is approximately 6 or 7 decimal digits of precision.
    • Using 8 bits for a binary exponent, the digits can be shifted left or right. This works out to an absolute range from about 10^-38 to 10^38.
  • Note that because a float only has 24 bits available for storing the digits of the number, it can actually be less precise than a 32-bit int for certain large integers.

double:

The double data type is at least as large as the float type, and it may be larger. Its minimum value is stored in DBL_MIN, and its maximum value is stored in DBL_MAX.

  • Double precision data types use twice as many bits as floats, resulting in approximately twice as many digits of precision as floats.
  • The IEEE standard specifies that a double precision floating point number consists of 64 bits:
    • one sign bit.
    • There are 52 bits (plus one implied) for recording digits, which is equivalent to about 15 decimal digits of precision.
    • 11 bits for the exponent, which works out to a range of about 10^-308 to 10^308.
  • For most scientific and engineering calculations, the double data type is preferred.

long double:

The long double data type is at least as large as the float type, and it may be larger. Its minimum value is stored in LDBL_MIN, and its maximum value is stored in LDBL_MAX.

  • The long double type is guaranteed to have more bits than a double, but the exact number may vary depending on the hardware platform. The most typical implementations are either 80 or 128 bits.
  • In accordance with IEEE standards, quadruple precision floating point numbers consist of 128 bits:
    • one sign bit.
    • 112 bits ( plus one implied ) for digits, working out to about 34 decimal digits of precision.
    • 15 bits for the exponent, giving a range from about 10^-4932 to 10^4932.


Follow us on Facebook and Twitter for latest update.