Difference Between Float And Double Data Types

SHARE

Data types are divided into two groups:

  • Primitive data types which include byte, short, int, long, float, double, boolean and char.
  • Non-primitive data types which include: String, Array and Classes 

You can declare variables as float or double, depending on the needs of your application. The principal differences between the two types are the significance they can represent, the storage they require, and their range. 

What is Double Data Type?

Double is the floating-point data type (traditionally) with the greatest precision. It gives more bits of “mantissa” as well as “exponent”, than the usual alternate choice “float”, in a lot of programming languages like ‘C’, Fortran and Java. As floating point numbers (values like 1.3333 or 2.666×10**15) get very large or very small, you can represent more possible different values between them if you have more storage for the mantissa.

It is called double data type because it can hold the double size of data compared to the float data type. A double has 8 bytes, which is equal to 64 bits in size. In double data type, the 1 bit for sign representation, 11 bits for the exponent and the remaining 52 bits used for the mantissa. The range of double is 1.7E-308 to 1.7E+308. Double data can be represents in real number (1, 10), decimals (0.1, 11.002) and minus (-1, -0.00002). It can hold approximately 15 to 16 digits before and after the decimal point.

What you need to know about double

  • Double is a 64-bit IEEE 754 double-precision floating-point number.
  • 1-bit for the sign, 11-bit for exponent, 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.

What is Float Data Type?

A float is a data type composed of a number that is not an integer, because it includes a fraction represented in decimal format. One of the most common definitions given from experts is that a float “has numbers on both sides of the decimal.” However, it may be simpler to say that the float includes decimal fractions, where the integer does not.

Floating-point numbers use the IEEE (Institute of Electrical and Electronics Engineers) format. Single-precision values with float type have 4 bytes, consisting of a sign bit, an 8-bit excess-127 binary exponent, and a 23-bit mantissa. The mantissa represents a number between 1.0 and 2.0. Since the high-order bit of the mantissa is always 1, it is not stored in the number. This representation gives a range of approximately 3.4E-38 to 3.4E+38 for type float.

What you need to know about float

  • Float is a 32-bit IEEE 754 single-precision floating-point number.
  • 1-bit for the sign, 8-bit for exponent, 23-bit for the value or 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.

Also Read: Difference Between P And NP Problems

Float Vs Double In Tabular Form

FLOATDOUBLE
Single Precision data-type.Double precision data type.
It can store numbers between the range 3.4E-38 to 3.4E+38 i.e., from -3.4 x 1038   to +3.4 x 1038 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 for declaring float variable:float weight=67.4;The syntax for declaring double data type:Double weight=78.9;
Format specifier for float data-type is %fFormat specifier for double data-type is %lf
Float is a 32-bit floating-point data type.1-bit for the sign, 8-bit for exponent, 23-bit for the value or mantissaDouble is a 64-bit floating-point data type.1-bit for the sign, 11-bit for exponent, 52-bit for the value or mantissa.
The float variable requires 4-bytes of memory space.Double variable requires 8-bytes of memory space. Just double as that of float.
Float has 6-digits of precision.Double has 15-digits of precision.
Conversion from float to double is valid, and no data is lost.Conversion from double to float is also valid, but data is lost.
Float is cost-effective, occupies less memory space.Double is costlier, occupies more memory space
It is good to use float when no or less precision is required.It is good to use double when high precision is required.

Also Read: Difference Between Primitive And Non-primitive Data Types

Conclusion

The double and float types are similar, but they differ in precision and range:

  • A float is a single precision, 32-bit floating-point data type that accommodates seven digits. Its range is approximately 1.5 × 10−45 to 3.4 × 1038.
  • A double is a double-precision, 64-bit floating-point data type. It accommodates 15 to 16 digits, with a range of approximately 5.0 × 10−345 to 1.7 × 10308.