Compare the Difference Between Similar Terms

Difference Between float and double

Key Difference – float vs double
 

In programming, it is required to store data. The data is stored in memory. Memory locations that store data are called variables. Each memory location can store a specific type of data. Memory size for each data type is different. In programming languages such as Python, the programmer does not need to declare the type of the variable. In programming languages such as Java, the programmer should declare the variable type. There is a number of data types such as char, int, float and double. The char data type is used to store a single character value. The int data type is used to store numeric values without decimal points. The float and double data types are used to store numerical values with decimal points.  This article discusses the difference between float and double. The key difference between float and double is that float is a single precision 32 bit IEEE 754 floating point data type while double is a double precision 64 bit IEEE 754 floating point data type.

CONTENTS

1. Overview and Key Difference
2. What is float
3. What is double
4. Similarities Between float and double
5. Side by Side Comparison – float vs double in Tabular Form
6. Summary

What is float?

The float is a single precision 32-bit floating point. It is predefined data type supported by programming languages such as Java. To declare a float variable, the keyword ‘float’ is used. So it cannot be used for identifier names such as method names and variable names.  Refer the below program.

Figure 01: Java Program with float Data Type

According to the above program, the number is a variable that can store a floating point number. Here, -20.5f is used instead of the -20.5. -20.5 is a double literal. To indicate the compiler to store the value as a float, the programmer should write f or F.

What is double?

The double is a double precision 64-bit floating point. It is a predefined data type. To declare a double variable, the keyword ‘double’ is used. Therefore, it cannot be used for identifier names such as method names and variable names.  Refer the below program.

Figure 02: Java Program with double Data Type

According to the above program, the number is a variable of type double. Printing the number will give the output as -20.5. It takes 64 bits in the memory to store the value. If the programmer is written -20.5, it is considered as a double. He can also write it as -20.5d. Writing ‘d’ is optional.

Type casting can be performed on data types. It is the process of converting a one data type to another data type. When assigning a smaller data type to a larger data type, there is no casting required. The widening occurs in a byte, short, int, long, float, double order. When assigning a larger data type to a small data type, it is necessary to do the casting.

Figure 03: Casting

According to the above program, num1 and num2 have float data types. The summation is assigned to the variable sum. It is a float. As float is a smaller data type comparing to double, it can be directly assigned to the double variable number without a type casting.

The x and y can store double data types. The summation is assigned to the variable z. It can also store double. Type casting is required to assign a larger data type to a smaller data type. Therefore, to store the double value to a float variable, it is necessary to do type casting because double is a larger data type than float.

What are the Similarities Between float and double?

What is the Difference Between float and double?

float vs double

The float is a single precision 32 bit IEEE 754 floating point data type. The double is a double precision 64 bit IEEE 754 floating point data type.
 Number of Bytes
The float is 4 bytes long. The double is 8 bytes long.
Default Values
The default value of float is 0.0f. The default value of double is 0.0d.
 Keyword
The keyword ‘float’ is used to declare a floating value. The keyword ‘double’ is used to declare a double value.
Required Memory
The float requires less memory than double. The double requires more memory than float.

Summary – float vs double

In programming, it is necessary to store data. Those data are stored in memory locations and called variables. Each variable stores data of specific type. There are data types such as int, char, double and float etc. This article discussed the difference between two data types that are a float and double. The difference between float and double is that float is a data type, which is a single precision 32 bit IEEE 754 floating point while double is a data type, which is a double precision 64 bit IEEE 754 floating point.

Reference:

1. Java Variables and (Primitive) Data Types. Available here 
2.Point, Tutorials. “Java Basic Datatypes.”, Tutorials Point, 8 Jan. 2018. Available here