The Solution To The Simple Puzzle

The first time I ran my histogram visualizer I asked for a Cauchy distribution with a minimum of -10 and a maximum of 10, and of course I got a graph that looks much like the one from my article of last week:   Looks perfectly reasonable; I guess my program is correct right out…

Looking inside a double

Occasionally when I’m debugging the compiler or responding to a user question I’ll need to quickly take apart the bits of a double-precision floating point number. Doing so is a bit of a pain, so I’ve whipped up some quick code that takes a double and tells you all the salient facts about it. I…

Precision and accuracy of DateTime

The DateTime struct represents dates as a 64 bit number that measures the number of “ticks” since a particular start date. Ten million ticks equals one second. That’s a quite high degree of precision. You can represent dates and times to sub-microsecond accuracy with a DateTime, which is typically more precision than you need. Not…

Is there such a thing as too much precision?

Well, enough chit-chat, back to programming language design. Suppose you’re building electronic piano software. As we’ve discussed before, the “equal temperament” tuning for a piano goes like this: the 49th note from the left on a standard 88 key piano is A, and its frequency is 440 Hz. Each octave above or below that doubles…

As Timeless As Infinity

User: Recently I found out about a peculiar behaviour concerning division by zero in floating point numbers in C#. It does not throw an exception, as with integer division, but rather returns an “infinity”. Why is that? Eric: As I’ve often said, “why” questions are difficult for me to answer. My first attempt at an answer to…

Every Number Is Special In Its Own Special Way

I got a question recently about where in the .NET framework the “special numbers” were defined. The questioner was actually asking about the Double.NaN, Double.PositiveInfinity, etc, special values for floating point numbers. Of course there are other “special numbers” defined by the framework, such as Math.PI. The question was easily answered but it got me…

Fun With Floating Point Arithmetic, Part Six

One more thing — I said earlier that the VBScript float-to-string algorithm was a little bit different than the JScript algorithm. We can demonstrate quite easily by comparing the outputs of two nigh-identical programs: ‘ VBScriptprint 9.2 * 100.0 < 920.0print 919.9999999999999 < 920.0 print 920.0000000000001 > 920.0 ‘ JScriptprint(9.2*100.00 < 920.0);print(919.9999999999999 < 920.0);print (920.0000000000001…

Fun with Floating Point Arithmetic, Part Five

I went to Joel Spolsky’s geek dinner at Crossroads the other night, which was a lot of fun. I didn’t get much of a chance to chat with Joel, as he was surrounded by a cadre of adoring fans three deep the whole time.  I mostly hung out with KC and Larry and some other…

Fun with Floating Point Arithmetic, Part Four

A reader also asked the other day why it is that in VBScript, CSng(0.1) = CDbl(0.1) is False. Forget about binary floating point for a moment. Suppose that we had two fixed-point decimal systems, say one with five digits after the decimal place and one with ten. You want to represent one-third. In our first…

Fun With Floating Point Arithmetic, Part Three

I’ve been getting lots of mail, questions and pointers to interesting articles on some of the trials and tribulations of using floating point arithmetic correctly. Please do keep it coming! Though I am certainly no expert in this area, I’m happy to take a crack at any questions you might have. To sum up the story…