Does "float" always occupy 32 bits

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chandanlinster

    Does "float" always occupy 32 bits

    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?

  • Skarmander

    #2
    Re: Does "float&quo t; always occupy 32 bits

    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.

    Comment

    • Philip Potter

      #3
      Re: Does "float&quo t; always occupy 32 bits

      "chandanlinster " <chandanlinster @gmail.comwrote in message
      news:1157379273 .635173.126280@ e3g2000cwe.goog legroups.com...
      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

      Comment

      • rramesh1@gmail.com

        #4
        Re: Does &quot;float&quo t; always occupy 32 bits

        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,o bjc,obj-c++,ada,treelan g
        --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=releas e 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 " <chandanlinster @gmail.comwrote in message
        news:1157379273 .635173.126280@ e3g2000cwe.goog legroups.com...
        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

        Comment

        • chandanlinster

          #5
          Re: Does &quot;float&quo t; always occupy 32 bits

          Thanks everybody for providing the solutions.

          Comment

          • Keith Thompson

            #6
            Re: Does &quot;float&quo t; always occupy 32 bits

            "chandanlinster " <chandanlinster @gmail.comwrite s:
            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_Keit h) kst-u@mib.org <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.

            Comment

            • Ben Pfaff

              #7
              Re: Does &quot;float&quo t; always occupy 32 bits

              Keith Thompson <kst-u@mib.orgwrites :
              "chandanlinster " <chandanlinster @gmail.comwrite s:
              >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

              Comment

              • Keith Thompson

                #8
                Re: Does &quot;float&quo t; always occupy 32 bits

                Ben Pfaff <blp@cs.stanfor d.eduwrites:
                Keith Thompson <kst-u@mib.orgwrites :
                >"chandanlinste r" <chandanlinster @gmail.comwrite s:
                >>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_Keit h) kst-u@mib.org <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.

                Comment

                • Ben Pfaff

                  #9
                  Re: Does &quot;float&quo t; always occupy 32 bits

                  Keith Thompson <kst-u@mib.orgwrites :
                  Ben Pfaff <blp@cs.stanfor d.eduwrites:
                  >Keith Thompson <kst-u@mib.orgwrites :
                  >>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

                  Comment

                  • Dik T. Winter

                    #10
                    Re: Does &quot;float&quo t; always occupy 32 bits

                    In article <lnu03n9qoq.fsf @nuthaus.mib.or gKeith Thompson <kst-u@mib.orgwrites :
                    "chandanlinster " <chandanlinster @gmail.comwrite s:
                    ....
                    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/

                    Comment

                    • Neil

                      #11
                      Re: Does &quot;float&quo t; always occupy 32 bits

                      Keith Thompson wrote:
                      "chandanlinster " <chandanlinster @gmail.comwrite s:
                      >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.

                      Comment

                      • Keith Thompson

                        #12
                        Re: Does &quot;float&quo t; always occupy 32 bits

                        Neil <NeilKurzm@worl dnet.att.netwri tes:
                        Keith Thompson wrote:
                        >"chandanlinste r" <chandanlinster @gmail.comwrite s:
                        >>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_Keit h) kst-u@mib.org <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.

                        Comment

                        • Malcolm

                          #13
                          Re: Does &quot;float&quo t; always occupy 32 bits




                          "Keith Thompson" <kst-u@mib.orgwrote in message
                          news:lnlkox7eo4 .fsf@nuthaus.mi b.org...
                          Neil <NeilKurzm@worl dnet.att.netwri tes:
                          >Keith Thompson wrote:
                          >>"chandanlinst er" <chandanlinster @gmail.comwrite s:
                          >>>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.
                          --

                          freeware games to download.



                          Comment

                          • Joe Wright

                            #14
                            Re: Does &quot;float&quo t; always occupy 32 bits

                            Keith Thompson wrote:
                            Neil <NeilKurzm@worl dnet.att.netwri tes:
                            [ 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 ---

                            Comment

                            • CBFalconer

                              #15
                              Re: Does &quot;float&quo t; always occupy 32 bits

                              Malcolm wrote:
                              "Keith Thompson" <kst-u@mib.orgwrote 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>


                              Comment

                              Working...