Sunday, July 29, 2007

Teeny Weeny Little Pains

While writing on some Ruby code, I hit a snag. I'm developing some infrastructure for engineering programming and was curious about values not coming out exactly right.

Having a few minutes, I got into irb and entered

(1..100).select{ |i| 10**-i - 10.0**-i != 0.0 }.join " "

Imagine my surprise when I got back

21 23 24 25 26 28 29 30 32 34 39 42 45 49 50 54 56 60 63 66 72 75 81 82 84 86 88 91 97

that's 29 numbers in the first 100 that have roundoff errors when taking the difference between integers and floats raised to high negative powers. Or perhaps this isn't curious - perhaps what is curious is that 71 numbers in the first 100 have no roundoff error.

Is this a Ruby bug? Well, maybe. The numbers are very small and probably won't cause anyone to lose too much sleep.

I'm certainly not waving hands or pointing fingers. I was just surprised. It just reinforces the skeptic in me that says nothing is ever finished and things may not always do what you expect.

Program with abandon, but check your work.

No comments: