0 converted to 0.0 for float variable?

Discussion in 'C Programming' started by vlsidesign, Nov 8, 2007.

  1. vlsidesign

    vlsidesign Guest

    Newbie question: 0 converted to 0.0 for float variable?

    If I have the following code,
    float myTotal;
    myTotal = 0;

    will C automatically add a decimal point and at least one zero to the
    right of the decimal place? I am expecting that 0 will become at least
    0.0 or maybe something with more zeros.
     
    vlsidesign, Nov 8, 2007
    #1
    1. Advertising

  2. vlsidesign wrote:
    > Newbie question: 0 converted to 0.0 for float variable?
    >
    > If I have the following code,
    > float myTotal;
    > myTotal = 0;
    >
    > will C automatically add a decimal point and at least one zero to the
    > right of the decimal place? I am expecting that 0 will become at least
    > 0.0 or maybe something with more zeros.
    >


    #include <stdio.h>

    inline void pf(int n, float x)
    {
    printf("%%9.%df %9.*f; ", n, n, x);
    printf("%%9.%dg %9.*g; ", n, n, x);
    printf("%%9.%de %9.*e\n", n, n, x);
    }

    int main(void)
    {
    float myTotal = 0;
    int i;
    for (i = 0; i < 4; i++)
    pf(i, myTotal);
    putchar('\n');
    myTotal = 1;
    for (i = 0; i < 4; i++)
    pf(i, myTotal);
    return 0;
    }


    %9.0f 0; %9.0g 0; %9.0e 0e+00
    %9.1f 0.0; %9.1g 0; %9.1e 0.0e+00
    %9.2f 0.00; %9.2g 0; %9.2e 0.00e+00
    %9.3f 0.000; %9.3g 0; %9.3e 0.000e+00

    %9.0f 1; %9.0g 1; %9.0e 1e+00
    %9.1f 1.0; %9.1g 1; %9.1e 1.0e+00
    %9.2f 1.00; %9.2g 1; %9.2e 1.00e+00
    %9.3f 1.000; %9.3g 1; %9.3e 1.000e+00
     
    Martin Ambuhl, Nov 8, 2007
    #2
    1. Advertising

  3. vlsidesign wrote:

    > Newbie question: 0 converted to 0.0 for float variable?
    >
    > If I have the following code,
    > float myTotal;
    > myTotal = 0;
    >
    > will C automatically add a decimal point and at least one zero to the
    > right of the decimal place? I am expecting that 0 will become at least
    > 0.0 or maybe something with more zeros.


    Almost good. 0 gets converted to (float)0, which indeed can be
    written as 0.0f, but that is not the same as adding extra zeros
    in the conversion. C works with values, that's all you need to know.
     
    Peter Pichler, Nov 8, 2007
    #3
  4. vlsidesign

    Ben Pfaff Guest

    vlsidesign <> writes:

    > If I have the following code,
    > float myTotal;
    > myTotal = 0;
    >
    > will C automatically add a decimal point and at least one zero to the
    > right of the decimal place? I am expecting that 0 will become at least
    > 0.0 or maybe something with more zeros.
    >


    The value 0 will be converted to type float for storage in
    myTotal, yes. Zero is a value that can be represented exactly as
    a floating-point number, so it's not really meaningful to try to
    count the "number of zeros" being stored.

    Floating-point numbers are probably represented like this in your
    computer:
    http://en.wikipedia.org/wiki/IEEE_754
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
     
    Ben Pfaff, Nov 8, 2007
    #4
  5. vlsidesign

    Eric Sosman Guest

    vlsidesign wrote On 11/08/07 15:54,:
    > Newbie question: 0 converted to 0.0 for float variable?
    >
    > If I have the following code,
    > float myTotal;
    > myTotal = 0;
    >
    > will C automatically add a decimal point and at least one zero to the
    > right of the decimal place? I am expecting that 0 will become at least
    > 0.0 or maybe something with more zeros.


    Strictly speaking, the statement involves a float
    variable and an int value. When you assign a value of
    one type to a variable of a different type, the value
    is converted to the type of the variable (if possible)
    as part of the process. So the statement says

    - obtain an int with the value zero
    - convert it to the equivalent float value
    - store that float value in myTotal

    In practice, most compilers will recognize that 0
    is not just an int but an int whose value does not change,
    and therefore they will know that the conversion from int
    to float will always produce the same result. So they'll
    take a shortcut and generate code to do

    - obtain a float with the value zero
    - store that float value in myTotal

    Note that I'm being very careful to talk about types
    (int, float) and values (zero), not about how the values
    are represented in C source. The description above does
    not talk about adding decimal points or adding zero digits
    to something in your source code; it talks about what the
    computer does with the values and variables that your
    source code defines. You need to know what the various
    code constructs mean -- 0 and 0.0 and .0f and 0L and 0u
    are all zeroes, but all different -- but you should think
    of the program as working with the values, not with the
    source code that produced them. You'll be happier that
    way, I promise.

    --
     
    Eric Sosman, Nov 8, 2007
    #5
  6. vlsidesign

    jaysome Guest

    On Thu, 08 Nov 2007 16:14:29 -0500, Martin Ambuhl
    <> wrote:

    >vlsidesign wrote:
    >> Newbie question: 0 converted to 0.0 for float variable?
    >>
    >> If I have the following code,
    >> float myTotal;
    >> myTotal = 0;
    >>
    >> will C automatically add a decimal point and at least one zero to the
    >> right of the decimal place? I am expecting that 0 will become at least
    >> 0.0 or maybe something with more zeros.
    >>

    >
    >#include <stdio.h>
    >
    >inline void pf(int n, float x)


    There should be an informal rule that states that code that uses the
    inline function specifier should also include at least a sprinkle of
    "//" comments :^)

    And anyone who uses the inline function specifier should insure that
    their compiler conforms to the current standard (in at least one
    specific area) by subjecting it to the following code (in a hosted
    environment):

    inline int main(void)
    {
    return 0;
    }

    Regards
    --
    jay
     
    jaysome, Nov 9, 2007
    #6
  7. vlsidesign

    jacob navia Guest

    jaysome wrote:
    > On Thu, 08 Nov 2007 16:14:29 -0500, Martin Ambuhl
    > <> wrote:
    >
    >> vlsidesign wrote:
    >>> Newbie question: 0 converted to 0.0 for float variable?
    >>>
    >>> If I have the following code,
    >>> float myTotal;
    >>> myTotal = 0;
    >>>
    >>> will C automatically add a decimal point and at least one zero to the
    >>> right of the decimal place? I am expecting that 0 will become at least
    >>> 0.0 or maybe something with more zeros.
    >>>

    >> #include <stdio.h>
    >>
    >> inline void pf(int n, float x)

    >
    > There should be an informal rule that states that code that uses the
    > inline function specifier should also include at least a sprinkle of
    > "//" comments :^)
    >
    > And anyone who uses the inline function specifier should insure that
    > their compiler conforms to the current standard (in at least one
    > specific area) by subjecting it to the following code (in a hosted
    > environment):
    >
    > inline int main(void)
    > {
    > return 0;
    > }
    >
    > Regards



    OOOOOPS!!!

    I just discovered I forgot that test.

    Thanks

    :)

    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique
    http://www.cs.virginia.edu/~lcc-win32
     
    jacob navia, Nov 9, 2007
    #7
    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. Andy
    Replies:
    7
    Views:
    6,296
    Roedy Green
    May 10, 2004
  2. sidewinder
    Replies:
    10
    Views:
    3,933
    Alexander Terekhov
    Jun 26, 2003
  3. bd
    Replies:
    0
    Views:
    651
  4. k3n3dy
    Replies:
    15
    Views:
    1,004
    dan2online
    Apr 20, 2006
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,610
    James Kanze
    Oct 8, 2009
Loading...

Share This Page