Thread: Float and double size

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    Float and double size

    I was reading K&R, in section 2.2 they mentioned that

    float is single precision floating point, and
    double is double precision floating point

    When I checked wiki, it says that "IEEE single-precision floating point computer numbering format, is a binary computing format that occupies 4 bytes (32 bits) in computer memory."

    But in the second last paragraph of section 2.2 in K&R, it says that size of float,double and long double is also implementation defined.

    How can that be possible because wiki says it always take 4 byte for single precision or 8 bytes for double precision.

    Please help

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    They mention that just in case some hardware vendors don't conform to the IEEE standards.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    The C standard and IEEE 754 are two totally different things. The C type "float" does not mean "IEEE 754 single-precision", nor C "double" mean "IEEE 754 double-precision", though the names do suggest a correlation. The C standard does not say that implementations must use the IEEE 754 floating point format. They generally do, but it is not a requirement. Thus, the C standard describes floating point types for C, regardless of the underlying implementation.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Quote Originally Posted by anduril462 View Post
    The C standard and IEEE 754 are two totally different things.
    Right!!
    Anybody who knows the standard.
    btw, the standard also says that
    FLT_MAX >= 1e+37
    DBL_MAX >= 1e+37.
    The standard never never never requires the sizeof float/double/long double.
    To define the bytes for an float/... its the job of the compiler writer. (with or without IEEE 754)
    Last edited by BillyTKid; 10-10-2011 at 10:11 AM.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Actually K&R says that float has single-precision, what I want to know is that whether single-precision is attached with IEEE or not, AFAIK single precision term always comes with IEEE.

    Please clarify.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by _arjun View Post
    Actually K&R says that float has single-precision, what I want to know is that whether single-precision is attached with IEEE or not, AFAIK single precision term always comes with IEEE.
    No, it is not attached, as I said before. If it were, K&R would say something like "A single-precision 'float' type in C must conform to the IEEE 754 standard for single-precision floating point." But it doesn't say that because they are not connected. The single/double precision in C just refers to relative size. The C99 standard (6.2.5p10) states that the set of values of type float is a subset of the set of values of type double, meaning that a double can represent every value a float can and possibly more. Similarly, a long double must hold everything a double can, and possibly more. It also says (Annex F) that an implementation may define __STDC_IEC_559__, and if it does, it must conform to IEC 60559, which is basically IEEE 754, but nothing about how implementations that do not define __STDC_IEC_559__ must represent floats.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Quote Originally Posted by anduril462 View Post
    No, it is not attached, as I said before. If it were, K&R would say something like "A single-precision 'float' type in C must conform to the IEEE 754 standard for single-precision floating point." But it doesn't say that because they are not connected. The single/double precision in C just refers to relative size. The C99 standard (6.2.5p10) states that the set of values of type float is a subset of the set of values of type double, meaning that a double can represent every value a float can and possibly more. Similarly, a long double must hold everything a double can, and possibly more. It also says (Annex F) that an implementation may define __STDC_IEC_559__, and if it does, it must conform to IEC 60559, which is basically IEEE 754, but nothing about how implementations that do not define __STDC_IEC_559__ must represent floats.
    Thank you so much for clarifying my doubt

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need this (float != double)
    By m.sudhakar in forum C Programming
    Replies: 4
    Last Post: 08-19-2006, 06:27 AM
  2. float vs Double
    By sughandh in forum C Programming
    Replies: 3
    Last Post: 07-16-2006, 04:26 AM
  3. double to float
    By jsbeckton in forum C Programming
    Replies: 2
    Last Post: 11-07-2005, 11:29 PM
  4. float; double; int
    By Lyanette in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2003, 12:04 PM
  5. Float,double,int
    By Bill 101 in forum C Programming
    Replies: 1
    Last Post: 11-06-2002, 02:09 PM

Tags for this Thread