The oddities of floating-point arithmetic

Posted by Tom Moertel Wed, 28 Sep 2005 02:59:00 GMT

Tobias at too-biased asks wtf? is going on with the following unintuitive result from Ruby:

irb(main):001:0> (4.10 * 100).to_i
=> 409

What’s going on is that to_i truncates its floating-point argument, discarding the fractional part. The fractional part, owing to the intricacies of floating-point arithmetic, does not agree with our intuition:

irb(main):002:0> "%3.20g" % (4.10 * 100)
=> "409.99999999999994316" 

In this case, the factional part is almost 1. Throwing it away turns our expected result of 410 into the unexpected 409. (That’s why round makes for a better choice than to_i when we want the nearest integer to our float.)

If this behavior seems bizarre, I recommend David Goldberg’s classic What Every Computer Scientist Should Know About Floating Point Arithmetic (1991). The paper was originally published in ACM Computing Surveys and later republished with corrections. The corrected versions, however, were apparently typeset in Microsoft Word, and the formulas are hard to look at (especially if you are accustomed to TeX-quality math typesetting). You might want to get your hands on a PDF of the original version if you care about that kind of thing.

Posted in
no comments
no trackbacks
Reddit Delicious

Scope herding with delimited continuations

Posted by Tom Moertel Tue, 13 Sep 2005 14:24:00 GMT

Recently I took advantage of delimited continuations to create a more natural Haskell-based kernel for GIML. The amazing scope-herding abilities of reset and shift were the magic that made it possible.

Ready to get wild? Grab an espresso and read on.

Read more...

Posted in , ,
Tags , ,
2 comments
no trackbacks
Reddit Delicious

New RPM release: Emacs Speaks Statistics 5.2.10

Posted by Tom Moertel Mon, 12 Sep 2005 18:12:00 GMT

I just posted new RPMs for Emacs Speaks Statistics. These RPMs are for the recent ESS 5.2.10 release. As usual, you can get them from the RPMs section of the Community Projects site.

I had to tweak the RPM spec file to accommodate build-system changes introduced in ESS 5.2.10. If you want to build from scratch, make sure you pull the most recent spec file from my site instead of using the one included in the 5.2.10 distribution, which is now out of date.

Posted in ,
no comments
no trackbacks
Reddit Delicious

Good stuff: Foyle's War

Posted by Tom Moertel Thu, 01 Sep 2005 19:20:00 GMT

“Reality” shows have plunged mainstream television into an entirely new depth of stupidity – and for television, that’s saying something. Fortunately for us, some programs defy the downward trend, and Masterpiece Theatre’s Foyle’s War is one of the best.

Read more...

Posted in , ,
1 comment
no trackbacks
Reddit Delicious