Open In App

C Float and Double

Last Updated : 14 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Float and double are two primitive data types in C programming that are used to store decimal values. They both store floating point numbers but they differ in the level of precision to which they can store the values.

In this article, we will study each of them in detail, their memory representation, and the difference between them.

Float

Float is used to store single-precision floating point numbers. It can store decimal values with precision up to 6-7 decimal places.

Syntax

float var_name;
  • The size of the float is 4 bytes.
  • Float can store values varying from 3.4 x 10-38 to 3.4 x 1038
  • It can store values up to 7 decimal points without loss of precision.
  • The format specifier for float is %f.

Example

C




// C Program to illustrate float
#include <stdio.h>
  
int main()
{
    // Syntax of declaring and initializing
    // the float variable
    float myVariable = 789.123456f;
  
    // printing floating point number
    printf("Float value is %f", myVariable);
    return 0;
}


Output

Float value is 789.123474

As you can see in the above output, the precision of decimal numbers is lost after the 7th digit due to the limited bits in float. In these cases, a double datatype is recommended.

Note: All the real number literals are of double type by default. We can append an “f” at the end of the literal to define it as float type.

Double

Double is used to store double precision floating point values. It is the greater version of float which can store real numbers with precision up to 15 decimal places.

  • The size of the double is 8 bytes.
  • The range of double is 1.7×10-308 to 1.7×10+308
  • It can store values up to 15 decimal points without loss of precision.
  • The format specifier for double is %lf

Example

C




#include <stdio.h>
  
int main()
{
    // Syntax of declaring and initializing
    // the double variable
    double myVariable = 789.123456;
    printf("Double value is %lf", myVariable);
    //%lf or %f both can be used to
    // print Float values
    return 0;
}


Output

Double value is 789.123456

How float and double are stored?

C language follows the IEEE 754 standard for representing floating point values in the memory. Unlike the int type that is directly stored in the memory in binary form, the float values are divided into two parts: exponent and mantissa, and then stored.

According to IEEE 754, the floating point values consist of 3 components:

  1. Sign Bit: This represents the sign of the number. 0 represents positive while 1 represents negative.
  2. Biased Exponent: The exponent of the number cannot be directly stored as it can be both negative or positive, so we use a biased exponent where we add some bias to the exponent.
  3. Normalized Mantissa: Matissa is the number in scientific notation, i.e. precision bits of the number.

C float Memory Representation

The size of the float is 32-bit, out of which:

  • The most significant bit (MSB) is used to store the sign of the number.
  • The next 8 bits are used to store the exponent.
  • The remaining 23 bits are used to store the mantissa.
memory representation of float

 

Example

Let’s take 65.125 as a decimal number that we want to store in the memory.

Converting to Binary form, we get:
65     = 1000001
0.125  = 001
So, 
65.125 = 1000001.001
       = 1.000001001 x 106
Normalized Mantissa = 000001001

Now, according to the standard,
we will get the baised exponent by adding the exponent to 127,
       = 127 + 6 = 133
Baised exponent = 10000101

And the signed bit is 0 (positive)

So, the IEEE 754 representation of 65.125 is,
0 10000101 00000100100000000000000

C double Memory Representation

The size of the float is 32-bit, out of which:

  • The most significant bit (MSB) is used to store the sign of the number.
  • The next 11 bits are used to store the exponent.
  • The remaining 52 bits are used to store the mantissa.
memory representation of double

 

Example

Let’s take the example of the same number 65.125,

From above,
    65.5 = 1.000001001 x 106
Normalized Mantissa = 000001001

Now, according to the standard, bais is 1023. So,
       = 1023 + 6 = 1029
Baised exponent = 10000000101

And the signed bit is 0 (positive)

So, the IEEE 754 representation of 65.125 is,
0 10000000101 0000010010000000000000000000000000000000000000000000

Differences between float and double

Points

Float

Double

Precision Float is single precision IEEE 754 floating point which provides precision up to 7 decimal points. Double is double precision IEEE 754 floating point that provides precision up to 15 decimal points.
Memory   Usage      Float uses 32 bits or 4 bytes of memory. Double uses 64 bits or 8 bytes of memory.
Range Float can store values varying from 3.4 x 10-38 to 3.4 x 10+38. The range of double is 1.7×10-308 to 1.7×10+308.
Format Specifier %f is the format specifier for float. %lf is the format specifier for double.
Memory Representation Sign = 1 bit
Exponent = 8 bits
Mantissa = 23 bits
Sign = 1 bit
Exponent = 11 bits
Mantissa = 52 bits

Conclusion

In conclusion, C uses both float and double for decimal numbers, but they vary in terms of precision, memory usage, range, and speed. When space is limited and precision can be compromised, it is better to use float there, while double is used for high-precision applications where space is not an issue. It’s essential to choose the appropriate data type based on the requirements of the application.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads