Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sizeof double

Reply
Thread Tools

sizeof double

 
 
Yoonsik Oh
Guest
Posts: n/a
 
      12-10-2008
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.
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      12-10-2008
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

 
Reply With Quote
 
 
 
 
SG
Guest
Posts: n/a
 
      12-10-2008
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
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      12-10-2008
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.

 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      12-10-2008
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
 
Reply With Quote
 
robertwessel2@yahoo.com
Guest
Posts: n/a
 
      12-10-2008
On Dec 10, 5:00*am, Kai-Uwe Bux <(E-Mail Removed)> 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.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      12-10-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.

 
Reply With Quote
 
robertwessel2@yahoo.com
Guest
Posts: n/a
 
      12-11-2008
On Dec 10, 5:57*pm, Rolf Magnus <(E-Mail Removed)> wrote:
> (E-Mail Removed) 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)?
 
Reply With Quote
 
robertwessel2@yahoo.com
Guest
Posts: n/a
 
      12-11-2008
On Dec 10, 9:43*pm, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> On Dec 10, 5:57*pm, Rolf Magnus <(E-Mail Removed)> wrote:
>
>
>
>
>
> > (E-Mail Removed) 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.
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      12-11-2008
(E-Mail Removed) 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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sizeof( int ) != sizeof( void * ) blufox C Programming 2 05-22-2006 03:25 PM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments