 # C++ float and double

In C++, both `float` and `double` data types are used for floating-point values. Floating-point numbers are used for decimal and exponential values. For example,

``````// creating float type variables
float num1 = 3.0f;
float num2 = 3.5f;
float num3 = 3E-5f; // 3x10^-5

// creating double type variables
double num4 = 3.0;
double num5 = 3.5;
double num6 = 3E-5; // 3x10^-5``````

We must add the suffix `f` or `F` at the end of a `float` value. This is because the compiler interprets decimal values without the suffix as `double`.

Consider this code.

``float a = 5.6;``

Here, we have assigned a `double` value to a `float` variable.

In this case, 5.6 is converted to `float` by the compiler automatically before it is assigned to the variable a. This may result in data loss. To learn more, visit C++ Type conversion.

## Difference Between float and double

float double
Size: 4 bytes Size: 8 bytes
Precision: In general, 7 decimal digits precision Precision: In general, 15 decimal digits precision
Example: `3.56f`, `3e5f` etc. Example: `3.56`, `3e5` etc.

Note: Unless you have a specific requirement, always use `double` instead of `float`, as `float` variables may be prone to introduce errors when working with large numbers.

### Example 1: 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

```Double Type Number  = 3.91235
Float Type Number = 3.91235```

Note: The compiler used for this example (MinGW compiler) allowed for 6 digits. So, our variable values were rounded off and truncated to 6 digits by the compiler.

## setprecision() to Specify Decimal Points

We can specify the number of decimal points to print in `cout` by using the `setprecision()` function.

This function is defined in the `iomanip` header file, which stands for input/output manipulation.

### Example 2: 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

```Double Type Number  = 3.912348239293
Float Type Number      = 3.912348270416```

As we can see from the example above, we have specified the precision up to 13 digits.

``cout << setprecision(13);``

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 3: 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

```Double Type Number = 3.9123482393
Float Type Number    = 3.912348```

From the program above, we can see that we have set two different precision values for `float` and `double`.

In both cases, the precision is smaller than the actual digits of the number. So the last digit is rounded off and the rest is truncated.

Note: If we specify the precision greater than the precision of the data type itself (7 for `float` and 15 for `double`), then the compiler will give us garbage values after the precision limit has been exceeded, as can be seen with the `float` output in example 2.

## Work with Exponential Numbers

As mentioned above, `float` and `double` can also be used to represent exponential numbers. For example,

``````// ex = 325 X (10 ^ 25)
double ex = 325E25;``````

C++ outputs exponential numbers and very large numbers in a format called the scientific format. The variable ex will be outputted in this format by default since it is a very large number.

In order to force C++ to display our floating-point numbers in the `scientific` format regardless of the size of the number, we use the format specifier `scientific` inside of `cout`.

``````double num = 3.25;

// ex = 325 X (10 ^ 25)
double ex = 325E25;

// using scientific format
cout << scientific << num;
cout << scientific << ex;``````

In addition to this, there is another format specifier known as `fixed`, which displays floating-point numbers in the decimal format.

It is similar to displaying floating-point numbers by only using `cout` without `setprecision()`, except for the fact that `fixed` displays numbers up to 6 decimal points.

On the other hand, only using `cout` displays digits according to the specific compiler (6 total digits in the case of MinGW compiler, including the digits before the decimal point).

### Example 4: Fixed and Scientific Formats

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

using namespace std;

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

// Creating an exponential double type variable
double ex1 = 325e+2;

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

// Creating an exponential float type variable
float ex2 = 325e+2f;

// Displaying output with fixed
cout << "Displaying Output With fixed:" << endl;
cout << "Double Type Number 1 = " << fixed << a << endl;
cout << "Double Type Number 2 = " << fixed << ex1 << endl;
cout << "Float Type Number 1  = " << fixed << b << endl;
cout << "Float Type Number 2  = " << fixed << ex2 << endl;

// Displaying output with scientific
cout << "\nDisplaying Output With scientific:" << endl;
cout << "Double Type Number 1 = " << scientific << a << endl;
cout << "Double Type Number 2 = " << scientific << ex1 << endl;
cout << "Float Type Number 1  = " << scientific << b << endl;
cout << "Float Type Number 2  = " << scientific << ex2 << endl;
return 0;
}``````

Output

```Displaying Output With fixed:
Double Type Number 1 = 3.912348
Double Type Number 2 = 32500.000000
Float Type Number 1  = 3.912348
Float Type Number 2  = 32500.000000

Displaying Output With scientific:
Double Type Number 1 = 3.912348e+000
Double Type Number 2 = 3.250000e+004
Float Type Number 1  = 3.912348e+000
Float Type Number 2  = 3.250000e+004```

## long double

Apart from `float` and `double`, there is another data type that can store floating-point numbers. This is known as `long double`.

It usually occupies a space of 12 bytes (depends on the computer system in use), and its precision is at least the same as `double`, though most of the time, it is greater than that of `double`.

`long double` values should end with `L`. For example,

``````// declaring a long double variable

long double num_ldb = 2.569L;``````

Note: The floating-point data types supported by C++ are `float`, `double` and `long double`. There is no `long float`.