[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

sizeof double

Yoonsik Oh

12/10/2008 9:45:00 AM

Hi.

The size of double type in the standard C++ is to ensure that the 8-
byte?

Or, depending on the environment are different?

Thanks.
10 Answers

Kai-Uwe Bux

12/10/2008 10:07:00 AM

0

Yoonsik Oh wrote:

> The size of double type in the standard C++ is to ensure that the 8-
> byte?

No.

> Or, depending on the environment are different?

The standard leaves the size of a double unspecified. That allows different
compilers for the _same_ computer to use different sizes. In fact, it may
even depend on compiler options.


Best

Kai-Uwe Bux

SG

12/10/2008 10:12:00 AM

0

On 10 Dez., 10:44, Yoonsik Oh wrote:
> The size of double type in the standard C++ is to ensure
> that the 8-byte?
> Or, depending on the environment are different?

There is no guarantee. It is implementation-defined. The same is true
for all other types.

The only guarantee you have is
1 = sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
and
sizeof(float) <= sizeof(double) <= sizeof(long double)

But the standard library provides some tools to query many properties
of types. Checkout the the following two header files:

<climits>:
provides macros like CHAR_BIT and UINT_MAX, etc. Note: CHAR_BIT is
not guaranteed to be 8. But it is at LEAST 8. So, it's possible
that one some platform every integral type is 32 bit with
CHAR_BIT==32 && sizeof(int)==1

<limits>:
traits class "std::numeric_limits" for various built-in types
including numeric_limits<double>.

Cheers!
SG

Rolf Magnus

12/10/2008 10:16:00 AM

0

SG wrote:

> On 10 Dez., 10:44, Yoonsik Oh wrote:
>> The size of double type in the standard C++ is to ensure
>> that the 8-byte?
>> Or, depending on the environment are different?
>
> There is no guarantee. It is implementation-defined. The same is true
> for all other types.
>
> The only guarantee you have is
> 1 = sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
> and
> sizeof(float) <= sizeof(double) <= sizeof(long double)

Well, there are some more guarantees. For integer types, there is a minimum
range, for the floating point types, a minimum precision.

Kai-Uwe Bux

12/10/2008 11:00:00 AM

0

Rolf Magnus wrote:

> SG wrote:
>
>> On 10 Dez., 10:44, Yoonsik Oh wrote:
>>> The size of double type in the standard C++ is to ensure
>>> that the 8-byte?
>>> Or, depending on the environment are different?
>>
>> There is no guarantee. It is implementation-defined. The same is true
>> for all other types.
>>
>> The only guarantee you have is
>> 1 = sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
>> and
>> sizeof(float) <= sizeof(double) <= sizeof(long double)
>
> Well, there are some more guarantees. For integer types, there is a
> minimum range, for the floating point types, a minimum precision.

As far as I can tell, all guarantees about floating types are in terms of
their precision and range [3.9.1/8]. What is guaranteed is that every float
value is representable as a double and every double value is representable
as a long double. However

sizeof(float) <= sizeof(double) <= sizeof(long double)

appears to be _not_ guaranteed. At least, I didn't find it. (Of course, an
implementation going agains this rule would be stupidly wasteful.)


Best

Kai-Uwe Bux

robertwessel2@yahoo.com

12/10/2008 10:02:00 PM

0

On Dec 10, 5:00 am, Kai-Uwe Bux <jkherci...@gmx.net> wrote:
> Rolf Magnus wrote:
> > SG wrote:
>
> >> On 10 Dez., 10:44, Yoonsik Oh wrote:
> >>> The size of double type in the standard C++ is to ensure
> >>> that the 8-byte?
> >>> Or, depending on the environment are different?
>
> >> There is no guarantee. It is implementation-defined. The same is true
> >> for all other types.
>
> >> The only guarantee you have is
> >>   1 = sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)
> >> and
> >>   sizeof(float) <= sizeof(double) <= sizeof(long double)
>
> > Well, there are some more guarantees. For integer types, there is a
> > minimum range, for the floating point types, a minimum precision.
>
> As far as I can tell, all guarantees about floating types are in terms of
> their precision and range [3.9.1/8]. What is guaranteed is that every float
> value is representable as a double and every double value is representable
> as a long double. However
>
>   sizeof(float) <= sizeof(double) <= sizeof(long double)
>
> appears to be _not_ guaranteed. At least, I didn't find it. (Of course, an
> implementation going agains this rule would be stupidly wasteful.)


C99 6.2.5: "10: There are three real floating types, designated as
float, double, and long 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 minimum of ten decimal digits of precision and exponent range of
-37..+37 and the needed sign implies a minimum of about 41 bits for a
double.

Rolf Magnus

12/10/2008 11:57:00 PM

0

robertwessel2@yahoo.com wrote:

>> As far as I can tell, all guarantees about floating types are in terms of
>> their precision and range [3.9.1/8]. What is guaranteed is that every
>> float value is representable as a double and every double value is
>> representable as a long double. However
>>
>> sizeof(float) <= sizeof(double) <= sizeof(long double)
>>
>> appears to be _not_ guaranteed. At least, I didn't find it. (Of course,
>> an implementation going agains this rule would be stupidly wasteful.)
>
>
> C99 6.2.5: "10: There are three real floating types, designated as
> float, double, and long 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."

