| by Arround The Web | No comments

Float vs Double in C++ – What is the Difference Between Them

Computer languages support a variety of data types, including int, char, bool, float, and double. They are employed to store specific data that fits various data types in memory. Integer data types only store whole integers, while float and double data types are used to store decimals, Although their functions are identical, there are some differences as well. The distinction between the float and double data types in C++ will be examined and analyzed in this article.

float and double Data Types in C++

The data types “float” and “double” are used in C++ to represent floating-point values, which are decimal numbers that can be positive or negative. Now, let’s see each data type one by one.

What is float Data Type in C++?

In C++ language, the float data type is utilized for storing actual numbers or large values with a fractional component, such as 1.0, 27.4, 33.20, and 1235.722. They have a single precision. Which, in contrast to a double, are often used to hold decimal integers with less precision.

Syntax

The C++ syntax for defining a float variable is as follows:

float var_name;

 
As the above, the float is the data type that is stored in a floating type variable named var_name.

Example Program of float Data type

The following example code represents the variables of the float data type and multiplies them and recognizes the precision of the float data type:

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    float num1, num2, mul;
    int my_precision;
    cout << "Enter first float number: " << endl;
    cin >> num1;
    cout << "Enter second float number: " << endl;
    cin >> num2;
    cout << "Enter precision: ";
    cin >> my_precision;
    mul = num1 * num2;
    cout << fixed << setprecision(my_precision);
    cout << "The output is: " << mul << endl;
    cout << "The size in bytes is: " << sizeof(mul) << endl;
    return 0;
}

 
The above program is taking the two float numbers as user input and then multiplies them. The program is also taking the precision value. The output is:


The float data type takes 4 bytes.

What is double Data Type in C++?

In C++, double-precision real numbers or enormous numbers with a partial element, such as -11.431, and 18.387220 are referred to as double data types, they possess greater precision than a float and are represented by the double data type. Double variables can hold values more precisely as float variables however, they take up more memory:

Syntax

The following represents the C++ syntax for declaring a double variable:

double var_name;

 
In the above, double represents the datatype, which has a double type variable named var_name.

Example Program of double Datatype

The example code below represents the float data type variables, multiplies them, and also considers the float data type’s accuracy:

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

using namespace std;

int main()
{
    double num1, num2, mul;
    int my_precision;
    cout << "Enter first double number: " << endl;
    cin >> num1;
    cout << "Enter second double number: " << endl;
    cin >> num2;
    cout << "Enter precision: ";
    cin >> my_precision;
    mul = num1 * num2;
    cout << fixed << setprecision(my_precision);
    cout << "The output is: " << mul << endl;
    cout << "The size in bytes is: " << sizeof(mul) << endl;
    return 0;
}

 
This example is similar to the first example. The above program is taking the two double numbers as user input and then multiplies them. The program is also taking the precision value. The output is:

The code’s execution is shown by the output below:


The float data type takes 8 bytes.

Float vs Double in C++

There are some distinctions between both data types, despite the fact that the terms “float” and “double” may correspond to floating-point numbers. The following is the table that demonstrates the difference between the float and double data types.
 

Difference float double
Precision The precision of “float” is about accurate, 7 decimal digits. While the precision of “double” is approximately 15 decimal places.
Size Memory usage for “float” is 4 bytes. Whereas memory usage for “double” is 8 bytes. This indicates that “double” requires twice the amount of memory as “float”.
Usage For software applications like graphics or games that don’t need as much precision or memory. For tasks requiring greater precision, such as financial calculations or scientific computations.

 

Which is Preferable, float or double?

Double can store 64 bits, which is twice as many bits as float and is more precise. If we need accuracy up to 15 places in a decimal number, we choose double over float; otherwise, we can use the float in most cases because double takes up more space.

Conclusion

C++ uses float and double data types to represent floating-point integers values. Their size and precision are the main distinctions between the two. While float has less precision and uses less memory space, double has a higher level of precision. The program’s requirements will determine which option is best.

Share Button

Source: linuxhint.com

Leave a Reply