Open In App

# 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: For a quadratic equation x^2 – 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.

## C++

 `// C program to demonstrate``// double and float precision values` `#include ``#include ` `// utility function which calculate roots of``// quadratic equation using double values``void` `double_solve(``double` `a, ``double` `b, ``double` `c)``{``    ``double` `d = b * b - 4.0 * a * c;``    ``double` `sd = ``sqrt``(d);``    ``double` `r1 = (-b + sd) / (2.0 * a);``    ``double` `r2 = (-b - sd) / (2.0 * a);``    ``printf``(``" % .5f\t % .5f\n "``, r1, r2);``}` `// utility function which calculate roots of``// quadratic equation using float values``void` `float_solve(``float` `a, ``float` `b, ``float` `c)``{``    ``float` `d = b * b - 4.0f * a * c;``    ``float` `sd = sqrtf(d);``    ``float` `r1 = (-b + sd) / (2.0f * a);``    ``float` `r2 = (-b - sd) / (2.0f * a);``    ``printf``(``" % .5f\t % .5f\n "``, r1, r2);``}` `// driver program``int` `main()``{``    ``float` `fa = 1.0f;``    ``float` `fb = -4.0000000f;``    ``float` `fc = 3.9999999f;``    ``double` `da = 1.0;``    ``double` `db = -4.0000000;``    ``double` `dc = 3.9999999;` `    ``printf``(``"roots of equation x^2- 4.0000000 x + 3.9999999= "``           ``"0 are: \n "``);``    ``printf``(``"for float values: \n"``);``    ``float_solve(fa, fb, fc);` `    ``printf``(``"for double values: \n"``);``    ``double_solve(da, db, dc);``    ``return` `0;``}`

Output

```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
```

### Complexity Analysis

• The time complexity of the given code is O(1)
• The auxiliary space complexity of the code is also O(1)

## Difference between float and double

Let us see the differences in a tabular form that is as follows:

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 review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.