That only covers the precision and range, not the size.

robertwessel2@yahoo.com

12/11/2008 3:43:00 AM

0

On Dec 10, 5:57 pm, Rolf Magnus <ramag...@t-online.de> wrote:
> robertwess...@yahoo.com wrote:
> >> As far as I can tell, all guarantees about floating types are in terms of
> >> their precision and range [3.9.1/8]. What is guaranteed is that every
> >> float value is representable as a double and every double value is
> >> representable as a long double. However
>
> >> sizeof(float) <= sizeof(double) <= sizeof(long double)
>
> >> appears to be _not_ guaranteed. At least, I didn't find it. (Of course,
> >> an implementation going agains this rule would be stupidly wasteful.)
>
> > C99 6.2.5: "10: There are three real floating types, designated as
> > float, double, and long 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."
>
> That only covers the precision and range, not the size.- Hide quoted text -


Fair enough. Although I'm now not sure where sizeof(char)<=sizeof
(short)<=sizeof(int)<=sizeof(long) is required either. Could a
perverse implementation have conventional 32 bit ints (32 value bits
stored in 32 physical bits), and then provide 16 bit shorts with 48
pad bits (thus yielding a 64 bit object)?

robertwessel2@yahoo.com

12/11/2008 3:52:00 AM

0

On Dec 10, 9:43 pm, "robertwess...@yahoo.com"
<robertwess...@yahoo.com> wrote:
> On Dec 10, 5:57 pm, Rolf Magnus <ramag...@t-online.de> wrote:
>
>
>
>
>
> > robertwess...@yahoo.com wrote:
> > >> As far as I can tell, all guarantees about floating types are in terms of
> > >> their precision and range [3.9.1/8]. What is guaranteed is that every
> > >> float value is representable as a double and every double value is
> > >> representable as a long double. However
>
> > >> sizeof(float) <= sizeof(double) <= sizeof(long double)
>
> > >> appears to be _not_ guaranteed. At least, I didn't find it. (Of course,
> > >> an implementation going agains this rule would be stupidly wasteful.)
>
> > > C99 6.2.5: "10: There are three real floating types, designated as
> > > float, double, and long 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."
>
> > That only covers the precision and range, not the size.- Hide quoted text -
>
> Fair enough.  Although I'm now not sure where sizeof(char)<=sizeof
> (short)<=sizeof(int)<=sizeof(long) is required either.  Could a
> perverse implementation have conventional 32 bit ints (32 value bits
> stored in 32 physical bits), and then provide 16 bit shorts with 48
> pad bits (thus yielding a 64 bit object)?- Hide quoted text -


Obviously sizeof(char)<=sizeof(short/int/long) must be true since
sizeof(char) is 1, and everything has to be at least that long.

Rolf Magnus

12/11/2008 10:42:00 AM

0

robertwessel2@yahoo.com wrote:

>> Fair enough. Although I'm now not sure where sizeof(char)<=sizeof
>> (short)<=sizeof(int)<=sizeof(long) is required either. Could a
>> perverse implementation have conventional 32 bit ints (32 value bits
>> stored in 32 physical bits), and then provide 16 bit shorts with 48
>> pad bits (thus yielding a 64 bit object)?- Hide quoted text -
>
>
> Obviously sizeof(char)<=sizeof(short/int/long) must be true since
> sizeof(char) is 1, and everything has to be at least that long.

In C++98, I found this:

There are four signed integer types: â??signed charâ?, â??short intâ?, â??intâ?,
and â??long int.â? In this list, each type provides at least as much
storage as those preceding it in the list.

Now I'm wondering about the word "provides" here. That sounds to me like the
space that is used for the value, excluding any padding bits.
Maybe someone could look it up in C++03 and see if that was changed?

muli_treiber

12/11/2008 4:19:00 PM

0

Rolf Magnus wrote:
> robertwessel2@yahoo.com wrote:
>
>>> Fair enough. Although I'm now not sure where sizeof(char)<=sizeof
>>> (short)<=sizeof(int)<=sizeof(long) is required either. Could a
>>> perverse implementation have conventional 32 bit ints (32 value bits
>>> stored in 32 physical bits), and then provide 16 bit shorts with 48
>>> pad bits (thus yielding a 64 bit object)?- Hide quoted text -
>>
>> Obviously sizeof(char)<=sizeof(short/int/long) must be true since
>> sizeof(char) is 1, and everything has to be at least that long.
>
> In C++98, I found this:
>
> There are four signed integer types: â??signed charâ?, â??short intâ?, â??intâ?,
> and â??long int.â? In this list, each type provides at least as much
> storage as those preceding it in the list.
>
> Now I'm wondering about the word "provides" here. That sounds to me like the
> space that is used for the value, excluding any padding bits.
> Maybe someone could look it up in C++03 and see if that was changed?
>
same in c++03