What is the difference between float and double?

Float and double are both widely used data types in programming that have the ability to store decimal or floating-point​ numbers. The only difference between them is the precision.

A float is a 32-bit IEEE 754 single-precision floating-point number.

1 bit for the sign 8 bits for the exponent 23 bits for the value.

A float has 7 decimal digits of precision and occupies 32 bits.

A double is a 64-bit IEEE 754 double-precision floating-point number. 1 bit for the sign, 11 bits for the exponent, and 52 bits for the value. A double has 15 decimal digits of precision and occupies a total of 64 bits.

Below is example for further clarification. See how float cannot store the entire value since it has less precision than double.

using System;
class HelloWorld
{
static void Main()
{
float f = 1F/3; //1F is float value of 1
double d = 1D/3; // 1D is 1 stored as a double
Console.WriteLine("float: {0} double: {1}", f, d);
}
}

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved