• (cs) in reply to DeusEx
    DeusEx:
    Is there a prize for when the number of WTF's exceeds the number of lines of code?


    If there isn't, there should be because that isn't easy to do.  You have to try pretty hard.

    A small annoyance in the grand scheme but, I hate when people don't use a ternary for these simple if...else conditions.
  • (cs) in reply to Dave Markle
    Anonymous:
    He's not avoiding that.  He's avoiding a bug that showed up in .NET (and hasn't been fixed!) whereby if you divide zero by a number, you keep getting zero, instead of a DivisionWithZero exception.   I attempt to avoid such problems in my code by writing only Javascript.




    This looks like Java to me. 
  • (cs) in reply to OneFactor

    And for those who don't know, that means that the long type has 64 bits, regardless of the machine.

    Oh, and C++ doesn't throw exceptions on division by zero. The behaviour is implementation-defined: POSIX-based machines will cause a SIGFLT (I think), Windows-based will throw a /0 SEH exception (which is not the same as a C++ exception).

  • (cs)

    I think we need to start a new sourceforge project, libwtf

    It would be a collection (or perhaps, a collection for each submitted language) of the best entries found here.

    If possible, it should make the compiler (vm, etc...) explode.

    The CVS would need to be set up to not allow edits, we don't want people 'fixing' the code!

  • (cs) in reply to CornedBee
    CornedBee:
    And for those who don't know, that means that the long type has 64 bits, regardless of the machine.

    Oh, and C++ doesn't throw exceptions on division by zero. The behaviour is implementation-defined: POSIX-based machines will cause a SIGFLT (I think), Windows-based will throw a /0 SEH exception (which is not the same as a C++ exception).


    C++ on windows can be convinced to use standard C++ exceptions... I forget how at the moment, but it's not too difficult.
  • (cs) in reply to OneFactor
    OneFactor:
    Ytram:
    OneFactor:

    Anonymous:
    I don't get how making it positive then negative again is any easier than just doing it with the negative value.

    Possibly because he wants to round towards zero rather than to the floor. That is -2.3 minutes should round to -2 rather than -3.



    As far as I know, integer division does not necessarily use a floor function to perform the division.  It just simply takes the integer portion of the number without regard to the numbers after the decimal point.

    -2.3 as a result of integer division becomes -2
    2.3 as a result of integer division becomes 2

    I tested it in .NET and this is the case for that implementation of integer division at least.

    And for those of you just tuning in, today's WTF is brought to you by the letters J, A, V, and A.

    Hmm... I think I'm growing senile. Java integer division rounds towards zero as well. I was sure it rounded down though so that (-7)/3 would be -3 in java. Maybe I'm getting confused with Apple II Basic or something [:S]

  • (cs) in reply to Homer
    Anonymous:
    Djinn:
    Anonymous:

    Ytram:
    Anonymous:
    Actually, if you divide by zero a DivisionByZero exception will be thrown in C#, something similar in Java and IIRC C++ will throw an exception aswell


    You might want to re-read his comment.  Try turning your sarcasm detector on also.

    What's sarcasm?



    I think it's like rhetorics. Do you know what that is?


    Do I know what rhetorical means? (8(1)


    Nice monkey.  First time I've seen that.
  • (cs) in reply to AndrewVos
    AndrewVos:
    Alex Papadimoulis:

     //Watch out for exceptional value 0
      

     

    BWAAAAHAHAHAHHAHAHAHAAHHAHA! What a tosser!



    Is "wanker" passe' now?
  • (cs) in reply to Bustaz Kool
    Bustaz Kool:
    limelight:
    Alex Papadimoulis:
      if (milliDiff == 0) // Watch out for exceptional value 0
        minutesDiff = 0;
      else
        minutesDiff = (int) (milliDiff / 1000) / 60;

     

      I suppose he is a little confused about what "divison by zero" really means. The last time I checked dividing zero by any number always returns zero, so the extra check here is not necessary.

    To be fair, some of those zeroes are pretty exceptional.... except for the ones that aren't.

    Is that like the non-trivial zeros of the Riemann Zeta Function?

  • (cs) in reply to Rank Amateur
    Rank Amateur:
    limelight:

    Anonymous:
    Assuming there are more than ten additional bits in a long data type compared to an int data type, there's also an overflow here.

    You're right in that there is a possible overflow; I didn't even notice that. Although, to hit the overflow, the difference passed in would have to be equal to just at 4082 years, so it's doubtful that it would happen in practical usage. Still, I hate to see code where a bug is possible even if it's not practical.

    Worse than that. 2^15 minutes is only about 23 days, so overflow hits at about 2 billion (US) milliseconds.

    Wasn't there a bug in Windows like that that forced you to reboot servers after they were left on for about a month?

    --RA



    Yea, but it took years for anyone to notice, because that was back in the NT 4 days, and servers never went for a month without a reboot.

  • (cs) in reply to CornedBee
    CornedBee:
    And for those who don't know, that means that the long type has 64 bits, regardless of the machine.


    That has nothing to do with this wtf, which is written in Java.  Every JVM is required to use 32 bits for int, and 64 bits for long.  There's no guesswork for platform the code will be run on.
  • Xereos (unregistered) in reply to Djinn

    Rhetorics? Is that that new feature they are putting in C# 2.0?

  • (cs) in reply to Ytram

    Ytram:
    Comic Book Guy: A sarcasm detector, that's a real useful invention.

    KABOOM!

    Ytram, you're the reason I keep coming back to this site. It's not just your humor either. It's that sweet sweet pizza-making man-love.

  • Jonathan Pryor (unregistered) in reply to Lews
    On x86 (I doubt the coder was using something else), longs and ints are both 4 bytes long.

    In Java and C#, int is defined as a 32-bit integer while long is defined as a 64-bit integer. The actual processor is irrelvant.

  • (cs) in reply to OneFactor
    OneFactor:
    And for those of you just tuning in, today's WTF is brought to you by the letters J, A, V, and A.


    Just tested it in Java as well, and it performs the same way:

    public static void main(String [] args)
    {
            System.out.println("Positive: " + 5/2);
            System.out.println("Negative: " + -5/2);
            System.out.println("Straight cast of negative double(-2.3): " + (int)-2.3);
    }

    Had an output of:

    Positive: 2
    Negative: -2
    Straight cast of negative double(-2.3): -2

    So despite your Capt. Obvious remark about the WTF actually being written in Java, the quotient from integer division is still directed towards zero.
  • (cs) in reply to Rank Amateur

    Rank Amateur:
    Wasn't there a bug in Windows like that that forced you to reboot servers after they were left on for about a month?

    Yep. It was 49.7 days, actually, and it went unnoticed for about 4 years.

    Old news article on it: http://news.com.com/2100-1040-222391.html?legacy=cnet

    It had to do with the Windows Virtual Machine Manager and the Get_System_Time function. One of the things this function did was to load the EAX register with the number of milliseconds since the machine was started. EAX is a 32 bit register. When it looped, the time would reset to zero, and anything calling get_system_time would likely freak out.

    It only affected 95 and 98 First edition, actually. NT4 was actually not affected.

  • (cs)
    Alex Papadimoulis:
    Given the problem of converting milliseconds to minutes, I imagine that most of us would simply divide the number of milliseconds by 60,000.


    Right.

        milliDiff = -milliDiff; // Make positive (is easier)
    


    I love this line.  In C, C++, and possibly other C-derived languages, integer overflow on signed values results in undefined behaviour.  On a twos-complement machine (e.g. x86), applying negation to the most negative value results in an overflow.  Easier?  Easier to WTF!

    There is also the overflow of long to int that others have mentioned.

    Sincerely,

    Gene Wirchenko

  • (cs) in reply to Manni
    Manilovepizza:
    It's that sweet sweet pizza-making


    Yeah, I like pizza too!

    Manilovemen:
    man-love


    I'm just assuming this is a typo.


  • (cs) in reply to Anonymoose
    Anonymoose:
    I think we need to start a new sourceforge project, libwtf

    It would be a collection (or perhaps, a collection for each submitted language) of the best entries found here.

    If possible, it should make the compiler (vm, etc...) explode.


    You lack ambition, my friend.

    It should do its damnedest to frag the HARDWARE!
    • set screen resolutions the monitor can't handle - some older models could get permanently damaged that way, I've heard.
    • overwrite the BIOS and the firmware of all devices it can identify with random garbage
    • lock the harddisk with a randomly-generated password - nobody's gonna see THAT data ever again...
    Any other suggestions?

  • (cs) in reply to brazzy
    brazzy:
    Anonymoose:
    I think we need to start a new sourceforge project, libwtf

    It would be a collection (or perhaps, a collection for each submitted language) of the best entries found here.

    If possible, it should make the compiler (vm, etc...) explode.


    You lack ambition, my friend.

    It should do its damnedest to frag the HARDWARE!
    • set screen resolutions the monitor can't handle - some older models could get permanently damaged that way, I've heard.
    • overwrite the BIOS and the firmware of all devices it can identify with random garbage
    • lock the harddisk with a randomly-generated password - nobody's gonna see THAT data ever again...
    Any other suggestions?



    Ohh ohh!  I have some suggestions:
    • Re-distribute itself through the users e-mail address book
    • Log all keystrokes
    • Gather personal and private information from the users files and e-mail them to whoever we want
    • Gain control over the household appliances.  Repeatedly burn toast
    • Kill the users' family and burn house down
    When did we jump from a harmless library filled with WTFs to a malicious virus?
  • (cs) in reply to Anonymoose
    Anonymoose:
    I think we need to start a new sourceforge project, libwtf

    It would be a collection (or perhaps, a collection for each submitted language) of the best entries found here.

    If possible, it should make the compiler (vm, etc...) explode.

    The CVS would need to be set up to not allow edits, we don't want people 'fixing' the code!


    Too easy.

    When I was taking my diploma:

    1) Some C++ code caused the compiler to throw an internal error.  I had forgotten a pair of "()" after two method names in one line.

    2) One extra period in a COBOL program caused the compiler to crash.  It simply terminated with no error message.  Because of this, there was no listing generated.  It is good that I had a fair idea of what the error might be.  Without the period, the program worked perfectly.

    3) The same compiler blew up on another program of mine.  I had turned on the checking options (overflow, unitialised variable, and array bounds).  The uninitialised variable use check was incompatible with the COBOL report writer, and my code failed to work.  Without the option set, my code worked fine.

    Anyone else who has been in the industry for a while probably has similar stories.

    Sincerely,

    Gene Wirchenko

  • (cs)

    I bet the usage of this method looked something like this:

    <font size="1">long millidiff = System.getCurrentTimeMillis() - startTime;
    System.out.println("This took: "  +  convertToHours(millidiff)  +  ":"
                                      +  convertToMinutes(millidiff) + ":"
                                      +  convertToSeconds(millidiff));</font>

  • (cs) in reply to Gene Wirchenko
    Gene Wirchenko:
    Anonymoose:
    I think we need to start a new sourceforge project, libwtf

    It would be a collection (or perhaps, a collection for each submitted language) of the best entries found here.

    If possible, it should make the compiler (vm, etc...) explode.

    The CVS would need to be set up to not allow edits, we don't want people 'fixing' the code!


    Too easy.

    When I was taking my diploma:

    1) Some C++ code caused the compiler to throw an internal error.  I had forgotten a pair of "()" after two method names in one line.

    2) One extra period in a COBOL program caused the compiler to crash.  It simply terminated with no error message.  Because of this, there was no listing generated.  It is good that I had a fair idea of what the error might be.  Without the period, the program worked perfectly.

    3) The same compiler blew up on another program of mine.  I had turned on the checking options (overflow, unitialised variable, and array bounds).  The uninitialised variable use check was incompatible with the COBOL report writer, and my code failed to work.  Without the option set, my code worked fine.

    Anyone else who has been in the industry for a while probably has similar stories.

    Sincerely,

    Gene Wirchenko



    Similar in terms of the specific languages in use at the time or similar in terms of being absent-minded as a result of being semi-ignorant of the language you're working with?  If the latter, then anyone who has every learned a new language has similar stories, not just the dinosaurs.
  • (cs) in reply to kipthegreat
    kipthegreat:
    I bet the usage of this method looked something like this:

    <font size="1">long millidiff = System.getCurrentTimeMillis() - startTime;
    System.out.println("This took: "  +  convertToHours(millidiff)  +  ":"
                                      +  convertToMinutes(millidiff) + ":"
                                      +  convertToSeconds(millidiff));</font>



    Nevermind, that wouldn't quite work.. if it took more than 59 minutes, he'd see something like "This took: 2:143:16"
  • (cs) in reply to Mung Kee
    Mung Kee:
    Similar in terms of the specific languages in use at the time or similar in terms of being absent-minded as a result of being semi-ignorant of the language with which you're working?  If the latter, then anyone who has every learned a new language has similar stories, not just the dinosaurs.

    Before anyone lashes me with my own whip, I know, piss poor grammar.




  • Christian (unregistered)

    I really want a T-Shirt that says

      // Watch out for exceptional value 0
    
  • (cs) in reply to Mung Kee
    Mung Kee:
    Mung Kee:
    Similar in terms of the specific languages in use at the time or similar in terms of being absent-minded as a result of being semi-ignorant of the language with which you're working?  If the latter, then anyone who has every learned a new language has similar stories, not just the dinosaurs.

    Before anyone lashes me with my own whip, I know, piss poor grammar.


    This is the sort of nonsense up with which I shall not put!
  • badong (unregistered)

    WTF, I just don't get it... don't these people know, what the stack is made for?
    It should of course be

    static int milli_so_far = 0;

    int convertToMinutes (long milliDiff) {
        milli_so_far += 1;
        if (milli_so_far == 60000) {
           milli_so_far = 0;
           return convertToMinutes (milliDiff - 1) + 1;
        }
        return convertToMinutes (milliDiff - 1);
    }

    Disclaimer: Please note, that this method is not thread-safe. If you plan to use it in a threaded application, please don't blame be if it doesn't work!!

  • badong (unregistered) in reply to badong

    Oops, forgot to add the stopping condition...
    static int milli_so_far = 0;

    int convertToMinutes (long milliDiff) {
       
    if (milliDiff == 0)
           return 0;

        milli_so_far += 1;
        if (milli_so_far == 60000) {
           milli_so_far = 0;
           return convertToMinutes (milliDiff - 1) + 1;
        }
        return convertToMinutes (milliDiff - 1);
    }

  • (cs)

    Maybe he was paid by the line.

  • Mongoose (unregistered) in reply to Jon

    May as well go surf naked, for all the protection offered in the code.

    pi

    ^--- If a non-Y-mouse can do it, so can you!

  • Anonymous (unregistered) in reply to CornedBee
    CornedBee:

    Oh, and C++ doesn't throw exceptions on division by zero. The behaviour is implementation-defined: POSIX-based machines will cause a SIGFLT (I think), Windows-based will throw a /0 SEH exception (which is not the same as a C++ exception).


    On x86 machines, divide by zero generates a hardware exception (interrupt 0).
  • (cs) in reply to Rank Amateur
    Rank Amateur:
    ...Wasn't there a bug in Windows like that that forced you to reboot servers after they were left on for about a month?
    There might have been but it is hard to tell when it needs weekly rebooting  anyway.

    ps. Don't bother replying if all you want to say is you have had a Windows server last more than a week; I have no prizes to give away at this time.

  • (cs) in reply to Mung Kee
    Mung Kee:
    Gene Wirchenko:
    Anyone else who has been in the industry for a while probably has similar stories.


    Similar in terms of the specific languages in use at the time or similar in terms of being absent-minded as a result of being semi-ignorant of the language you're working with?  If the latter, then anyone who has every learned a new language has similar stories, not just the dinosaurs.


    Similar in terms of compilers that blow up on odd input.  One extra period crashing a compiler means the compiler is much frailer than it ought to be.  (That particular compiler was sensitive to errors in the FD/SD/RD areas.)  This is, of course, rather easy when you are still learning what unodd is.

    Sincerely,

    Gene Wirchenko

  • (cs) in reply to Ytram

    Ytram:
    Manilovepizza:
    It's that sweet sweet pizza-making


    Yeah, I like pizza too!

    Manilovemen:
    man-love


    I'm just assuming this is a typo.


    Sorry. The keys are like right next to each other.

    Today's response is brought to you by this quote from bash.org

  • JC (unregistered) in reply to Martin Carolan

    or -0.01715 minutes. 

    Jeff

  • JC (unregistered) in reply to Martin Carolan

    but we're dividing by 60k, not zero.

    Jeff

  • (cs) in reply to limelight
    limelight:
    Bustaz Kool:
    limelight:
    Alex Papadimoulis:
      if (milliDiff == 0) // Watch out for exceptional value 0
        minutesDiff = 0;
      else
        minutesDiff = (int) (milliDiff / 1000) / 60;

     

      I suppose he is a little confused about what "divison by zero" really means. The last time I checked dividing zero by any number always returns zero, so the extra check here is not necessary.

    To be fair, some of those zeroes are pretty exceptional.... except for the ones that aren't.

    Is that like the non-trivial zeros of the Riemann Zeta Function?

    Not to be confused with the trivial ones. [:P]

  • Himself (unregistered) in reply to Dave Markle

    Anonymous:
    He's not avoiding that.  He's avoiding a bug that showed up in .NET (and hasn't been fixed!) whereby if you divide zero by a number, you keep getting zero, instead of a DivisionWithZero exception.   I attempt to avoid such problems in my code by writing only Javascript.


     

    2 X WTF

  • (cs) in reply to Himself
    Anonymous:

    Anonymous:
    He's not avoiding that.  He's avoiding a bug that showed up in .NET (and hasn't been fixed!) whereby if you divide zero by a number, you keep getting zero, instead of a DivisionWithZero exception.   I attempt to avoid such problems in my code by writing only Javascript.


     

    2 X WTF



    You hitting the "Quote" button AND the "Post" button?
  • (cs) in reply to Gene Wirchenko
    Gene Wirchenko:
    Mung Kee:
    Gene Wirchenko:
    Anyone else who has been in the industry for a while probably has similar stories.


    Similar in terms of the specific languages in use at the time or similar in terms of being absent-minded as a result of being semi-ignorant of the language you're working with?  If the latter, then anyone who has every learned a new language has similar stories, not just the dinosaurs.


    Similar in terms of compilers that blow up on odd input.  One extra period crashing a compiler means the compiler is much frailer than it ought to be.  (That particular compiler was sensitive to errors in the FD/SD/RD areas.)  This is, of course, rather easy when you are still learning what unodd is.

    Sincerely,

    Gene Wirchenko



    I see.  I happened upon the scene when frailty of compilers wasn't such an issue.  Unfortunately, frailty of knowledge on the other hand, which plagues all who try a new language, will be with us forever.  In the end, it's the same struggle.
  • ByteJuggler (unregistered) in reply to DeusEx
    DeusEx:
    And I don't know *how many* times I've forgotten to watch out for the exceptional value 0. Maybe this comment will help me remember.


    Yeah... zero's purportedly especially dangerous when you divide it by anything... 8-)

  • ABAP (unregistered) in reply to Erin

    > Except integer division doesn't round, it truncates, and will therefore always go towards zero anyways.

    Depends on the "programming language"...

  • (cs) in reply to boohiss

    He could do a case
    switch(IsTrue(milliDiff==0)){
    case true: //do nothing;
        break;
    case false: //flip to positve
        break;
    case else:  //return file not found.
        break;
    }

    JC

  • AC (unregistered) in reply to Anonymous Coward
    Anonymous:
    I don't get how making it positive then negative again is any easier than just doing it with the negative value.

    You must have slept through middschool then! Just try for yourself how to do long division with a negative dividend! See, it's already confusing to a human, let alone a much less intelligent computer!

    And special casing 0 is actually a good thing. What's the point of the error prone division, when the result is 0 anyway? Always remember the lessons the original Intel Pentium (codenamed something around i585.99) taught us!

  • (cs) in reply to OneFactor
    OneFactor:
    limelight:
    Bustaz Kool:
    limelight:
    Alex Papadimoulis:
      if (milliDiff == 0) // Watch out for exceptional value 0
        minutesDiff = 0;
      else
        minutesDiff = (int) (milliDiff / 1000) / 60;

     

      I suppose he is a little confused about what "divison by zero" really means. The last time I checked dividing zero by any number always returns zero, so the extra check here is not necessary.

    To be fair, some of those zeroes are pretty exceptional.... except for the ones that aren't.

    Is that like the non-trivial zeros of the Riemann Zeta Function?

    Not to be confused with the trivial ones. [:P]

    I guess that might had been more of a mathematician's joke than a programmer's. The "trivial" zeros of the Zeta function occur for all negative even integer inputs. The non-trivial zeros occur for certain complex numbers and have a very close relation to the prime counting function. The Riemann hypothesis asserts that all non-trivial zeros have real part of one-half. This has never been proven and is one of the last problems from Hilbert's list that has yet to be resolved.

  • (cs) in reply to tedbilly
    tedbilly:

    0 / n = 0 // This is NOT a bug in .NET, this is valid math!

    n / 0 = DivideByZero Exception

    In this case his math should be; milliseconds / 60000 = minutes.  If milliseconds is 0 then minutes will be 0 (try it if you don't believe me)



    Next thing you know, samples from this site will crop up all over tedbilly's code.
  • (cs)
    Alex Papadimoulis:

    Given the problem of converting milliseconds to minutes, I imagine that most of us would simply divide the number of milliseconds by 60,000. That's because most of us are not experts in the art of complexification. We've all certainly seen the work of such "artists" on this site before and, I have to say, that they never fail to impress. Take, for example, how Jesper's predecessors solved the "millisecond conversion" problem. Impressive, eh?

    private int convertToMinutes(long milliDiff)
    {
    boolean negative = false;
    int minutesDiff = -1;

    if (milliDiff < 0)
    {
    negative = true;
    milliDiff = -milliDiff; // Make positive (is easier) }

    if (milliDiff == 0) // Watch out for exceptional value 0 minutesDiff = 0; else minutesDiff = (int) (milliDiff / 1000) / 60;

    if (negative) minutesDiff = -minutesDiff; // Make it negative again

    return minutesDiff;
    }



    The real wtf here is that this subhuman programmer is using a ridiculous, space-wasting brace style.

  • (cs)
    Alex Papadimoulis:
    private int convertToMinutes(long milliDiff)
    {
    boolean negative = false;
    int minutesDiff = -1;

    if (milliDiff < 0)
    {
    negative = true;
    milliDiff = -milliDiff; // Make positive (is easier) }

    if (milliDiff == 0) // Watch out for exceptional value 0 minutesDiff = 0; else minutesDiff = (int) (milliDiff / 1000) / 60;

    if (negative) minutesDiff = -minutesDiff; // Make it negative again

    return minutesDiff;
    }



    If they are worried about milliDiff being negative, the solution is (in Java) the following.

    minutesDiff = Math.abs( time2 - time1 ) / 60000;

    Since there are no comments documenting what the function should output (another WTF), the above statement is an acceptable replacement for this function.

  • (cs) in reply to limelight

    limelight:
    [

    Is that like the non-trivial zeros of the Riemann Zeta Function?

    For all negative even integers, no!  Those are indeed trivial...

Leave a comment on “The Millisecond Converter”

Log In or post as a guest

Replying to comment #:

« Return to Article