Differences between float, double and long double in C/C++

This article will introduce the differences between float, double and long double in C/C++.

There are three parts in this artcile, including definition of float, double, long double and testing them on specific platform with C and C++ language.

Definition

Float, double and long double are floating-point number types in arithmetic types in C/C++ language.

All of them have the smallest significant digits and value range. You can refer to the below table.

TypeMeaningSmallest significant digitsValue range
floatSingle precision floating-point number6 significant digits-3.4*10^(38)~3.4*10^(38)
doubleDouble precision floating point numbers10 significant digits-1.7*10^(308)~1.7*10^(308)
long doubleExtended precision floating point numbers10 significant digitsDepend on system.

For each type, it has different numbers of bit. They are all made up of three parts: Sign bits, mantissa bits, exponent bits.

Floating point typeNumber of bitssignmantissaexponent
float321238
double6415211
long doubleDepend on system

In a C program

Below is the code used to test the size, significant digits and precision of float, double and long double. Also it shows you how to print them in printf function.

#include <stdio.h>

int main()
{
	short s = 1;
	int i = 2;
	long l = 3;
	long long ll = 4;
	float f = 5.17889843f;
	double d = 6.142142145123456;
	long double ld = 7.1412512432234524535345423L;
	printf("short size = %zd\n", sizeof(s));
	printf("int size = %zd\n", sizeof(i));
	printf("long size = %zd\n", sizeof(l));
	printf("long long size = %zd\n", sizeof(ll));
	printf("float size = %zd, value = %13.13f\n", sizeof(f), f);
	printf("short size = %zd, value = %17.17lf\n", sizeof(d), d);
	printf("short size = %zd, value = %52.52Lf\n", sizeof(ld), ld);
        return 0;
}

Resut on Vistual studio 2017:

Result on gcc (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609 (Ubuntu)

Result analysis:

From the above result, we can got the below ideas.

  • As for long type, it is 4 bytes in windows, but it is 8 bytes in linux.
  • As for long double type, it is 8 bytes in windows, but it is 16 bytes in linux.
  • From the result, you can know the significan digits of float is 7, both double and long double are 16. Even it has print many digits, but they are not the same with value, they are some useless digits.
  • if you didn’t print them with specific precison, it will only print 7 significan digits.

In a C++ program

Below is the C++ code to test it.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	short s = 1;
	int i = 2;
	long l = 3;
	long long ll = 4;
	float f = 5.17889843f;
	double d = 6.142142145123456;
	long double ld = 7.1412512432234524535345423L;
	cout << "short size is = " << sizeof(s) << endl;
	cout << "int size is = " << sizeof(i) << endl;
	cout << "long size is = " << sizeof(l) << endl;
	cout << "long long size is = " << sizeof(ll) << endl;
	cout << "float size is = " << sizeof(f) << " value = " << setprecision(7) << f << endl;
	cout << "double size is = " << sizeof(d) << " value = " << setprecision(16) << d << endl;
	cout << "long double size is = " << sizeof(ld) << " value = " << setprecision(52) << ld << endl;
	return 0;
}

Result on Vistual studio 2017:

Result on g++ (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609

Result anslysis:

The result is the same with C code and we also need to set precision by ourself. othewise, it will print 5 ~ 6 significant digits.

Note:

Additional, two points you can take away.

  1. The correct way to set a float and double float is shown below. f is for float and L is for long double. the default is double, so we don’t need to add anything when we set a constant value to double type.
    1. float f = 5.17889843f;
    2. long double ld = 7.1412512432234524535345423L;

2. Format specifiers for printf to print float is “%f”, double is “%f” or “%lf”, long double is “Lf”.

If you would like to know more about printf format specifiers, you can visit An Article to Introduce Detailed Printf Usage.

Share this article to your social media
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments