Influenced by Pascal...



  • I never had a problem asking for help with code I didn't understand.

    I really wish I hadn't this time...

    When looking at some very old code to set the location of a graphical annotation, I ended up talking to one of our senior developers.

    The conversation:

    "Oh, yeah.  I remember the guy who wrote that. See--here's the thing...He has a very...different style of coding.
    I like it, granted, but it's definitely different. I think he was influenced by Pascal.  He likes to flip things
    around back and forth until he gets them the way he wants them."

    //The code in question
    int senseCount = nNintersections + nEdges + nVertexParams;

    bool flip = false;

    for (int ii=0;  ii!= senseCount; ++ii)
    {
        flip = !flip;
    }

     



  • Flip.... Flip, Flip, Flip the patties! Flip, Flip, Flip!

    Oh Parappa the Rappa, the memories you bring.



  • Doesn't seem so weird to me..  I make use of this paradigm all the time.

     

    int is_neg(long n)
    {
        // tells if a number is negative or positive
    
    for (;;) {
        n++;
    
        if (n == 0) {
            return 1;
        } else if (n == 2147483647) {
            return 0;
        }
    }
    

    }



    long multi_add(int x, int y)
    {
    // takes the first number and finds out how much it is
    // added to itself the number of times specified by the second

    long result = 0;
    
    for(int i = 0; i < y; i++) {
        result += x;
    }
    
    return result;
    

    }


  • Garbage Person

    @morbiuswilters said:

    Satanic code

    HEAD ASPLODES



  • @morbiuswilters said:

    I have come here to chew memory..
     

    and processor time.. 



  • @ZippoLag said:

    @morbiuswilters said:

    I have come here to chew memory..
     

    and processor time.. 

    Are you joking?  is_neg can determine that 5 is positive in less than 10 seconds on my computer.  I'd like to see you now-it-alls do better. 



  • @morbiuswilters said:

    @ZippoLag said:

    @morbiuswilters said:

    I have come here to chew memory..
     

    and processor time.. 

    Are you joking?  is_neg can determine that 5 is positive in less than 10 seconds on my computer.  I'd like to see you now-it-alls do better. 

    Fine, here you go:

    int is_neg(long n)
    {
    // tells if a number is negative or positive

    for (;;) {
    n /= 2;

    if (n == 1) {
    return 1;
    } else if (n == -1) {
    return 0;
    }
    }
    }

     Stick around, maybe I'll teach you a thing or two about good programming.

     



  • @bstorer said:

    Fine, here you go

    You're both sick. LOL



  • @bstorer said:

     Stick around, maybe I'll teach you a thing or two about good programming.

    Nice hustle, but is_neg(0) causes my machine to get stuck in an infinite loop.  Better luck next time, rookie. 



  • @morbiuswilters said:

    @bstorer said:

     Stick around, maybe I'll teach you a thing or two about good programming.

    Nice hustle, but is_neg(0) causes my machine to get stuck in an infinite loop.  Better luck next time, rookie. 

    You can't figure out if 0 is negative without using a computer?  Pathetic.


  • I'm feeling dizzy.. 



  • @bstorer said:

    You can't figure out if 0 is negative without using a computer?  Pathetic.

    Listen, that kind of attitude may pass the mustard in your academic ivory tower.  But here in the real world we have to deal with user data data data.  And part of that means sometimes there are zeroes and you can't use your "elegant" solutions and you just have to Jam it!



  • @morbiuswilters said:

    @bstorer said:

    You can't figure out if 0 is negative without using a computer?  Pathetic.

    Listen, that kind of attitude may pass the mustard in your academic ivory tower.  But here in the real world we have to deal with user data data data.  And part of that means sometimes there are zeroes and you can't use your "elegant" solutions and you just have to Jam it!

    Fine, fine.  The workaround is fairly obvious, of course.  I'm surprised you couldn't figure it out for yourself.  Just further proof that my work isn't done here, I guess:

    int is_neg(long n)
    {
        // tells if a number is negative or positive
        int i = 0;
        for (;;) {
            n /= 2;
    
        if (n == 1) {
            return 1;
        } else if (n == -1) {
            return 0;
        }
    ++i;
    if (i == 32)
      return 1;
    }
    

    }



  • @bstorer said:

    int i = 0;

    What a memory hog.  Hey, I hear there's an opening on the Firefox team -- you might consider joining. 



  • @morbiuswilters said:

    @bstorer said:

    int i = 0;

    What a memory hog.  Hey, I hear there's an opening on the Firefox team -- you might consider joining. 

    Too true, should've been a char.


  • @bstorer said:

    int is_neg(long n)
    {
    // tells if a number is negative or positive
    int i = 0;
    for (;;) {
    n /= 2;

        if (n == 1) {
            return 1;
        } else if (n == -1) {
            return 0;
        }
    ++i;
    if (i == 32)
      return 1;
    }
    

    }


    Is there a WTF competition on that I missed? The code in this thread is sick. Brilliant, but sick.



  • @bstorer said:

    if (i == 32) return 1;
     

    Thanks for the code, I've been using it with great success. I've noticed that large negative numbers are detected as positive numbers though. It seems that the function isn't quite ready for production yet.



  •  @Nandurius said:

    It seems that the function isn't quite ready for production yet.
      Nonsense.  All code has been tested and confirmed to work on my production 286.



  • @bstorer said:

    Nonsense.  All code has been tested and confirmed to work on my production 286.
     

    Unfortunately there's no money for hardware upgrades left in the budget this year.

    I managed to fix the issue with large numbers, though, by making multiple passes through the division loop. Since my doubles are twice as long as the standard 32 bit ones, I need at least two passes.. I added another one just to be on the safe side of things.

     

      static boolean is_neg(long n)
    {
    // tells if a number is negative or positive
    int i = 0;
    int passes = 0;

    for (;;)
    {
    n /= 2;

    if (n == 1)
    {
    return false;
    }
    else if (n == -1)
    {
    return true;
    }
    ++i;

    if (i == 32)
    if( passes == 2)
    return true; // return false;
    else {
    passes++;
    i = 0;
    }
    }
    }
    Unfortunately there still seems to be an issue with -1, which was returning false. If we redefine 0 to be negative though, and change the last return to true then it works as expected.


  • @Nandurius said:

    I managed to fix the issue with large numbers, though, by making multiple passes through the division loop.
     

    No, no, no.  Just copy and paste the loop twice, obviously, and reset i to 0.

    @Nandurius said:

    Unfortunately there still seems to be an issue with -1, which was returning false.

    Simple solution to that, too: just subtract 1 from n before testing it.

     



  • @Nandurius said:

    ...almost 30 lines of code... 

    I swear, today's programmers can't code, even if I hit them on the head with a mallet. Observe, students:

    // tells if a number is negative or positive 
    static boolean is_neg(long n) {
    	for (; n != 0; n /= 2) if (n == -1) return true; return false;

    There, just one line, doesn't waste memory for extra variables and correctly handles "0" and "-1".



  • use threads;
    
    sub universe
    {
    	my ($number, $direction) = @_;
    	while ($number) { $number += $direction; }
    	threads->exit();
    }
    
    sub is_neg
    {
    	my $number = shift;
    	my $positive_universe = threads->create('universe', $number, 1);
    	my $negative_universe = threads->create('universe', $number, -1);
    	while (1)
    	{
    		if (not $positive_universe->is_running())
    		{
    			$negative_universe->kill('DESTROY');
    			return 0;
    		}
    		if (not $negative_universe->is_running())
    		{
    			$positive_universe->kill('DESTROY');
    			return 1;
    		}
    	}
    }


  • for (; n != 0; n /= 2)

    This is far too complicated for maintainance programmers to understand. What's wrong with do ... while (n /= 2)?

    And using an int returning values of 1 and 0 would save a whole load of characters. Really, I don't know how your company can afford to retain people like you who insist on filling up the punched cards with all this verbosity.



  • @Iago said:

    for (; n != 0; n /= 2)

    This is far too complicated for maintainance programmers to understand. What's wrong with do ... while (n /= 2)?

     

    How about...

    for(; n; n >>= 1)

    Regarding the original post: I know it's just a question of personal taste, but I really dislike when people use ++x instead of x++ in cases where there is no difference between the two (e.g. here in the third part of the 'for' clause). I don't know why, bust I just feel it's plain wrong. Yes, I know it doesn't make any difference, but still, it just looks wrong. Any opinions?

     Edit: Oh yes, sorry, that was a signed integer. Never mind the bit shift then.



  • @DrJokepu said:

    I know it's just a question of personal taste, but I really dislike when people use ++x instead of x++ in cases where there is no difference between the two (e.g. here in the third part of the 'for' clause). I don't know why, bust I just feel it's plain wrong. Yes, I know it doesn't make any difference, but still, it just looks wrong.

    Technically, it is wrong.  x++ creates a temporary variable to store the return value.  Now, most compilers are smart enough to convert it to ++x on the fly, but that doesn't make it right.  Off the top of my head I cannot conceive of a single time when x++ is superior.  A construct like n = ++x is no more expressive -- really, it's actually more muddled -- and less efficient than doing n = x; ++x;



  • @bstorer said:

    A construct like n = ++x is no more expressive -- really, it's actually
    more muddled -- and less efficient than doing n = x; ++x;

    Now I am far from being a C expert, but isn't n = ++x is more like x = x + 1; n = x; instead of n = x; ++x;? Note the difference between the final values of n.



  • @DrJokepu said:

    @bstorer said:
    A construct like n = ++x is no more expressive -- really, it's actually more muddled -- and less efficient than doing n = x; ++x;

    Now I am far from being a C expert, but isn't n = ++x is more like x = x + 1; n = x; instead of n = x; ++x;? Note the difference between the final values of n.

    Quiet, you!  I meant n = x++ is less expressive and efficient than n = x; ++x;  Clearly I have an aversion to even typing x++.


  • @bstorer said:

    @DrJokepu said:

    I know it's just a question of personal taste, but I really dislike when people use ++x instead of x++ in cases where there is no difference between the two (e.g. here in the third part of the 'for' clause). I don't know why, bust I just feel it's plain wrong. Yes, I know it doesn't make any difference, but still, it just looks wrong.

    Technically, it is wrong.  x++ creates a temporary variable to store the return value. 

     

     And in C++, when x is an object with overloaded preincrement and postincrement operators, there can be a massive difference in efficiency. The preincrement can just increment the object and return it. The postincrement must copy the object (i.e. create a new one using the copy-constructor), then increment the original and return the copy. That's why people should write ++iterator instead of iterator++. 



  • @mxsscott said:

    And in C++, when x is an object with overloaded preincrement and postincrement operators, there can be a massive difference in efficiency. The preincrement can just increment the object and return it. The postincrement must copy the object (i.e. create a new one using the copy-constructor), then increment the original and return the copy. That's why people should write ++iterator instead of iterator++.

    Or, um, avoid operator overloading altogether. 



  • @morbiuswilters said:

    @mxsscott said:

    And in C++, when x is an object with overloaded preincrement and postincrement operators, there can be a massive difference in efficiency. The preincrement can just increment the object and return it. The postincrement must copy the object (i.e. create a new one using the copy-constructor), then increment the original and return the copy. That's why people should write ++iterator instead of iterator++.

    Or, um, avoid operator overloading altogether. 

    Or, um, avoid C++ altogether.


  • @bstorer said:

    @morbiuswilters said:

    @mxsscott said:

    And in C++, when x is an object with overloaded preincrement and postincrement operators, there can be a massive difference in efficiency. The preincrement can just increment the object and return it. The postincrement must copy the object (i.e. create a new one using the copy-constructor), then increment the original and return the copy. That's why people should write ++iterator instead of iterator++.

    Or, um, avoid operator overloading altogether. 

    Or, um, avoid C++ altogether.
     

    But then there would be no WTFs! 



  • @mxsscott said:

    But then there would be no WTFs!
    So long as there are languages to program in, there will WTFs.  Have no fear.



  • @mxsscott said:

    But then there would be no WTFs!
    Why would C++ be necessary for WTFs?



  • @bstorer said:

    Technically, it is wrong.  x++ creates a temporary variable to store the return value.  Now, most compilers are smart enough to convert it to ++x on the fly, but that doesn't make it right.

    You may be a smart programmer, but you have a lot to learn about programming.  Do all of your fancy books teach you a damn thing about relating to your variables as people?  You see creating a temporary variable as wasteful, I see a variable who is going through a scary change and who might appreciate a friend.  Remember the story of Variable Jesus who allowed his own destructor to be called so that all other variables might have everlasting scope.



  • @ender said:

    Why would C++ be necessary for WTFs?

    C++ doesn't create WTFs, it only attracts them.  The hope is that one day the C++ spec becomes so massive that all WTFs are contained therein and it can then be nuked from orbit. 



  • @bstorer said:

    Nice hustle, but is_neg(0) causes my machine to get stuck in an infinite loop.  Better luck next time, rookie
     

    here's a fix

    int is_neg(long n)
    {
        // tells if a number is negative or positive
        if (n == 0) return FILE_NOT_FOUND;
        for (;;) {
            n /= 2;

            if (n == 1) {
                return 1;
            } else if (n == -1) {
                return 0;
            }
        }
    }



  • @piptheGeek said:

    @bstorer said:
    Nice hustle, but is_neg(0) causes my machine to get stuck in an infinite loop.  Better luck next time, rookie
     

    learn2quote

     

    That said, your program fails because I always #define FILE_NOT_FOUND to 1 in all of my C headers.  Obviously, is_neg should not return 1 for 0. 



  • @morbiuswilters said:

    You see creating a temporary variable as wasteful, I see a variable who is going through a scary change and who might appreciate a friend.  Remember the story of Variable Jesus who allowed his own destructor to be called so that all other variables might have everlasting scope.
    You drag your religion in as a justification of your wasteful practices?  Next you'll be claiming that C++ is the result of intelligent design, despite the overwhelming evidence showing its evolution from C.



  • @DrJokepu said:

    Regarding the original post: I know it's just a question of personal taste, but I really dislike when people use ++x instead of x++ in cases where there is no difference between the two (e.g. here in the third part of the 'for' clause). I don't know why, bust I just feel it's plain wrong. Yes, I know it doesn't make any difference, but still, it just looks wrong. Any opinions?

     

     

    whats wring with using inline asm instruction - inc? 



  • @Helix said:

     

    whats wring with using inline asm instruction - inc? 

    Other than a lack of portability, doing something in assembly that has a direct C counterpart, and limiting the compiler's ability to optimize?  Nothing, I guess.


  • @Mo6eB said:

    @Nandurius said:

    ...almost 30 lines of code... 

    I swear, today's programmers can't code, even if I hit them on the head with a mallet. Observe, students:

    // tells if a number is negative or positive 
    static boolean is_neg(long n) {
    	for (; n != 0; n /= 2) if (n == -1) return true; return false;

    There, just one line, doesn't waste memory for extra variables and correctly handles "0" and "-1".

     

     

    This is actually two lines of code so time to learn a little known programming fact "the more lines of code the more expensive it is to maintain".

    Additionaly this example shows javas waste of processor cycles, after your byte code has been interpreted, mine is executed without any loops or maths:

     

    int is_neg(long n) {return((n&0x80000000)>>31);} 

     

    Just an AND and SHR on intel

     



  • //using 72 as test case

    //Arm7TDMI core

     

    bstores solution cycle count = 234

    helix solution cycle count = 2

     ~100 times faster code without changing tools



  • @Helix said:

     ~100 times faster code without changing tools
     

    I'm not sure you're getting what's going on here... 



  • @upsidedowncreature said:

    @Helix said:

     ~100 times faster code without changing tools
     

    I'm not sure you're getting what's going on here... 

     

    And my worry is, if you're not getting that everyone is producing inefficient solutions on purpose, that you didn't write:

    bool is_neg(long x) { return (x < 0); }

    which works for any size of long.



  • @upsidedowncreature said:

    @Helix said:

     ~100 times faster code without changing tools
     

    I'm not sure you're getting what's going on here... 

    SSDS is the only tool you need!

    I think the original writer of the flipping code deserves a job flipping burgers. 



  • this cirtainly does not compile any flatter - worse depending on tool.  Also bool is not suggested for use due to vendors c standard implimentation - i prefer to stick with  ISO/IEC 9899:1990 Programming Languages C standard, changed by Amendment 1:1995, Technical corrigendum 1:1994, and Technical corrigendum 2:1996.  That way there is no problems with code migration.



  • @Helix said:

    whats wring with using inline asm instruction - inc?

     

    @Helix said:

    This is actually two lines of code so time to learn a little known programming fact "the more lines of code the more expensive it is to maintain".

    Additionaly this example shows javas waste of processor cycles, after your byte code has been interpreted, mine is executed without any loops or maths:

     

    int is_neg(long n) {return((n&0x80000000)>>31);} 

     

    Just an AND and SHR on intel

     

    @Helix said:

    That way there is no problems with code migration.

     

    *cough* 



  • I suggest you try bool is_neg(long x) { return (x < 0); } and  int is_neg(long n) {return((n&0x80000000)>>31);}  in multiple c compilers and archs and let me know the results of processor time and code portability.<o:p></o:p>

     <o:p></o:p>

    The trouble is these days too many posters on TDWTF forums do not run real 'down to the metal code' and just run queries on databases - a bit like the receptionist at the doctors.   <o:p></o:p>



  • @Helix said:

    this cirtainly does not compile any flatter - worse depending on tool.  Also bool is not suggested for use due to vendors c standard implimentation - i prefer to stick with  ISO/IEC 9899:1990 Programming Languages C standard, changed by Amendment 1:1995, Technical corrigendum 1:1994, and Technical corrigendum 2:1996.  That way there is no problems with code migration.

    I was using C++, so bool is fine to use. Agreed that int is better for x-compiler compatibilty with C.

    However, it's time for compilation wars:

    return (n < 0) compiles as:

    0x00001df9  <+0015>  shr    $0x1f,%eax

    return (0x8000000 & n) >> 31 compiles as:

    0x00001e0d  <+0015>  and    $0x8000000,%eax
    0x00001e12  <+0020>  sar    $0x1f,%eax 

    This, with gcc 4.0.1 (Xcode 3 on an Intel Mac).

    Cunningly, if I compiled in release mode, the functions didn't exist (so saved all that parameter passing and stack manipulation) and it just shoved 0 onto the stack for the call to printf (I was asking if 10000 was negative).


  • @Helix said:

    I suggest you try bool is_neg(long x) { return (x < 0); } and  int is_neg(long n) {return((n&0x80000000)>>31);}  in multiple c compilers and archs and let me know the results of processor time and code portability.

    Are you a complete dumbass or what?  Not only did you somehow miss that this whole thread was a joke, but you took the assignment seriously and actually wrote a function that checks if a number is negative.  I'll let your contradictory, babbling quotes on portability speak for themselves.

     

    @Helix said:

    The trouble is these days too many posters on TDWTF forums do not run real 'down to the metal code' and just run queries on databases - a bit like the receptionist at the doctors.

    Apparently that's the only way to keep your sanity.


Log in to reply