37

Possible Duplicate:
long double vs double

I am new to programming and I am unable to understand the difference between between long double and double in C and C++. I tried to Google it but was unable to understand it and got confused. Can anyone please help.?

phuclv
  • 37,963
  • 15
  • 156
  • 475
user1543957
  • 1,758
  • 4
  • 20
  • 30
  • 4
    I would expect this to depend on your compiler. Did you look at the docs? – Jonathan Wood Jan 08 '13 at 18:27
  • My compiler is gcc-4.3.4 – user1543957 Jan 08 '13 at 18:28
  • 1
    The difference is the size. They may be the same, or a long double may be larger. Larger meaning that it can hold greater (and smaller) values and with higher precision. – Variable Length Coder Jan 08 '13 at 18:28
  • The difference is that any type with long is more precise and has a greater range then the type itself without long because it uses more bytes. – Kevin Jan 08 '13 at 18:29
  • 7
    @Kevin Not entirely true - it *may* be more precise and/or have greater range, but that is not guaranteed. There have been and/or are platforms/compilers where `long int == int`, for example... – twalberg Jan 08 '13 at 18:40

3 Answers3

60

To quote the C++ standard, §3.9.1 ¶8:

There are three floating point types: float, double, and long double. The type double provides at least as much precision as float, and the type long double provides at least as much precision as double. The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double. The value representation of floating-point types is implementation-defined. Integral and floating types are collectively called arithmetic types. Specializations of the standard template std::numeric_limits (18.3) shall specify the maximum and minimum values of each arithmetic type for an implementation.

That is to say that double takes at least as much memory for its representation as float and long double at least as much as double. That extra memory is used for more precise representation of a number.

On x86 systems, float is typically 4 bytes long and can store numbers as large as about 3×10³⁸ and about as small as 1.4×10⁻⁴⁵. It is an IEEE 754 single-precision number that stores about 7 decimal digits of a fractional number.

Also on x86 systems, double is 8 bytes long and can store numbers in the IEEE 754 double-precision format, which has a much larger range and stores numbers with more precision, about 15 decimal digits. On some other platforms, double may not be 8 bytes long and may indeed be the same as a single-precision float.

The standard only requires that long double is at least as precise as double, so some compilers will simply treat long double as if it is the same as double. But, on most x86 chips, the 10-byte extended precision format 80-bit number is available through the CPU's floating-point unit, which provides even more precision than 64-bit double, with about 21 decimal digits of precision.

Some compilers instead support a 16-byte (128-bit) IEEE 754 quadruple precision number format with yet more precise representations and a larger range.

greyfade
  • 24,948
  • 7
  • 64
  • 80
  • 2
    So in practice, MSVC++ 2017, as of 2021, still has `sizeof(long double)` evaluating to the same value as `sizeof(double)`: 8 bytes, regardless of 32bit or 64bit. ( https://learn.microsoft.com/en-us/previous-versions/9cx8xs15(v=vs.140)?redirectedfrom=MSDN ) – Mike Weir Mar 25 '21 at 06:05
  • GCC and Clang both give 16 bytes for `long double` on Linux, and GCC on ARM gives 8 bytes. Yeah, still very much platform-dependent. – greyfade Apr 01 '21 at 00:28
19

It depends on your compiler but the following code can show you the number of bytes that each type requires:

int main() { 
    printf("%d\n", sizeof(double)); // some compilers print 8
    printf("%d\n", sizeof(long double)); // some compilers print 16
    return 0;
}
DavidRR
  • 18,291
  • 25
  • 109
  • 191
hmatar
  • 2,437
  • 2
  • 17
  • 27
10

A long <type> data type may hold larger values then a <type> data type, depending on the compiler.

alk
  • 69,737
  • 10
  • 105
  • 255