• John Hensley (unregistered) in reply to bleh
    Anonymous:
    Assignment is expensive, there is no point doing an assignment statement unless you need to.

    Very clever. But I dare you to paste the code into a program, compile to assembly with full optimization, and see if it isn't replaced with a direct assignment.

  • (cs) in reply to John Hensley
    Anonymous:
    Anonymous:
    Assignment is expensive, there is no point doing an assignment statement unless you need to.

    Very clever. But I dare you to paste the code into a program, compile to assembly with full optimization, and see if it isn't replaced with a direct assignment.



    That's as may be... but some prefer the transparency of wetware compiler optimization.

  • David Walker (unregistered) in reply to Merlin
    Anonymous:
    if (checkAmt == 0) 

    This one is all the more amazing that comparing a floating point number to exactly zero is usually a very bad idea.
    Strict equality and FP numbers don't mix. Usually.

    The rest is just icing on the cake...
     
    If this is the amount of a check, it's probably not a floating point number, but rather a fixed point number with (I hope) two decimal places.. depending on the language and the computer's available data representations.  A fixed point number can compare exactly to zero....
  • John Hensley (unregistered) in reply to Maurits
    Maurits:
    That's as may be... but some prefer the transparency of wetware compiler optimization.

    Now that's a whole 'nother WTF. What did Knuth say about optimization?

  • Merlin (unregistered) in reply to David Walker
    Anonymous:
    Anonymous:
    if (checkAmt == 0) 

    This one is all the more amazing that comparing a floating point number to exactly zero is usually a very bad idea.
    Strict equality and FP numbers don't mix. Usually.

    The rest is just icing on the cake...
     
    If this is the amount of a check, it's probably not a floating point number, but rather a fixed point number with (I hope) two decimal places.. depending on the language and the computer's available data representations.  A fixed point number can compare exactly to zero....


    You cared to realize that it was declared as the following?
    bool validateCheckAmount(float checkAmt)


    Now if you insist on finding a way out of this by claiming that this could be a fixed point number in spite of being declared a float, that would be an even bigger WTF. Maybe that was some kind of language where you can redefine base types? ;-) Maybe it would be clever then to redefine a string type as 'boolean'? Holy crap!

    And if the author of this piece of crap has found it clever to represent what we could think are amounts of money as floating point numbers, this is very, very frightening. Just imagine this is a banking software...
  • Stateless (unregistered) in reply to Donny

    And even that can be dicey if the result of GetState() can change between calls. The second GetState() call returns the state originally stored in nOldState - now update, or not?

  • Fred (unregistered)
    Alex Papadimoulis:

    if( nextIndex != currIndex )
    {
        nextIndex = currIndex;
    }



    I actually encountered some code like this in some avionics code I was reviewing and inquired about it. It was for debugging. They could set a trap on assignments to a variable and only wanted to trap when the value changed. So this is what they did.
  • (cs)

    In de.comp.lang.php.misc somebody asked for a better way to find the inverse of a boolean. His current implementation looks like:

    <?php
    switch($var) {
       case false:
         $var = true;
       break;
       case true:
         $var = false;
       break;
    }
    ?>


  • sicher (unregistered)

    A friend of mine did consulting work at a company a couple of years ago. In their codebase, he found this little piece of beauty:

    BOOL IsZero(float Number)
    {
        char String[256];
        sprintf(String,"%0.1f", Number);
        if( strcmp(String,"0.0") == 0)
           return TRUE;
        return FALSE;
    }

    /Mikael

  • (cs) in reply to sicher
    sicher:
    A friend of mine did consulting work at a company a couple of years ago. In their codebase, he found this little piece of beauty:

    BOOL IsZero(float Number)
    {
        char String[256];
        sprintf(String,"%0.1f", Number);
        if( strcmp(String,"0.0") == 0)
           return TRUE;
        return FALSE;
    }

    /Mikael


    This obviously tests for abs(Number)<0.05, in a very inefficient way.
  • (cs) in reply to ammoQ
    ammoQ:
    sicher:
    A friend of mine did consulting work at a company a couple of years ago. In their codebase, he found this little piece of beauty:

    BOOL IsZero(float Number)
    {
        char String[256];
        sprintf(String,"%0.1f", Number);
        if( strcmp(String,"0.0") == 0)
           return TRUE;
        return FALSE;
    }

    /Mikael


    This obviously tests for abs(Number)<0.05, in a very inefficient way.


    Negative numbers?
  • Roman (unregistered) in reply to d4ddyo
    d4ddyo:
    Alex Papadimoulis:

    if (connectionOpen || !connectionOpen)
    {
      ...
    }



    if (toB || !toB)
    {
        return that_is_the_question();
    }

     

    The correct way would be:

    if (twoBeer || !twoBeer)

    {

       return that_is_the_question();

    }

     

  • (cs) in reply to Roman

    So, am I correct in saying that the main reason not to use "float" to represent currency is that a number that can be represented exactly in decimal floating-point, like "0.01", can never be represented exactly in binary floating-point, because it would have to be represented by a repeating decimal (i.e. 0x1.47AE147AE147AE... and so on)?

Leave a comment on “Of Ifs And Bools”

Log In or post as a guest

Replying to comment #:

« Return to Article