The good people over at Tech.Pro have asked me to write a short series of articles for their site on the subject of mistakes I’ve made and seen others make when writing benchmark performance tests of C# code. The series will be pitched at the beginner level.
You can check out part one here. Enjoy!
The intention of this method is to round a double to the nearest integer. If the double is exactly half way between two integers then it rounds to the larger of the two possibilities:[1. For negative numbers, -1.5 should round to -1.0, since -1.0 is larger than -2.0; I do not mean larger in the sense of absolute magnitude. That would be characterized as “midpoint rounding away from zero”.]
static double MyRound(double d)
return Math.Floor(d + 0.5);
Is it correct? Can you find a value for which it does not give the mathematically correct value?[2. HINT: The value I’m thinking of is small.]
UPDATE: The answer is in the comments, so if you don’t want spoilers, don’t read the comments.
Next time on FAIC: The answer, of course.
Today, another of my ongoing series of reruns of my fun-for-Friday non-computer posts. Here’s one from the dot-com recovery of 2004.
The economy must be picking up — I’m getting cold calls from recruiters again for the first time in about four years. Today was the second – and third – this month.
However, apparently some of them are just a wee bit disorganized. I just had the following conversations:
Me: Hi, this is Eric.
Her: Hi, this is Barbara[1. I learned later from some of my fellow Microsoftie bloggers that it appeared that Barbara was calling everyone at Microsoft with an MSDN blog. She must have been repeatedly calling the switchboard and asking to speak with each.] at XYZ Recruiters. How are you today?
Last time on FAIC I generated a “random” permutation of a deck of cards and gave you the first five cards, and challenged you to determine what the next five cards were. David Poeschl (of the Roslyn team!) was the first to get a possible order and, with the additional hint that the sixth card was the three of hearts, Joel Rondeau found the correct solution, which is below. Both used brute-force algorithms.
Last time in this series I presented an algorithm for generating a random number, and the time before I presented an algorithm that turns such a number into a permutation, so we can put them together to make an algorithm that returns a random permutation:
static Permutation RandomPermutation(int size, Random random)
return Permutation.NthPermutation(size, RandomFactoradic(size, random));
Is this actually a correct algorithm for generating a random permutation? Give it some thought.
I’m back from a short but productive trip to beautiful San Francisco only to discover that the May-June issue of Dot Net Curry magazine in my inbox apparently has me on the cover. [1. Fortunately I did know about it ahead of time.]
It’s free, it’s online, but you have to “subscribe” to read it. Check it out!
Next time on FAIC: More on random permutations.
Photo by my colleague Bob.