Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

long double vs double

I know that size of various data types can change depending on which system I am on.

I use XP 32bits, and using the sizeof() operator in C++, it seems like long double is 12 bytes, and double is 8.

However, most major sources states that long double is 8 bytes, and the range is therefore the same as a double.

How come I have 12 bytes? If long double is indeed 12 bytes, doesn't this extends the range of value also? Or the long signature is only used (the compiler figures) when the value exceed the range of a double, and thus, extends beyond 8 bytes?

like image 701
CppLearner Avatar asked Aug 11 '10 00:08

CppLearner


People also ask

What is the meaning of long double?

In C and related programming languages, long double refers to a floating-point data type that is often more precise than double precision though the language standard only requires it to be at least as precise as double . As with C's other floating-point types, it may not necessarily map to an IEEE format.

Is a long or a double bigger?

In an article on MSDN, it states that the double data type has a range of "-1.79769313486232e308 .. 1.79769313486232e308". Whereas the long data type only has a range of "-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807".

What is the difference between short long double long double?

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. 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.

Is long double 12 or 16 bytes?

However, most major sources states that long double is 8 bytes, and the range is therefore the same as a double .


1 Answers

Quoting from Wikipedia:

On the x86 architecture, most compilers implement long double as the 80-bit extended precision type supported by that hardware (sometimes stored as 12 or 16 bytes to maintain data structure .

and

Compilers may also use long double for a 128-bit quadruple precision format, which is currently implemented in software.

In other words, yes, a long double may be able to store a larger range of values than a double. But it's completely up to the compiler.

like image 139
Borealid Avatar answered Sep 22 '22 14:09

Borealid