Thread: return value to nearest 100?

  1. #16
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    He could just use an integer with mine as well in this case (since he's multiplying by 500)
    It would increase the speed a little, but it would still use the FPU because the result of your division returns a floating point variable. Even thought you wouldn't be using any explicit casts, C# still has to do some implicit casts behind the scenes.

  2. #17
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    That's true. However, I think mine will stay at about the same speed no matter what you're rounding to. Yours might be faster rounding to the nearest 100, but is it faster when rounding to the nearest 1,000 or 10,000?

  3. #18
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Well since I have some free time, I decided to do a bit of testing to see

    Here are the two functions:
    Code:
        static int Round1(int number, int place)
        {
            int i = 1;
            if (place <= 0)
                return number;
            while (place > 0)
            {
                i = i * 10;
                place--;
            }
    
            int r = number % i;
            if (r < (i / 2))
                return number - r;
            else
                return number - r + i;
        }
    
        static int Round2(int someNumber, int place)
        {
            int roundTo = 1;
            if (place <= 0)
                return (int)someNumber;
            while (place > 0)
            {
                roundTo = roundTo * 10;
                place--;
            }
            return ((someNumber + (roundTo / 2)) / roundTo) * roundTo;
        }
    I'm running the functions in a control loop 1,000,000,000 times.
    When rounding to the nearest 100, both functions took exactly 40s. When rounding to the nearest 10,000, yours took 40s, and mine took 41.5s. I guess we can conclude that your function is faster when rounding to larger numbers

  4. #19
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    I'd be interested to see how much of that time is spent in this part:
    Code:
    		while (place > 0)
    		{
    			roundTo = roundTo * 10;
    			place--;
    		}
    Also, I'm pretty sure this line can be safely removed:
    Code:
    		if (place <= 0)
    			return (int)someNumber;
    The place can't be < 0 (it's an integer) and if it's 0, I believe it works out the correct number.

  5. #20
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    I'd be interested to see how much of that time is spent in this part:
    I'm guessing about 10-40% depending on what you are rounding to. I can't really think of a faster way of doing it though.

    The place can't be < 0 (it's an integer) and if it's 0, I believe it works out the correct number.
    Integers can be less than 0...

  6. #21
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    Quote Originally Posted by bithub
    Integers can be less than 0...
    What the hell am I thinking.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is it ok like that?
    By ExDHaos in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2009, 09:02 AM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  3. Simulator
    By MasterAchilles in forum C Programming
    Replies: 10
    Last Post: 11-30-2008, 10:31 PM
  4. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  5. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM