Today on FAIC, a detective story.
Continue reading
Category Archives: C#
Benchmarking mistakes, part two
What is lexical scoping?
Happy Eliza Doolittle day all; today seems like an appropriate day for careful elocution of technical jargon. So today, yet another question about "scope". As one of the more over-used jargon terms in programming languages, I get a lot of questions about it.
I'll remind you all again that in C# the term "scope" has a very carefully defined meaning: the scope of a named entity is the region of program text in which the unqualified name can be used to refer to the entity.1
- Scope is often confused with the closely related concepts of declaration space (the region of code in which no two things may be declared to have the same name), accessibility domain (the region of program text in which a member's accessibility modifier permits it to be looked up), and lifetime (the portion of the execution of the program during which the contents of a variable are not eligable for garbage collection.) ↩
Spot the defect: rounding, part two
Last time I challenged you to find a value which does not round correctly using the algorithm
Math.Floor(value + 0.5)
The value which does not round correctly is the double 0.49999999999999994, which is the largest double that is smaller than 0.5. With the given algorithm this rounds up to 1.0, even though clearly 0.49999999999999994 is less than one half, and therefore should round down.
What the heck is going on here?
Benchmarking mistakes, part one
Spot the defect: rounding
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
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
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.
Producing permutations, part seven
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 Poeschl1 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.
- Of the Roslyn team! ↩
Producing permutations, part six
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.
Mmm, curry
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
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.
- Fortunately I did know about it ahead of time. ↩
Producing permutations, part five
Last time on FAIC I showed how you could produce the mth permutation of n elements, where m is a number between zero and n!-1 that chooses from the n! possible permutations. There's a very interesting way to represent numbers that are always less than n! for a given n that I thought I might talk about today.