Difference between float and double in C/C++

Difference between float and double in C/C++
Difference between float and double in C/C++

Introduction

Numbers are everywhere- on price tags, bills, books, license plates, phones, even on our keyboards. And Have you ever wondered how computers understand and store these numbers? Yes, you are right, they store everything in them in zeroes and ones.

But, Humans are more innovative and know more than just zeroes and ones. Thus some great engineers invented programming languages. The primary purpose of programming is to communicate an algorithm to a machine (computer), i.e., to make a computer do something.

There are about 700 programming languages, and each has its syntax, rules, and regulations.


Ok! But there is one thing common; In most programming languages, all basic data types are built-in. In addition, many languages also provide a set of composite data types. Now! What are data types? A data type specifies the type of data that a variable can store, such as integer, floating, character, etc.

Image Source: Quora

Today we are going to differentiate between float and double data types in C/C++. But before jumping directly into the differences, let us first talk about the fundamental concepts like what is float and double, the storage they require, their precisions, the conversion of a float variable to double, and vice-versa.

What is ‘float’ data type?

  • A float data type is used to store real numbers or large numbers with a fractional component like 1.0,14.01,23.45,-21.560,191.123456, etc. The part after the decimal is known as its fractional component.
  • Ok, but why the name ‘float’? 

Decimal is referred to as floating-point because we can observe from above that decimal supports a variable number of digits before and after it, which means decimal point can float between the numbers. Thus the name float data type comes from the floating-point.

  • Float can store the numbers between the range 3.4E-38 to 3.4E+38 i.e., from -3.4 x 1038   to +3.4 x 1038 
  •  The syntax to declare float variables in C and C++ is as follows:
  • float variable_name = value;
  • float weight = 85.6;

Now, we know the basic definition of a float data type. Let’s move on to read some more exciting facts 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.

The C++ program below shows the 6-digit precision of the float variable and truncating digits after that.

What is a ‘double’ data-type?

  • A double data type is also used to store real numbers or large numbers with a fractional component like -10.231,19.345621.

So what’s the difference between double and float?

The main difference between them is in its size and precision.

  • 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 to declare double variables in C and C++ is as follows:
    • double variable_name = value;
    • double weight = 85.6;

Some other interesting facts about double are as follows:

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.

Conversion of a float variable to double

  • Float occupies 4 bytes in memory and has a precision of 7 digits. 
  • Double occupies 8 bytes in memory and has a precision of 15 digits.

C++ code shows float to double-conversion:

From the above code output, we can observe that it is impossible to create more precision than what we already have. Thus if you typecast a float variable to a double, the resulting double will still only have a precision of 6 digits.

Conversion of a double variable to float

Converting from double to float will give you the closest possible float because double occupies 8 bytes in memory and has a precision of 15 digits. In contrast, float occupies half the size and precision of double.

C++ code shows double to float conversion:

Note:

  •  The compiler used for the above codes is the MinGW compiler which allows 6 digits precision. So, our variable values were rounded off and truncated to 6 digits by the compiler.
  • The precision supported by the compiler is smaller than the actual digits of the number. So the last digit is rounded off, and the rest is truncated.

SetPrecision() Function:

  • We can specify the number of decimal points to print in cout by using the setprecision() function in c++.
  • setprecision() function is defined in the iomanip header file, which stands for input/output manipulation.
  • Below c++ code shows use of setprecision() function.

From the above codes, we can infer the following points:

  • Conversion from float to double and double to float is valid, but the data or digits are lost due to the low precision of float if we convert double to float.
  • Double requires more memory space in comparison to float. Thus Double is costlier than float.
  • Generally, the float is used to store floating-point numbers, but Double is more often used when high precision is required, like in currencies, Sensex, etc.

Since we know everything about float and double, let’s create a table that discusses the differences between float and double for quick reference.

Difference Table

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.

Frequently Asked Questions

What is a float vs. double?


Features of Float:

1. Float is a single-precision data type.
2. Float has 6-digits of precision.
3. Float is a 32-bit floating-point data type.
4. The float variable requires 4-bytes of memory space.

Features of Double:

1. Double is a double-precision data type.
2. Double has 15-digits of precision.
3. Double is a 64-bit floating-point data type.
4. The double variable needs 8-bytes of memory space. Just double as that of float.

Is 99.9 float or double?

Floating-point numbers are by default of type double. Therefore 99.9 is a double, not a float.

What is a double data type example?

Double can store numbers between the range -1.7 x 10308 to +1.7 x 10308. Therefore 1.3 x 1038 is also an example of double.

Which is better: float or double?

Double is more precise than float and can store 64 bits; double the number of bits float can store. We prefer double over float if we need to do precision up to 15 or 16 decimal points; otherwise, we can stick to float in most applications, as double is more expensive.

What is a floating-point?

Decimal is referred to as floating-point because we know that decimal supports a variable number of digits before and after it, which means decimal point can float between the numbers.

Can a float be negative?

Yes, Floating-point numbers can be positive or negative.

Is double faster than float?

Yes, Operations on doubles are typically faster than floats, but double is costlier in terms of memory space.

Key Takeaways

This article briefly explains the concepts of float and double data types in C/C++.

We started with a formal definition of float followed by some interesting facts like why it has the name “float,” the storage it occupies, and its precision value. Then we moved on to double data-type and explained its storage, precision, how it’s different from float data-type.

We also discussed how typecasting from float to double and double to float is ideally allowed and valid but should be done carefully in the code because precision is lost if we convert double to float too often.

In the end, a table that discusses the differences between float and double is created for quick reference and recap.

Yeah! It is the end of the article but not of your learning. “Develop a passion for learning. If you do, you will never cease to grow.” If you ever feel the need for expert guidance to learn more concepts, Coding Ninjas will always be there; check out our DSA courses by starting your free trial today. 

Also, you can try as many problems as possible on our platform CodeStudio created by aspiring and creative minds, which provides you hassle-free, adaptive, and excelling online courses, practice questions, blogs, mentors-support, interview experiences, and everything you need to become the perfect candidate for your dream company!

By Aanchal Tiwari

Exit mobile version