DEV Community

Lalit Kumar
Lalit Kumar

Posted on

What is the difference between a float and a double?

For representing floating-point numbers, we use float, double and long double.


title: "What is the difference between a float and a double?"
tags: cpp

canonical_url: https://kodlogs.com/blog/706/what-is-the-difference-between-a-float-and-a-double

Float:

Float is a 32 bit IEEE 754 single-precision floating-point Numbers in which 1 bit is used for the sign, 8 bits for the exponent, and 32* for the value. The float data type has 7 decimal digits of precision.

Example:

float a = 1.f / 81;

float b = 0;

for (int i = 0; i < 729; ++ i)

 b += a;

printf("%.7g\n", b);
Enter fullscreen mode Exit fullscreen mode

Output:

9.000023

Double:

Double is a 64 bit IEEE 754 double-precision floating-point numbers in which 1 bit is used to represent the sign, 11 bits for the exponent, and 52* bits for the value. The double data type has 15 decimal digits of precision.

Example:

double a = 1.0 / 81;

double b = 0;

for (int i = 0; i < 729; ++ i)

b += a;

printf("%.15g\n", b);
Enter fullscreen mode Exit fullscreen mode

Output:

8.99999999999996
Note:
The maximum value of float is about 3e38 and double is about 1.7e308. So, using the float can hit “infinity” much more easily than double for something simple like computing the factorial of 60.

Top comments (0)