Monday, March 31, 2008

C# quiz: 1/?

I'm starting a series with small questions C# and/or .NET Framework related questions:

First out, NaN values:

            double foo = double.NaN;
// ...
if (foo == double.NaN) {
Console.WriteLine("NaN");
} else {
Console.WriteLine("a number");
}

If or else ?

3 comments:

Anonymous said...

'a number', because equality comparison between two NaN numbers is always false. the is defined in the IEEE 754 spec.

Java behaves the same way.

double.IsNaN(foo) would give the desired result.

and no, i didnt know the spec number off the top of my head. I did know what the result would be but looked up the actuall reason.

I hope these are a regular thing, made me think a bit and look for the reason 'why'

/SiD said...

Thanks for your kind words, and nice to know that someone actually reads my posts! :) The intention is to continue postings as long as I come up with appropriate material.

Anonymous said...

Hi Sid,
Just came across your blog. The quiz idea is a gud one. It was a good learning understanding all those. I suggest you continue posting more quizes so that a lot of people will be benefited.
Thank you.