Sunday, January 2, 2011

A little bit of fun

For fun, I wrote a little class in C# to handle arithmetic with significant figures. This is one of my goals for the new year. (I'm already getting one of them done! Wow, I am productive!)

I used Visual Studio Express for C#, which is simpler than the full Visual Studio package yet powerful enough to handle this task. The Express edition works almost identically to the Professional edition, so I was familiar with the environment.

The solution consists of a small class to handle the arithmetic and a small program to invoke the class. I don't have any test cases; I want to create some as I am uncomfortable working without them.

But the class does what it is supposed to do. The result of 1000 (1 sig-fig) plus 20 is 1000, not 1020 as one would expect. The answer should be 1000 since you must keep the number of significant figures at 1 and not expand it. Which means that 1000 plus 200 is also 1000, and 1000 + 200 + 200 + 200 + 200 + 200 is still 1000 although 200 + 200 + 200 + 200 + 200 + 1000 results in 2000, since the '200' amounts add up to 1000 and then the second 1000 yields 2000.

Leaving the oddities of sig-fig math, and coming back to programming, it was a fun project. Now perhaps I will port the solution to a few other languages.

No comments:

Post a Comment