sizeof double

Y

Yoonsik Oh

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

Kai-Uwe Bux

Yoonsik said:
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
 
S

SG

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
 
R

Rolf Magnus

SG said:
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.
 
K

Kai-Uwe Bux

Rolf said:
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
 
R

robertwessel2

Rolf said:
SG wrote:
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.
 
R

Rolf Magnus

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

robertwessel2

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)?
 
R

robertwessel2

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

Rolf Magnus

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?
 
M

muli_treiber

Rolf said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top