The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

A random assignment

Last post 03-28-2008 6:09 PM by mxsscott. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 03-28-2008 4:08 PM

    A random assignment

    I encountered the following line of (C++) code today, along with a few more like it:

          totalcost = rnd((totalcost += cost), 2);

     

    Now, as far as I can see, there's a few things wrong:

    1. The += is pointless - it should just be a +
    2. rnd() does not return a random number like in every other program/language I've seen. It rounds the 1st argument to the number of decimal places specified by the 2nd.
    3. totalcost and cost are double-precision floating point numbers. Ever rounded a floating point number and got the expected result? rnd() actually converts the double to a decimal, rounds it, then converts it back to a double.
    Fixing bugs in a VB program is like playing whack-a-mole.
  • 03-28-2008 4:30 PM In reply to

    Re: A random assignment

    mxsscott:
    The += is pointless - it should just be a +
    No, it's definitely very pointful. It tries to invoke undefined behaviour but fails because the function call creates a sequence point.
  • 03-28-2008 4:36 PM In reply to

    Re: A random assignment

    Ah indeed. I forgot to mention my quick research into sequence points after thinking undefined thoughts!

    Fixing bugs in a VB program is like playing whack-a-mole.
  • 03-28-2008 5:38 PM In reply to

    • alegr
    • Top 500 Contributor
    • Joined on 01-17-2008
    • Posts 91

    Re: A random assignment

    Vempele:
    mxsscott:
    The += is pointless - it should just be a +
    No, it's definitely very pointful. It tries to invoke undefined behaviour but fails because the function call creates a sequence point.

     

    It would suck, though, if rnd() were redefined as a macro...

  • 03-28-2008 6:09 PM In reply to

    Re: A random assignment

    alegr:

    Vempele:

    mxsscott:
    The += is pointless - it should just be a +
    No, it's definitely very pointful. It tries to invoke undefined behaviour but fails because the function call creates a sequence point.

     

    It would suck, though, if rnd() were redefined as a macro...

     

    (Un)fortunately rnd() is defined as a function in a file that gets passed through an Informix ESQL/C precompiler because it calls deccvdbl(), decround() then dectodbl() in the Informix client library. A macro sounds like too easy a way to break the calculation.

    Fixing bugs in a VB program is like playing whack-a-mole.
Page 1 of 1 (5 items)
Powered by Community Server (Non-Commercial Edition), by Telligent Systems