次の方法で共有


Bug Hunt!

Pedantic literary types love to dissect the simplest of English phrases and run amok with their often trivial corrections! So, what does the computer world have to rival the unfaltering persistence of these chaps? Bug finders! What takes more patience, and thought than pouring over line after line of code? For some time now a fellow MSDN blogger, Rick Samona, has been running a series of ‘Spot-the-bug’ posts, which are great fun and a good distraction on a quiet afternoon. To give you a flavor I have knocked up a short little ‘C’ snippet that contains a bug. Have a bash at finding it and drop a comment when you think you have the best solution. Good luck with the bug hunting...

/*******************
* The following function
* returns the amount of
* characters in an integer
* number
* E.g.
* lengthOfInt(100) => 3
* lenghtOfInt(51874) => 5
* lengthOfInt(9) => 1
******************/
int lengthOfInt( int x )
{
x = floor(log10((double)x));
return ++x;
}

Spot the bug in the above code. If you can, provide the simplest soution to the problem in the comments section.Don't forget to enter the Mint Source X-Box 360 Competition!

Found that too easy? Get yourself to The Bug Hunt Blog!
https://blogs.msdn.com/rsamona/

Comments

  • Anonymous
    January 09, 2006
    The entries have been flying in (though to my e-mail inbox for some reason)   The best answer I have had so far is: ##################
            double lengthOfInt(int x)         {             double t;             t = floor(log10((double)x));             return t;         }
    Basically you can’t convert type double to int. What you have to do is return a double. Hence the need for t!! ########################## If you run the code Visual Studio or another compiler I used, it will work fine (though I agree that it should at least have a forced cast in there) what I am looking for will probably cause a big problem and it's to do with the logic, not syntax, though of course a change in syntax will fix the problem.. ######################## As pointed out by another reader the following is a nice solution to the double -> Integer Problem: int lengthOfInt( int  x)
    {
       x = int(floor(log10((double)x)));
       return ++x;
    }

  • Anonymous
    January 09, 2006
    The entries have been flying in (though to my e-mail inbox for some reason)



    The best answer I have had so far is:

    ##################
    double lengthOfInt(int x)

    {

    double t;

    t = floor(log10((double)x));

    return t;

    }


    Basically you can’t convert type double to int. What you have to do is return a double. Hence the need for t!!

    ##########################

    If you run the code Visual Studio or another compiler I used, it will work fine (though I agree that it should at least have a forced cast in there)

    what I am looking for will probably cause a big problem and it's to do with the logic, not syntax, though of course a change in syntax will fix the problem..

    ########################

    As pointed out by another reader the following is a nice solution to the double -> Integer Problem:

    int lengthOfInt( int x)
    {
    x = int(floor(log10((double)x)));
    return ++x;
    }


  • Anonymous
    January 09, 2006
    This is a slightly modified version of what I emailed! I tried not to include any answers I didnt give to start with :)

    Got it I think:

    2 things:
    -Negative numbers cause erronous output, giving the results in terms of i (Imaginary numbers)
    -Several values of x are impossible, [-1, 0]


    I’m not sure of my C code, so the syntax may be bad, but…



    /*******************
    * The following function
    * returns the amount of
    * characters in a [POSITIVE] integer
    * number
    * E.g.
    * lengthOfInt(100) => 3
    * lenghtOfInt(51874) => 5
    * lengthOfInt(9) => 1
    ******************/
    int lengthOfInt( int x )
    {
    if (x>=1){

    x = floor(log10((double)x))+1;
    return ++x;

    }
    }

  • Anonymous
    January 11, 2006
    Excellent James! That was indeed the answer I was looking for. The solution you have there looks pretty good though it probably wouldn't compile due to it missing a return value if the IF evaluates to false. Otherwise it doesn't change the semantics of the the function. Another solution which would change the function somewhat is to change it to unsigned intergers! or.. have for integers you could write:

    int lengthOfString(int x)
    {
    if( (x > -10) && ( x < 10) )
    {
    return 1;
    }
    else if( x <= -10)
    {
    x = int ( floor ( log10( (double) (-x) )));
    x = x+2;//accounting for floor and - return x;
    }
    else
    {
    x = int ( floor ( log10( (double) (x) ) ) );
    x++; return x;
    }
    return 0;
    }

    The above expresses the logic that should allow the function to evaluate correctly with a signed int... Hope you enjoyed the little bug hunt!

  • Anonymous
    June 18, 2009
    PingBack from http://onlyoutdoorrugs.info/story.php?id=385