# Float vs Double

In this article, we’ll look at the differences between float vs double data types in C/C++. But before we get into the differences, let’s go over some basic ideas like what float and double are, how much storage they take, how precise they are, and how to convert a float variable to a double variable and vice versa.

## What is Float Data Type?

Real numbers or huge numbers with a fractional component, such as 1.0,14.01,23.45,-21.560,191.123456, are stored as float data types. The fractional component is the part after the decimal.

Because we can see from the above that decimal allows a variable number of digits before and after it, the decimal point can float between the integers, decimal is referred to as floating-point. As a result, the floating-point data type is known as float.

Float can store the numbers between the range 3.4E-38 to 3.4E+38 i.e., from -3.4 x 1038 to +3.4 x 1038.

The syntax to declare float variables in C and C++ is as follows:

```float variable_name = value;
float weight = 85.6;```

We now have a basic understanding of what a float data type is. Let’s move on to some more interesting float facts.

Float is a 32-bit IEEE 754 single-precision floating-point number.

• 1-bit for the sign, 8-bit for exponent, and 23-bit for the value of mantissa.
• The size of a float is 4-bytes(32 bit), i.e., a float variable requires 4-bytes of computer memory space.
• Float has 6-digits of precision which means we can use up to 6 digits after the decimal; otherwise, it will truncate anything after that. For example, 12.4356716 can be stored in a variable using float data type.

## What is a Double Data Type?

Real numbers or huge numbers with a fractional component, such as -10.231,19.345621, are also stored as double data types.

Double can store numbers between the range -1.7E+308 to +1.7E+308 i.e. from -1.7 x 10308 to +1.7 x 10308.

The syntax to declare double variables in C and C++ is as follows:

```double variable_name = value;
double weight = 85.6;```

Here are some more interesting facts about double:

• Double is a 64-bit IEEE 754 double-precision floating-point number.
• 1-bit for the sign, 11-bit for exponent, and 52-bit for the value of mantissa.
• Precision is the total number of digits (or significant digits) of a real number.
• The size of a double is 8-bytes(64 bit), i.e., a double variable requires 8-bytes of computer memory space.
• Double has 15-digits of precision which means the double variable is significant up to 15 decimal digits, and hence it will truncate anything after that. For example, 12.435671123654328 can be stored in a variable using a double data type.

## Code Examples

Example 01: C++ float and double

``````#include <iomanip>
#include <iostream>
using namespace std;

int main() {
// Creating a double type variable
double a = 3.912348239293;

// Creating a float type variable
float b = 3.912348239293f;

// Printing the two variables
cout << "Double Type Number  = " << a << endl;
cout << "Float Type Number  = " << b << endl;

return 0;
}``````

Output:

Example 02: Using setprecision() For Floating-Point Numbers

``````#include <iomanip>
#include <iostream>

using namespace std;

int main() {
// Creating a double type variable
double a = 3.912348239293;

// Creating a float type variable
float b = 3.912348239293f;

// Setting the precision to 12 decimal places
cout << setprecision(13);

// Printing the two variables
cout << "Double Type Number  = " << a << endl;
cout << "Float Type Number      = " << b << endl;

return 0;
}``````

Output:

As we can see from the example above, we have specified the precision of up to 13 digits. The floating-point value we have assigned to our variables also consists of 13 digits. However, since float has a precision of up to only 7 digits, it shows garbage values after its precision is exceeded.

Our double variable shows the correct number because it has a precision of 15 digits, while the number itself consists of 13 digits.

As an alternative, we can specify different precisions for different variables while printing them.

Example 03: Different Precisions For Different Variables

``````#include <iomanip>
#include <iostream>

using namespace std;

int main() {
// Creating a double type variable
double a = 3.912348239293;

// Creating a float type variable
float b = 3.912348239293f;

// Setting precision to 11 for double
cout << "Double Type Number = " << setprecision(11) << a << endl;

// Setting precision to 7 for float
cout << "Float Type Number  = " << setprecision(7) << b << endl;

return 0;
}``````

Output:

## FAQs

### What is a float vs double?

Features of Float:

• Float is a single-precision data type.
• It has 6-digits precision.
• Float is a 32-bit floating-point data type.
• The float variable requires 4-bytes of memory space.

Features of Double:

• Double is a double-precision data type.
• It has 15-digits of precision.
• Double is a 64-bit floating-point data type.
• The double variable needs 8-bytes of memory space. Just double as that of float.

### Is 99.9 float or double?

Double is the default type for floating-point numbers. As a result, 99.9 is a double rather than the float.

### Which is better: float or double?

Double is more exact than float and can hold 64 bits, which is twice as much as the float. If we need accuracy up to 15 or 16 decimal points, we prefer double over float; otherwise, we may continue with the float in most cases because double is more expensive.