sizeof double

Discussion in 'C++' started by Yoonsik Oh, Dec 10, 2008.

  1. Yoonsik Oh

    Yoonsik Oh Guest

    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.
     
    Yoonsik Oh, Dec 10, 2008
    #1
    1. Advertising

  2. Yoonsik Oh

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Dec 10, 2008
    #2
    1. Advertising

  3. Yoonsik Oh

    SG Guest

    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
     
    SG, Dec 10, 2008
    #3
  4. Yoonsik Oh

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Dec 10, 2008
    #4
  5. Yoonsik Oh

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Dec 10, 2008
    #5
  6. Yoonsik Oh

    Guest

    On Dec 10, 5:00 am, Kai-Uwe Bux <> 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.
     
    , Dec 10, 2008
    #6
  7. Yoonsik Oh

    Rolf Magnus Guest

    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.
     
    Rolf Magnus, Dec 10, 2008
    #7
  8. Yoonsik Oh

    Guest

    On Dec 10, 5:57 pm, Rolf Magnus <> wrote:
    > 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)?
     
    , Dec 11, 2008
    #8
  9. Yoonsik Oh

    Guest

    On Dec 10, 9:43 pm, ""
    <> wrote:
    > On Dec 10, 5:57 pm, Rolf Magnus <> wrote:
    >
    >
    >
    >
    >
    > > 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.
     
    , Dec 11, 2008
    #9
  10. Yoonsik Oh

    Rolf Magnus Guest

    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?
     
    Rolf Magnus, Dec 11, 2008
    #10
  11. Yoonsik Oh

    muli_treiber Guest

    Rolf Magnus wrote:
    > 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
     
    muli_treiber, Dec 11, 2008
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Derek
    Replies:
    7
    Views:
    24,416
    Ron Natalie
    Oct 14, 2004
  2. Sydex
    Replies:
    12
    Views:
    6,647
    Victor Bazarov
    Feb 17, 2005
  3. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    662
    CBFalconer
    Apr 10, 2004
  4. Vinu
    Replies:
    13
    Views:
    1,500
    Lawrence Kirby
    May 12, 2005
  5. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    579
    Joe Smith
    May 22, 2006
Loading...

Share This Page