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.

Key Differences Between float and double:

Feature float double
Memory Size Typically 4 bytes Typically 8 bytes
Precision Approximately 6–7 decimal places Approximately 15–16 decimal places
Range ±3.4e−38 to ±3.4e+38 ±1.7e−308 to ±1.7e+308
Usage Used when lower precision is enough Used when higher precision is needed

Example: Basic float Usage

In this example, pi is a float with limited precision, displaying only up to 7 decimal places accurately.

Code:

#include <stdio.h>

int main() {
    float pi = 3.1415926535f;
    printf("Float value of pi: %.7f\n", pi);
    return 0;
}

Output:

Float value of pi: 3.1415927

Example: Basic double Usage

Here, pi is a double with higher precision, displaying up to 15 decimal places, which is accurate enough for most scientific applications.

Code:

#include <stdio.h>
int main() {
    double pi = 3.141592653589793;
    printf("Double value of pi: %.15f\n", pi);
    return 0;
}

Output:

Double value of pi: 3.141592653589793

Example Comparison: float vs. double

This example demonstrates the precision difference by calculating a mathematical constant in both float and double.

Code:

#include <stdio.h>
int main() {
    float f_value = 1.0f / 7;
    double d_value = 1.0 / 7;

    printf("Float value: %.10f\n", f_value);
    printf("Double value: %.15f\n", d_value);

    return 0;
}

Output:

Float value: 0.1428571492
Double value: 0.142857142857143

This example shows that double maintains more accuracy (15 decimal places) than float, which is beneficial when precision is crucial.

When to Use float vs. Double:

  • Use float if memory is limited or if lower precision is acceptable (e.g., graphics processing).
  • Use double when you need greater precision, especially in scientific and financial applications, to reduce rounding errors.

Summary:

  • float and double are both used for floating-point numbers.
  • float is less precise but uses less memory, while double provides higher precision but uses more memory.
  • Choosing between them depends on the application's need for precision versus memory efficiency.


Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/c-programming-exercises/c-snippets/What-is-the-difference-between-float-and-double.php