Coding curiosity 4: Double trouble

If you're not used to working with floating-point numbers, then you might find some surprises. Witness the following code, which prints false as a result.

Just as 1/3 can't be repesented exactly as a decimal (0.3333...), 2.45 and many other numbers cannot be represented exactly in binary, the notation used by the CLR for floating-point numbers. If you use the ILDASM utility to examine the IL produced by the the code above, you'll see this. Note the interesting value being assigned to X!

As a slight digression, it's very easy to use ILDASM within Visual Studio to examine the IL produced by your code. In the dialog produced by External Tools... menu item on the Tools menu, simply add a new tool to the Tools menu with the following parameters. The /adv setting gives you some "advanced" menu settings within ILDASM, while the /source setting embeds your source code as comments within the IL listing.