Does "float" always occupy 32 bits

Discussion in 'C Programming' started by chandanlinster, Sep 4, 2006.

  1. As far as I know floating point variables, that are declared as float
    follow IEEE format representation (which is 32-bit in size). But
    chapter1-page no 9 of the book "The C programming language" states that
    "THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
    USING".
    Does this mean "float" variables have different sizes on different
    machines?
    chandanlinster, Sep 4, 2006
    #1
    1. Advertising

  2. chandanlinster

    Skarmander Guest

    chandanlinster wrote:
    > As far as I know floating point variables, that are declared as float
    > follow IEEE format representation (which is 32-bit in size). But
    > chapter1-page no 9 of the book "The C programming language" states that
    > "THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
    > USING".
    > Does this mean "float" variables have different sizes on different
    > machines?
    >

    Yes.

    Although the IEEE single-precision type is the most common implementation of
    float, this is not required. The standard only requires a minimum range and
    minimum precision. Implementations can optionally signal that they implement
    IEC 60559, a revision of IEEE 754.

    In practice, most programs do not depend on the particular size of a float;
    the ones that assume are usually those who also assume an int is 32 bits, so
    floats and ints can be stored interchangeably. Needless to say, these are
    not portable assumptions, and rarely appropriate, let alone necessary.

    S.
    Skarmander, Sep 4, 2006
    #2
    1. Advertising

  3. "chandanlinster" <> wrote in message
    news:...
    > As far as I know floating point variables, that are declared as float
    > follow IEEE format representation (which is 32-bit in size).


    This is not guaranteed.

    > But
    > chapter1-page no 9 of the book "The C programming language" states that
    > "THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
    > USING".
    > Does this mean "float" variables have different sizes on different
    > machines?


    Yes.

    Philip
    Philip Potter, Sep 4, 2006
    #3
  4. chandanlinster

    Guest

    Hi Guys.

    I have a AMD-64 turion running debian Linux with gcc & here is what I
    got..

    char:1
    short:2
    long:8
    float:4
    double:8
    long double:16

    Here are my compiler options..

    Using built-in specs.
    Target: x86_64-linux-gnu
    Configured with: ../src/configure -v
    --enable-languages=c,c++,java,fortran,objc,obj-c++,ada,treelang
    --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib
    --without-included-gettext --enable-threads=posix --enable-nls
    --program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu
    --enable-libstdcxx-debug --enable-java-awt=gtk --enable-gtk-cairo
    --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-4.1-1.4.2.0/jre
    --enable-mpfr --enable-checking=release x86_64-linux-gnu
    Thread model: posix
    gcc version 4.1.2 20060613 (prerelease) (Debian 4.1.1-5)

    Cheers
    /R

    Philip Potter wrote:
    > "chandanlinster" <> wrote in message
    > news:...
    > > As far as I know floating point variables, that are declared as float
    > > follow IEEE format representation (which is 32-bit in size).

    >
    > This is not guaranteed.
    >
    > > But
    > > chapter1-page no 9 of the book "The C programming language" states that
    > > "THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
    > > USING".
    > > Does this mean "float" variables have different sizes on different
    > > machines?

    >
    > Yes.
    >
    > Philip
    , Sep 4, 2006
    #4
  5. Thanks everybody for providing the solutions.
    chandanlinster, Sep 4, 2006
    #5
  6. "chandanlinster" <> writes:
    > As far as I know floating point variables, that are declared as float
    > follow IEEE format representation (which is 32-bit in size). But
    > chapter1-page no 9 of the book "The C programming language" states that
    > "THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
    > USING".
    > Does this mean "float" variables have different sizes on different
    > machines?


    Potentially, yes.

    I've never heard of a C implementation where float is not 32 bits
    (i.e., where sizeof(float) * CHAR_BIT != 32), but such implementations
    could easily exist.

    In any case, there's no real reason or need to assume that float is 32
    bits. The compiler knows how big a float is so you don't need to
    worry about it.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 5, 2006
    #6
  7. chandanlinster

    Ben Pfaff Guest

    Keith Thompson <> writes:

    > "chandanlinster" <> writes:
    >> As far as I know floating point variables, that are declared as float
    >> follow IEEE format representation (which is 32-bit in size). But
    >> chapter1-page no 9 of the book "The C programming language" states that
    >> "THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
    >> USING".
    >> Does this mean "float" variables have different sizes on different
    >> machines?

    >
    > Potentially, yes.
    >
    > I've never heard of a C implementation where float is not 32 bits
    > (i.e., where sizeof(float) * CHAR_BIT != 32), but such implementations
    > could easily exist.


    I don't think that float could be much smaller than 32 bits. By
    my calculations, a floating point number represented in the
    format that the Standard expects would need approximately 28 bits
    to have the required range and precision.
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
    Ben Pfaff, Sep 5, 2006
    #7
  8. Ben Pfaff <> writes:
    > Keith Thompson <> writes:
    >> "chandanlinster" <> writes:
    >>> As far as I know floating point variables, that are declared as float
    >>> follow IEEE format representation (which is 32-bit in size). But
    >>> chapter1-page no 9 of the book "The C programming language" states that
    >>> "THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
    >>> USING".
    >>> Does this mean "float" variables have different sizes on different
    >>> machines?

    >>
    >> Potentially, yes.
    >>
    >> I've never heard of a C implementation where float is not 32 bits
    >> (i.e., where sizeof(float) * CHAR_BIT != 32), but such implementations
    >> could easily exist.

    >
    > I don't think that float could be much smaller than 32 bits. By
    > my calculations, a floating point number represented in the
    > format that the Standard expects would need approximately 28 bits
    > to have the required range and precision.


    Sure, but float could easily be larger than 32 bits.

    And, in fact, contrary to what I wrote above, I've worked on machines
    where float is 64 bits (and double is 64 bits, and long double is 128
    bits). These were Cray vector machines, where the native word size is
    64 bits.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 5, 2006
    #8
  9. chandanlinster

    Ben Pfaff Guest

    Keith Thompson <> writes:

    > Ben Pfaff <> writes:
    >> Keith Thompson <> writes:
    >>> I've never heard of a C implementation where float is not 32 bits
    >>> (i.e., where sizeof(float) * CHAR_BIT != 32), but such implementations
    >>> could easily exist.

    >>
    >> I don't think that float could be much smaller than 32 bits. By
    >> my calculations, a floating point number represented in the
    >> format that the Standard expects would need approximately 28 bits
    >> to have the required range and precision.

    >
    > Sure, but float could easily be larger than 32 bits.


    Yes, I should have added that. I didn't mean to sound contradictory.

    > And, in fact, contrary to what I wrote above, I've worked on machines
    > where float is 64 bits (and double is 64 bits, and long double is 128
    > bits). These were Cray vector machines, where the native word size is
    > 64 bits.


    Right.
    --
    "I should killfile you where you stand, worthless human." --Kaz
    Ben Pfaff, Sep 5, 2006
    #9
  10. In article <> Keith Thompson <> writes:
    > "chandanlinster" <> writes:

    ....
    > > Does this mean "float" variables have different sizes on different
    > > machines?

    >
    > Potentially, yes.
    >
    > I've never heard of a C implementation where float is not 32 bits
    > (i.e., where sizeof(float) * CHAR_BIT != 32), but such implementations
    > could easily exist.


    I have used them. sizeof(float) == 8.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Sep 5, 2006
    #10
  11. chandanlinster

    Neil Guest

    Keith Thompson wrote:
    > "chandanlinster" <> writes:
    >> As far as I know floating point variables, that are declared as float
    >> follow IEEE format representation (which is 32-bit in size). But
    >> chapter1-page no 9 of the book "The C programming language" states that
    >> "THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
    >> USING".
    >> Does this mean "float" variables have different sizes on different
    >> machines?

    >
    > Potentially, yes.
    >
    > I've never heard of a C implementation where float is not 32 bits
    > (i.e., where sizeof(float) * CHAR_BIT != 32), but such implementations
    > could easily exist.
    >
    > In any case, there's no real reason or need to assume that float is 32
    > bits. The compiler knows how big a float is so you don't need to
    > worry about it.
    >


    The Hi-Tech compiler for the PIC gives you the option of 24 or 32 bit
    floats. I am not sure what ANSI says about that.
    Neil, Sep 6, 2006
    #11
  12. Neil <> writes:
    > Keith Thompson wrote:
    >> "chandanlinster" <> writes:
    >>> As far as I know floating point variables, that are declared as float
    >>> follow IEEE format representation (which is 32-bit in size). But
    >>> chapter1-page no 9 of the book "The C programming language" states that
    >>> "THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
    >>> USING".
    >>> Does this mean "float" variables have different sizes on different
    >>> machines?

    >> Potentially, yes.
    >> I've never heard of a C implementation where float is not 32 bits
    >> (i.e., where sizeof(float) * CHAR_BIT != 32), but such implementations
    >> could easily exist.
    >> In any case, there's no real reason or need to assume that float is
    >> 32
    >> bits. The compiler knows how big a float is so you don't need to
    >> worry about it.

    >
    > The Hi-Tech compiler for the PIC gives you the option of 24 or 32 bit
    > floats. I am not sure what ANSI says about that.


    It doesn't directly say anything, but I don't believe it's possible to
    meet the standard's requirements for type float in 24 bits. By my
    calculations you need at least 27 bits (1 sign bit, 20 mantissa bits,
    6 exponent bits).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 6, 2006
    #12
  13. chandanlinster

    Malcolm Guest

    "Keith Thompson" <> wrote in message
    news:...
    > Neil <> writes:
    >> Keith Thompson wrote:
    >>> "chandanlinster" <> writes:
    >>>> As far as I know floating point variables, that are declared as float
    >>>> follow IEEE format representation (which is 32-bit in size). But
    >>>> chapter1-page no 9 of the book "The C programming language" states that
    >>>> "THE RANGE OF BOTH int AND float DEPENDS ON THE MACHINE YOU ARE
    >>>> USING".
    >>>> Does this mean "float" variables have different sizes on different
    >>>> machines?
    >>> Potentially, yes.
    >>> I've never heard of a C implementation where float is not 32 bits
    >>> (i.e., where sizeof(float) * CHAR_BIT != 32), but such implementations
    >>> could easily exist.
    >>> In any case, there's no real reason or need to assume that float is
    >>> 32
    >>> bits. The compiler knows how big a float is so you don't need to
    >>> worry about it.

    >>
    >> The Hi-Tech compiler for the PIC gives you the option of 24 or 32 bit
    >> floats. I am not sure what ANSI says about that.

    >
    > It doesn't directly say anything, but I don't believe it's possible to
    > meet the standard's requirements for type float in 24 bits. By my
    > calculations you need at least 27 bits (1 sign bit, 20 mantissa bits,
    > 6 exponent bits).
    >

    Easy.Your 24 bits index into an array of double-precision 64 bit values.
    Since the user isn't guaranteed more than 64 K of memory, you've done it.
    --
    www.personal.leeds.ac.uk/~bgy1mm
    freeware games to download.
    Malcolm, Sep 6, 2006
    #13
  14. chandanlinster

    Joe Wright Guest

    Keith Thompson wrote:
    > Neil <> writes:

    [ snip ]
    >> The Hi-Tech compiler for the PIC gives you the option of 24 or 32 bit
    >> floats. I am not sure what ANSI says about that.

    >
    > It doesn't directly say anything, but I don't believe it's possible to
    > meet the standard's requirements for type float in 24 bits. By my
    > calculations you need at least 27 bits (1 sign bit, 20 mantissa bits,
    > 6 exponent bits).
    >


    Without regard to the Standard's requirements..

    IEEE 754 has an extra bit. Float on my Sun and x86 iron is 32 bits and
    includes a sign, 8 exponent bits and a 24-bit mantissa. They squeeze 33
    bits into 32 by assuming the high order bit of a normalized mantissa is
    always 1. This means we can use the bit position to hold the low order
    bit of the exponent.

    Same system for double, of course. Sign + 11 exponent bits + 53 mantissa
    bits == 65. It fits, of course, in 64 bits.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Sep 7, 2006
    #14
  15. chandanlinster

    CBFalconer Guest

    Malcolm wrote:
    > "Keith Thompson" <> wrote in message
    >

    .... snip ...
    >>
    >> It doesn't directly say anything, but I don't believe it's
    >> possible to meet the standard's requirements for type float in
    >> 24 bits. By my calculations you need at least 27 bits (1 sign
    >> bit, 20 mantissa bits, 6 exponent bits).

    >
    > Easy.Your 24 bits index into an array of double-precision 64 bit
    > values. Since the user isn't guaranteed more than 64 K of memory,
    > you've done it.


    Harumph. C passes by value. How do you pass those beasts by value?

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>
    CBFalconer, Sep 7, 2006
    #15
  16. CBFalconer <> writes:
    > Malcolm wrote:
    >> "Keith Thompson" <> wrote in message
    >>

    > ... snip ...
    >>>
    >>> It doesn't directly say anything, but I don't believe it's
    >>> possible to meet the standard's requirements for type float in
    >>> 24 bits. By my calculations you need at least 27 bits (1 sign
    >>> bit, 20 mantissa bits, 6 exponent bits).

    >>
    >> Easy.Your 24 bits index into an array of double-precision 64 bit
    >> values. Since the user isn't guaranteed more than 64 K of memory,
    >> you've done it.

    >
    > Harumph. C passes by value. How do you pass those beasts by value?


    They're only 24 bits; why would there be any problem passing them by
    value?

    So the idea is that a value of type "float" is a 24-bit index into a
    table of, say, 32-bit floating-point objects. There are up to 2**32
    possible values, but only 2**24 of them can exist within a single
    execution of a program. Any operation that produces a new (32-bit)
    value creates a new entry in the table and a new 24-bit index value.

    This is a very silly idea, but it could satisfy *most* of the required
    semantics of type float.

    One thing that you couldn't do with this is write a value of type
    "float" to a binary file, read it from another instance of the same
    program, and get the same value. Maybe a conforming freestanding
    implementation could get away this this. (Is there an embedded
    version of the DS9K?)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Sep 7, 2006
    #16
  17. chandanlinster

    David Wade Guest

    "Joe Wright" <> wrote in message
    news:...
    > Keith Thompson wrote:
    > > Neil <> writes:

    > [ snip ]
    > >> The Hi-Tech compiler for the PIC gives you the option of 24 or 32 bit
    > >> floats. I am not sure what ANSI says about that.

    > >
    > > It doesn't directly say anything, but I don't believe it's possible to
    > > meet the standard's requirements for type float in 24 bits. By my
    > > calculations you need at least 27 bits (1 sign bit, 20 mantissa bits,
    > > 6 exponent bits).
    > >

    >
    > Without regard to the Standard's requirements..
    >
    > IEEE 754 has an extra bit. Float on my Sun and x86 iron is 32 bits and
    > includes a sign, 8 exponent bits and a 24-bit mantissa. They squeeze 33
    > bits into 32 by assuming the high order bit of a normalized mantissa is
    > always 1. This means we can use the bit position to hold the low order
    > bit of the exponent.
    >
    > Same system for double, of course. Sign + 11 exponent bits + 53 mantissa
    > bits == 65. It fits, of course, in 64 bits.
    >
    > --
    > Joe Wright
    > "Everything should be made as simple as possible, but not simpler."
    > --- Albert Einstein ---


    Honeywell L66 has 36 bit floats.....
    David Wade, Sep 8, 2006
    #17
    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. pmastroianni

    Does a ctor/dtor occupy memory

    pmastroianni, Apr 1, 2005, in forum: C++
    Replies:
    1
    Views:
    351
    Howard
    Apr 1, 2005
  2. bd
    Replies:
    0
    Views:
    621
  3. R.Z.
    Replies:
    9
    Views:
    1,190
    Robbie Hatley
    Sep 21, 2005
  4. Replies:
    11
    Views:
    1,356
    Ben C
    Jun 14, 2007
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,539
    James Kanze
    Oct 8, 2009
Loading...

Share This Page