Difference between float and double in C/C++
To represent floating point numbers, we use float, double and long double. What’s the difference? double has 2x more precision than float. float is a 32-bit IEEE 754 single precision Floating Point Number – 1 bit for the sign, 8 bits for the exponent, and 23* for the value. float has 7 decimal digits of precision. double is a 64-bit IEEE 754 double precision Floating Point Number – 1 bit for the sign, 11 bits for the exponent, and 52* bits for the value. double has 15 decimal digits of precision. Let’s take an example (taken from here): For a quadratic equation x2 – 4.0000000 x + 3.9999999 = 0, the exact roots to 10 significant digits are, r1 = 2.000316228 and r2 = 1.999683772. Notice the difference in using float and double.
roots of equation x2 - 4.0000000 x + 3.9999999 = 0 are : for float values: 2.00000 2.00000 for double values: 2.00032 1.99968
Let us see the differences in a tabular form that is as follows:
|Its size is 4 bytes||Its size is 8 bytes|
|It has 7 decimal digits precision||It has 15 decimal digits precision|
|It is an integer data type but with decimals||It is an integer data type but with decimals|
|It may get Precision errors while dealing with large numbers||It will not get precision errors while dealing with large numbers.|
|This data type supports up to 7 digits of storage.||This data type supports up to 15 digits of storage.|
|For float data type, the format specifier is %f.||For double data type, the format specifier is %lf.|
|For example -: 3.1415||For example -: 5.3645803|
|It is less expensive.||It is costly.|
|It requires less memory space as compared to double data type.||It needs more resources such as occupying more memory space in comparison to float data type.|
|It is suitable in graphics libraries for greater processing power because of its small range.||It is suitable to use in the programming language to prevent errors while rounding off the decimal values because of its wide range.|
This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.