division... strange behaviour i dont understand

Discussion in 'C++' started by clqrq@yahoo.de, Jul 10, 2006.

  1. Guest

    can please someone explain why i get the message-box?
    im damn shure 500 divided by 300 doesnt equal to zero ;-\

    __________________________________
    unsigned short uiNumbers = 500;
    unsigned float ufFactor = 300.0;
    ufFactor = ufFactor / uiNumbers;
    if (ufFactor == 0) AfxMessageBox("0");
    , Jul 10, 2006
    #1
    1. Advertising

  2. Ian Collins Guest

    wrote:
    > can please someone explain why i get the message-box?
    > im damn shure 500 divided by 300 doesnt equal to zero ;-\
    >
    > __________________________________
    > unsigned short uiNumbers = 500;
    > unsigned float ufFactor = 300.0;


    What's an unsigned float?

    --
    Ian Collins.
    Ian Collins, Jul 10, 2006
    #2
    1. Advertising

  3. Ian Collins wrote:
    > wrote:
    > > can please someone explain why i get the message-box?
    > > im damn shure 500 divided by 300 doesnt equal to zero ;-\
    > >
    > > __________________________________
    > > unsigned short uiNumbers = 500;
    > > unsigned float ufFactor = 300.0;

    >
    > What's an unsigned float?
    >
    > --
    > Ian Collins.


    yes, unsigned float that is not compiling on my compiler.

    > > im damn shure 500 divided by 300 doesnt equal to zero ;-\


    > ufFactor = ufFactor / uiNumbers;


    any way, that's not 500 / 300.

    it is 300 / 500. Should lead to 0.6 if ufFactor is float.
    and 0 if it is integral.

    -- Murali Krishna
    Murali Krishna, Jul 10, 2006
    #3
  4. Zara Guest

    On 9 Jul 2006 21:32:18 -0700, wrote:

    >can please someone explain why i get the message-box?
    >im damn shure 500 divided by 300 doesnt equal to zero ;-\
    >
    >__________________________________
    >unsigned short uiNumbers = 500;
    >unsigned float ufFactor = 300.0;
    >ufFactor = ufFactor / uiNumbers;
    >if (ufFactor == 0) AfxMessageBox("0");


    unsigned float?

    Anyway, it seems thta ufFactor (which should have the value 300.0/500)
    is converted to int (thus, rounded to 0) before being compared.

    Try :

    if (ufFactor == 0.0) AfxMessageBox("0");

    This is an tricky comparison, as comparison with doubles (and floats)
    is subject to lots of problems due to rounding. You should take a look
    at this list, siblig list comp.lang.c++.moderated and cousin lists
    comp.lang.c and comp.lang.c.moderated about floating point comparisons

    Regards,

    Zara
    Zara, Jul 10, 2006
    #4
  5. Guest

    wrote:
    > can please someone explain why i get the message-box?
    > im damn shure 500 divided by 300 doesnt equal to zero ;-\
    >
    > __________________________________
    > unsigned short uiNumbers = 500;
    > unsigned float ufFactor = 300.0;
    > ufFactor = ufFactor / uiNumbers;
    > if (ufFactor == 0) AfxMessageBox("0");


    Because unsigned float is not a defined type in C++, the ufFactor will
    then be automatically converted to unsigned int.

    P.S. Any decent compiler should be able to spot the problem and give
    some warnings.
    , Jul 10, 2006
    #5
  6. Ian Collins Guest

    wrote:
    > wrote:
    >
    >>can please someone explain why i get the message-box?
    >>im damn shure 500 divided by 300 doesnt equal to zero ;-\
    >>
    >>__________________________________
    >>unsigned short uiNumbers = 500;
    >>unsigned float ufFactor = 300.0;
    >>ufFactor = ufFactor / uiNumbers;
    >>if (ufFactor == 0) AfxMessageBox("0");

    >
    >
    > Because unsigned float is not a defined type in C++, the ufFactor will
    > then be automatically converted to unsigned int.
    >

    No, it's just plain wrong and should fail to compile.

    --
    Ian Collins.
    Ian Collins, Jul 10, 2006
    #6
  7. Pete Becker Guest

    Zara wrote:
    >
    > unsigned float?
    >
    > Anyway, it seems thta ufFactor (which should have the value 300.0/500)
    > is converted to int (thus, rounded to 0) before being compared.
    >


    If ufFactor is really some floating point-type, then 0 would be
    converted to a floating-point type, not the other way around.


    > Try :
    >
    > if (ufFactor == 0.0) AfxMessageBox("0");
    >
    > This is an tricky comparison, as comparison with doubles (and floats)
    > is subject to lots of problems due to rounding.


    There's nothing tricky involved in checking whether a value is 0, and
    there are no floating-poitn roundoff issues in this code. Most likely,
    "unsigned float" was actually "unsigned int".
    Pete Becker, Jul 10, 2006
    #7
  8. Jim Langston Guest

    <> wrote in message
    news:...
    > can please someone explain why i get the message-box?
    > im damn shure 500 divided by 300 doesnt equal to zero ;-\


    No, but 300 divided by 500 is 0 in integer math.

    > _________________________________
    > unsigned short uiNumbers = 500;
    > unsigned float ufFactor = 300.0;
    > ufFactor = ufFactor / uiNumbers;
    > if (ufFactor == 0) AfxMessageBox("0");
    Jim Langston, Jul 12, 2006
    #8
  9. Jim Langston wrote:
    > <> wrote in message
    > news:...
    >> can please someone explain why i get the message-box?
    >> im damn shure 500 divided by 300 doesnt equal to zero ;-\

    >
    > No, but 300 divided by 500 is 0 in integer math.
    >
    >> _________________________________
    >> unsigned short uiNumbers = 500;
    >> unsigned float ufFactor = 300.0;


    WTF is "unsigned float"?

    >> ufFactor = ufFactor / uiNumbers;
    >> if (ufFactor == 0) AfxMessageBox("0");


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 12, 2006
    #9
  10. Jim Langston Guest

    "Victor Bazarov" <> wrote in message
    news:e93a00$apu$...
    > Jim Langston wrote:
    >> <> wrote in message
    >> news:...
    >>> can please someone explain why i get the message-box?
    >>> im damn shure 500 divided by 300 doesnt equal to zero ;-\

    >>
    >> No, but 300 divided by 500 is 0 in integer math.
    >>
    >>> _________________________________
    >>> unsigned short uiNumbers = 500;
    >>> unsigned float ufFactor = 300.0;

    >
    > WTF is "unsigned float"?


    A syntax error in his code which has already been discussed.
    Jim Langston, Jul 12, 2006
    #10
  11. Jim Langston wrote:
    > "Victor Bazarov" <> wrote in message
    > news:e93a00$apu$...
    >> Jim Langston wrote:
    >>> <> wrote in message
    >>> news:...
    >>>> can please someone explain why i get the message-box?
    >>>> im damn shure 500 divided by 300 doesnt equal to zero ;-\
    >>>
    >>> No, but 300 divided by 500 is 0 in integer math.
    >>>
    >>>> _________________________________
    >>>> unsigned short uiNumbers = 500;
    >>>> unsigned float ufFactor = 300.0;

    >>
    >> WTF is "unsigned float"?

    >
    > A syntax error in his code which has already been discussed.


    Ah... Thanks. Sorry to butt in at the middle of the thread.
    Victor Bazarov, Jul 12, 2006
    #11
    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. Mark Goldin

    Dont understand why this does not work

    Mark Goldin, Sep 7, 2004, in forum: ASP .Net
    Replies:
    11
    Views:
    568
    Greg Burns
    Sep 8, 2004
  2. Harri
    Replies:
    2
    Views:
    482
    Ryan Stewart
    Dec 24, 2004
  3. Alexandre
    Replies:
    4
    Views:
    470
    Lasse Reichstein Nielsen
    Apr 12, 2005
  4. Simon Mansfield
    Replies:
    10
    Views:
    572
    rossum
    Dec 16, 2004
  5. Replies:
    94
    Views:
    4,404
    ┬Ča\\/b
    Feb 9, 2007
Loading...

Share This Page