Float and Double both are the data types under Floating-point type. The Floating-point numbers are the real numbers that have a fractional component in it. The primary difference between float and double is that the float type has 32-bit storage. On the other hand, the double type has 64-bit storage.
There are some other differences between float and double that are discussed in the comparison chart given below.
Content: Float Vs Double
Comparison Chart
Basis for Comparison | Float | Double |
---|---|---|
Precision | Single precision. | Double precision. |
Bits | 32 bits. | 64 bits. |
Bytes | 4 bytes . | 8 bytes. |
Approximate Range | 1.4e-045 to 3.4e+038 | 4.9e-324 to 1.8e+308 |
Bits Representation | 1 bit represent sign bit. 8 bit represent exponent. 23 bits represent mantissa. | 1 bit represent sign bit. 11 bit represent exponent. 52 bits represent mantissa. |
Accuracy | Less accurate. | More Accurate. |
Definition of Float
A datatype float is one of the Floating-point types. The datatype float has 32-bit storage (which is equal to 4 bytes) for the variable of float type. Data type float specifies single precision. The representation of 32 bit in float can be explained as 1 bit is represented as the sign bit, 8 bits are represented as an exponent, and 23 bits are represented as mantissa.
The maximum range of a float type is 1.4e-045 to 3.4e+038. When compared with double floating-point type float type is less accurate while mathematical calculation. Let us understand float using an example.
#include <stdio.h> #include <math.h> int main() { float num1 = sqrt(64.23) cout<< num1; } //output 8.00060
As in above code, you can observe that a float variable num1 is assigned a value that is the by the function sqrt( ), which returns the square root of the value which is passed to this function. You can observe that when the value in num1 is printed it is near about the exact value, but it is not accurate. Let us now see the example below when the same program is executed using double as the datatype.
Definition of Double
Double is the second kind of floating-point datatype. A datatype double has 64-bit storage (which is equal to 8 bytes) for a variable of double type. It specifies double precision as its size is just double of the float. The 64 bits representation of type double can be explained as 1 bit represents sign bit, 11 bits represents an exponent, and remaining 52 bits represents mantissa.
Among float and double most commonly used data type is double. The type double is used during mathematical calculation, and when there is a need perfect accuracy. The mathematical functions sin( ), cos( ), and sqrt( ) always return a double value. Let us understand the accuracy of the datatype double with an example.
#include <stdio.h> #include <math.h> int main() { double num1 = sqrt(64.23) cout<< num1; } //output 8.0143621
You can observe that the output obtained in the example, explaining float is different from the output obtained in the example, explaining double. So, from this, we can say that the results obtained from double are more accurate as compared to float.
Key Differences Between Float and Double
- The datatype float specifies single precision that means when compared to double it has less accuracy whereas, the double specifies double precision as it is just double of float its error is negligible as compared to float.
- A variable of float type has storage of 32 bits whereas, a variable of double type has storage of 64 bits which compiles that double is greater in storage as compared to float.
- The value in float can range from 1.4e-045 to 3.4e+038 whereas, the value of type double can range from 4.9e-324 to 1.8e+308.
- The bits representation of a float value resembles that 1 bit of float is used for sing bit, the 8 bits for exponent and 23 bits for storing mantissa. On the other hand, a double value resembles that 1 bit of it is used for sing bit, the 11 bits for exponent and 52 bits of it for storing the mantissa.
- When compared with double float is less accurate hence, while mathematical calculation double is used.
Conclusion
Well, you must generally use double as it provides the accuracy which is our main moto most of the times.
Leave a Reply