float

Discussion in 'C++' started by Gurikar, Jun 27, 2005.

  1. Gurikar

    Gurikar Guest

    Hello,
    Can any one tell me is the code below correct.

    #include<iostream.h>
    int main()
    {
    int i = 1;
    float f = i / 2;
    if(f)
    cout<<"HI";
    else
    cout<<"Hello";
    }

    Does the above correct. Initially i felt output should be
    unpredictable..Am i right..But when i tried in my compiler(VC7),,its
    printing Hello..Is this right...Why????
    Gurikar, Jun 27, 2005
    #1
    1. Advertising

  2. Gurikar

    Suman Guest

    Gurikar wrote:
    > Hello,
    > Can any one tell me is the code below correct.
    >
    > #include<iostream.h>

    /* get modern!Use #include <iostream> instead*/
    > int main()

    /*
    ** you would be better served using int main(void), if you are
    ** not going to use the arguments passed to main)
    */
    > {
    > int i = 1;
    > float f = i / 2;

    Here what goes on under the hood is: i i.e. integer 1 is
    divided by integer 2, which is zero, in integer arithmetic,
    and the result is then stored to a float f.
    > if(f)

    Here value of float is zero, so the if condition fails, and
    you get the else branch executed.

    However, you are really lucky, as relational operators on floats
    are not likely to give you correct result, when you do not take
    into account a small tolerance.This is because of the way
    floating point numbers(which are rarely exactly representable
    using a finite number of bits) are internally stored as a bit pattern.
    > cout<<"HI";
    > else
    > cout<<"Hello";

    /* int main(), expects you to give back an int */
    return 0;
    > }
    >
    > Does the above correct.

    Depends on what you mean by *correct*.
    >Initially i felt output should be
    > unpredictable..Am i right..But when i tried in my compiler(VC7),,its
    > printing Hello..Is this right...Why????

    If you want to print "Hello", use something like:

    float f = (float)i / 2; /* or */
    float f = i / 2.0f; /* or */
    float f = (float)i / 2.0f; /* or */

    and change the if (cond...) to
    if ( fabs(f) < EPSILON ) /* we are sufficiently close to
    zero(?)*/
    where EPSILON maybe
    const float EPSILON = 0.0000001f;
    HTH,
    Suman.
    Suman, Jun 27, 2005
    #2
    1. Advertising

  3. Gurikar

    Arne Claus Guest

    > #include<iostream.h>
    > int main()
    > {
    > int i = 1;
    > float f = i / 2;
    > if(f)
    > cout<<"HI";
    > else
    > cout<<"Hello";
    > }
    >
    > Does the above correct. Initially i felt output should be
    > unpredictable..Am i right..But when i tried in my compiler(VC7),,its
    > printing Hello..Is this right...Why????


    Well ... i is an int and 2 is an int, that makes i/2 an int with value
    0 (because 1/2 = 0.5 with .5 truncated).
    Now you assign 0 to float f, making f 0.
    After this you test if f is true (aka. sth. other than 0) and as f is 0
    (aka false) the output "Hello" is correct.

    Arne
    Arne Claus, Jun 27, 2005
    #3
  4. Gurikar

    Lionel B Guest

    Suman wrote:
    > Gurikar wrote:
    >> Hello,
    >> Can any one tell me is the code below correct.
    >>
    >> #include<iostream.h>

    > /* get modern!Use #include <iostream> instead*/
    >> int main()

    > /*
    > ** you would be better served using int main(void), if you are
    > ** not going to use the arguments passed to main)
    > */


    Yuk - disagree. "int main()" is visually explicit - "main" takes no parameters - while "int main(void)" to me hints at
    some mysterious parameter called "void".

    >> {
    >> int i = 1;
    >> float f = i / 2;

    > Here what goes on under the hood is: i i.e. integer 1 is
    > divided by integer 2, which is zero, in integer arithmetic,
    > and the result is then stored to a float f.
    >> if(f)

    > Here value of float is zero, so the if condition fails, and
    > you get the else branch executed.
    >
    > However, you are really lucky, as relational operators on floats
    > are not likely to give you correct result, when you do not take
    > into account a small tolerance.This is because of the way
    > floating point numbers(which are rarely exactly representable
    > using a finite number of bits) are internally stored as a bit pattern.


    It is true that "in general" absolute floating point comparisons are a Bad Thing; however, I suspect that comparison
    with zero is ok - in the sense of giving predictable results - provided that (as in the OP's case) the object(s) of the
    comparison has been *explicitly set to zero*, as opposed to being the result of a calculation. Thus:

    double x = 0.0;

    (x == 0.0)

    will predictably evaluate to true, while:

    double y = 3.456;
    double x = y-3.456;

    (x == 0.0)

    will be UB. Please take with a pinch of salt; I cannot quote any standard to back me up on this.

    >> cout<<"HI";
    >> else
    >> cout<<"Hello";

    > /* int main(), expects you to give back an int */
    > return 0;
    >> }
    >>
    >> Does the above correct.

    > Depends on what you mean by *correct*.
    >
    >> Initially i felt output should be unpredictable.


    To the OP: How do you know it isn't unpredictable? ;-)

    >>.Am i right..But when i tried in my compiler(VC7),,its
    >> printing Hello..Is this right...Why????

    >
    > If you want to print "Hello", use something like:
    >
    > float f = (float)i / 2; /* or */
    > float f = i / 2.0f; /* or */
    > float f = (float)i / 2.0f; /* or */


    (Yuk! C style casts).

    Nonsense. They all print "HI", since they all evaluate to 0.5f so that (f) is true.

    --
    Lionel B
    Lionel B, Jun 27, 2005
    #4
  5. Gurikar

    Suman Guest

    Lionel B wrote:
    > Suman wrote:
    > > Gurikar wrote:
    > >> Hello,
    > >> Can any one tell me is the code below correct.
    > >>
    > >> #include<iostream.h>

    > > /* get modern!Use #include <iostream> instead*/
    > >> int main()

    > > /*
    > > ** you would be better served using int main(void), if you are
    > > ** not going to use the arguments passed to main)
    > > */

    >
    > Yuk - disagree. "int main()" is visually explicit - "main" takes no parameters - while "int main(void)" to me hints at
    > some mysterious parameter called "void".
    >

    Sorry, void *does not* mean *some mysterious parameter*.It means, *no
    parameter*. *int main()* is plain bad, it tells you nothing, only that
    main takes unspecified parameters.

    [snipped...]

    > > float f = (float)i / 2; /* or */
    > > float f = i / 2.0f; /* or */
    > > float f = (float)i / 2.0f; /* or */

    >
    > (Yuk! C style casts).
    >

    But they do the job, and it might just be easier to understand than
    static_cast<typename> operator.N'est-ce pas?
    > Nonsense. They all print "HI", since they all evaluate to 0.5f so that (f) is true.

    Yep! I got the HI and Hello wrong.

    Regards,
    Suman.
    Suman, Jun 27, 2005
    #5
  6. Gurikar

    msalters Guest

    Suman schreef:

    > Sorry, void *does not* mean *some mysterious parameter*.It means, *no
    > parameter*. *int main()* is plain bad, it tells you nothing, only that
    > main takes unspecified parameters.


    Look at the group. You're confusing C and C++. In C++, () means no
    parameters and (void) is a deprecated old form that will be removed
    (it's there to keep old C code compiling).

    There is *no* way to specify in C++ that a function takes unspecified
    parameters (not even ...)

    Regards,
    Michiel Salters
    msalters, Jun 27, 2005
    #6
  7. Gurikar

    Guest


    > > Yuk - disagree. "int main()" is visually explicit - "main" takes no parameters - while "int main(void)" to me hints at
    > > some mysterious parameter called "void".
    > >

    > Sorry, void *does not* mean *some mysterious parameter*.It means, *no
    > parameter*. *int main()* is plain bad, it tells you nothing, only that
    > main takes unspecified parameters.


    You are confusing C and C++.
    In C you would be correct but this is not the case in C++.
    Just try the following:
    int Foo()
    {
    return 0;
    }

    int main()
    {
    Foo(); // OK
    Foo(1) // Compiler error
    return 0;
    }

    The compiler should complain in the form of: Function does not take 1
    argument
    even though I just followed what you claimed that it would take
    unspecified parameters.
    Oh and if that is not enough I tried it also with a recursive call to
    main(), same result.
    , Jun 27, 2005
    #7
  8. Gurikar

    Geo Guest

    Suman wrote:
    > Lionel B wrote:
    > > Suman wrote:
    > > > Gurikar wrote:
    > > >> Hello,
    > > >> Can any one tell me is the code below correct.
    > > >>
    > > >> #include<iostream.h>
    > > > /* get modern!Use #include <iostream> instead*/
    > > >> int main()
    > > > /*
    > > > ** you would be better served using int main(void), if you are
    > > > ** not going to use the arguments passed to main)
    > > > */

    > >
    > > Yuk - disagree. "int main()" is visually explicit - "main" takes no parameters - while "int main(void)" to me hints at
    > > some mysterious parameter called "void".
    > >

    > Sorry, void *does not* mean *some mysterious parameter*.It means, *no
    > parameter*. *int main()* is plain bad, it tells you nothing, only that
    > main takes unspecified parameters.


    No it doesn't, this is C++, a function with an empty parameter list
    takes NO arguments, you are confused by a different language called C
    !!!!
    Geo, Jun 27, 2005
    #8
  9. Gurikar

    Lionel B Guest

    Suman wrote:
    > Lionel B wrote:
    >> Suman wrote:
    >>> Gurikar wrote:
    >>>> Hello,
    >>>> Can any one tell me is the code below correct.
    >>>>
    >>>> #include<iostream.h>
    >>> /* get modern!Use #include <iostream> instead*/
    >>>> int main()
    >>> /*
    >>> ** you would be better served using int main(void), if you are
    >>> ** not going to use the arguments passed to main)
    >>> */

    >>
    >> Yuk - disagree. "int main()" is visually explicit - "main" takes no
    >> parameters - while "int main(void)" to me hints at some mysterious
    >> parameter called "void".
    >>

    > Sorry, void *does not* mean *some mysterious parameter*.


    What part of "hints at" did you not understand?

    > It means, *no parameter*.


    .... in the C programming language.

    > *int main()* is plain bad, it tells you nothing, only that
    > main takes unspecified parameters.


    Wrong. It means no parameters and is standard C++.

    > [snipped...]
    >
    >>> float f = (float)i / 2; /* or */
    >>> float f = i / 2.0f; /* or */
    >>> float f = (float)i / 2.0f; /* or */

    >>
    >> (Yuk! C style casts).
    >>

    > But they do the job, and it might just be easier to understand than
    > static_cast<typename> operator.N'est-ce pas?


    Easier for whom? C programmers? Anyway, aren't you the one who admonished the OP to "get modern"...!?

    --
    Lionel B
    Lionel B, Jun 27, 2005
    #9
  10. Gurikar

    Suman Guest

    Lionel B wrote:
    > Suman wrote:
    > > Lionel B wrote:
    > >> Suman wrote:
    > >>> Gurikar wrote:
    > >>>> Hello,
    > >>>> Can any one tell me is the code below correct.
    > >>>>
    > >>>> #include<iostream.h>
    > >>> /* get modern!Use #include <iostream> instead*/
    > >>>> int main()
    > >>> /*
    > >>> ** you would be better served using int main(void), if you are
    > >>> ** not going to use the arguments passed to main)
    > >>> */
    > >>
    > >> Yuk - disagree. "int main()" is visually explicit - "main" takes no
    > >> parameters - while "int main(void)" to me hints at some mysterious
    > >> parameter called "void".
    > >>

    > > Sorry, void *does not* mean *some mysterious parameter*.

    >
    > What part of "hints at" did you not understand?
    >
    > > It means, *no parameter*.

    >
    > ... in the C programming language.
    >
    > > *int main()* is plain bad, it tells you nothing, only that
    > > main takes unspecified parameters.

    >
    > Wrong. It means no parameters and is standard C++.
    >
    > > [snipped...]
    > >
    > >>> float f = (float)i / 2; /* or */
    > >>> float f = i / 2.0f; /* or */
    > >>> float f = (float)i / 2.0f; /* or */
    > >>
    > >> (Yuk! C style casts).
    > >>

    > > But they do the job, and it might just be easier to understand than
    > > static_cast<typename> operator.N'est-ce pas?

    >
    > Easier for whom? C programmers? Anyway, aren't you the one who admonished the OP to "get modern"...!?
    >

    Point well taken, post refreseher! Thanks.
    Suman, Jun 27, 2005
    #10
  11. Gurikar

    Andre Kostur Guest

    "msalters" <> wrote in
    news::

    >
    >
    > Suman schreef:
    >
    >> Sorry, void *does not* mean *some mysterious parameter*.It means, *no
    >> parameter*. *int main()* is plain bad, it tells you nothing, only that
    >> main takes unspecified parameters.

    >
    > Look at the group. You're confusing C and C++. In C++, () means no
    > parameters and (void) is a deprecated old form that will be removed
    > (it's there to keep old C code compiling).
    >
    > There is *no* way to specify in C++ that a function takes unspecified
    > parameters (not even ...)


    Huh? Ellipsis is still legal in C++... you just can't pass objects through
    it.
    Andre Kostur, Jun 27, 2005
    #11
  12. Gurikar

    Jack Klein Guest

    On 27 Jun 2005 05:20:30 -0700, "Suman" <> wrote in
    comp.lang.c++:

    >
    >
    > Lionel B wrote:
    > > Suman wrote:
    > > > Gurikar wrote:
    > > >> Hello,
    > > >> Can any one tell me is the code below correct.
    > > >>
    > > >> #include<iostream.h>
    > > > /* get modern!Use #include <iostream> instead*/
    > > >> int main()
    > > > /*
    > > > ** you would be better served using int main(void), if you are
    > > > ** not going to use the arguments passed to main)
    > > > */

    > >
    > > Yuk - disagree. "int main()" is visually explicit - "main" takes no parameters - while "int main(void)" to me hints at
    > > some mysterious parameter called "void".
    > >

    > Sorry, void *does not* mean *some mysterious parameter*.It means, *no
    > parameter*. *int main()* is plain bad, it tells you nothing, only that
    > main takes unspecified parameters.


    In both C and C++, empty parentheses in the DEFINITION of a function
    indicate that it takes no parameters. There is absolutely no
    ambiguity in either language.

    In C, this definition would provide a declaration, but not a
    prototype, for the defined function.

    C++ does not actually have the concept of a function prototype. The
    word 'prototype' is not defined in the C++ standard, although it does
    slip in a few times, mostly in defining the concept of what C calls
    prototype scope.

    In C++, there is no way to declare a function accepts unspecified
    parameters. Empty parentheses and (void) are exactly equivalent in
    C++ function declarations.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Jun 28, 2005
    #12
    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,255
    Roedy Green
    May 10, 2004
  2. Jim West
    Replies:
    4
    Views:
    1,434
    Jeff Schwab
    Jan 16, 2004
  3. Jukka Lehtonen

    operator== (float, float)

    Jukka Lehtonen, Aug 4, 2004, in forum: C++
    Replies:
    5
    Views:
    2,424
    Jukka Lehtonen
    Aug 5, 2004
  4. bd
    Replies:
    0
    Views:
    624
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,540
    James Kanze
    Oct 8, 2009
Loading...

Share This Page