16 September 2007

Floating

It's not until you study math a little bit that you realize just how awful floating-point arithmetic is. I mean, so it's a little inexact. So what? But the following are examples of statements that are absolutely true for integers, rationals, and reals, but not for floating-point numbers (even setting aside floating-point infinities and “not-a-number”s):

a - (a - b) = b

If b > 0, then a + b > a.

The associative law: (a + b) + c = a + (b + c)

The distributive law: a × (b + c) = (a × b) + (a × c)

Cancellation: If a × b = a × c and a ≠ 0, then b = c.

In short, if you've ever done any reasoning about floating-point numbers, you were probably wrong.

It's easy to come up with formulas that floating-point arithmetic gets seriously wrong, especially for numbers very close to zero.

The inexactness is impossible to track in a useful way, so you never know just how bad the result is. Typically you just assume it's almost right until you find out it's wrong. The only surprising thing is that it works so often.

26 August 2007

Where to eat in Nashua, NH

Every town has something just a little different to offer. Here are the places I would recommend if you had a day to spend in Nashua, New Hampshire, U.S.A.

Breakfast: The City Room Cafe. Try the Cajun sirloin crepes. (They're not what I expected; they should be called "crepes curry" and they're a treat.) Check out the specials; they're usually inventive and scrumptious. Or have a sausage-apple-cheddar omelette.

Lunch: Nashua Garden, a pub on Main Street, for the best sandwiches in town. Order a "Big Poppy", loaded, plus hummus and roasted red peppers.

Dinner: Brazilian BBQ on Main Street. If you've never had Brazilian barbecue before (I hadn't), this is a must. Alternatively, Lilac Blossom serves great Chinese. The Crispy Whole Fish is not to be missed. Lamb in Two Styles is almost as good, but is made of fluffy, innocent baby sheep, alas.

Now, if you spurn variety entirely, instead have a nice, conventional breakfast at Hollis Country Kitchen (way out west), lunch at Nashua House of Pizza (just a pizza place, but unusually good), and dinner at Michael Timothy's on Main Street.

P.S. Adventurous or not, if it happens to be Sunday morning when the urge strikes you, have a go at Michael Timothy's weekly jazz brunch. It features live music, an excellent buffet, roast beast and omelettes each to order, and a better grade of coffee. Desserts are included in the price of adm. Singularly pleasant.

17 July 2007

Too cool for screenshots?

Mac OS X comes with a command-line utility, pbcopy, that copies its stdin to the clipboard. pbpaste dumps the clipboard to stdout.

13 July 2007

Usability gripes

I wonder if I'm the only one who finds it ironic that LDAP, of all the Internet protocols the one most directly aimed at fixing the problem of discoverability, is, itself, undiscoverable:

I happen to know the hostname of my company's LDAP server, so I only need to figure out 7 more settings before I can, you know, find out my colleagues' email addresses.

Uploading this screenshot was an ordeal, by the way. Mac OS has screen-capture hotkeys (Command-Alt-Shift-3 or something absurd like that), but it can't capture just one window. It also comes with a separate screen-capture program (Grab)... which unhelpfully saves your screenshots in TIFF format. (There is no way to configure this.) And Mac OS does not come with any basic image-hacking utility like Paint. Great style, awful usability.

While I'm on a roll:

$ nslookup
> help
The 'help' command is not yet implemented.

Oh, thank you. Thank you so much.

Update: Some great Mac OS screenshot tips are in the comments. (Thank you!)

11 July 2007

Overheard in a car

Buzz (not his real name) is 3; Woody is 22 months.
...
Buzz:  Yes you are!
Woody:  I'm not a are!
Buzz:  Yes you are!
Woody:  I'm not a are!
...

06 July 2007

C puzzle

I was reading source code this morning and came across this curious line in a header file:

JS_STATIC_ASSERT(sizeof(JSGCThing) >= sizeof(JSString));

The effect of this macro is to check at compile time that sizeof(JSGCThing) >= sizeof(JSString). If the condition is not met, compilation fails with an error.

The puzzle is: how does this work? A hint is in the comments.

04 July 2007

Things die

Around seven thousand languages are spoken today. Many are spoken only by a few elderly people. Over 500 such languages are headed for extinction.

Some linguists go much further, claiming that more than half of the languages spoken in 2000 will be extinct by 2100.

24 June 2007

Math links

A mathematician's apology (PDF). The early chapters are mostly grumbling about getting old. But then he starts talking about mathematics, with real poetry and love for the subject. Very nice.

Eudoxus. This ancient Greek anticipated Dedekind cuts and calculus. He also mapped the heavens and the earth. Yet no work of his survives--only frequent citations in the works of Euclid, Aristotle, Hipparchus, and more.

Triangle centers. Every triangle has a centroid. But oddly enough, that isn't the only center of a triangle. MathWorld has a picture showing over a thousand of them.

To do

Frivolous projects I would undertake, if I only had the time.
  • Submit a patch to Monotone that makes "mtn log" display log entries in chronological order.

  • Finish debugging the new, faster syntax-case implementation I have for Try Scheme. (sigh) It's so close!

  • Write about continuations.

    A good tutorial would be helpful. I haven't read anything I really liked. (Squawk of the Parrot has some very nice stuff on this.) I think a good approach would be to say up front that it's extremely generalized and hard to explain, but that we're going to work toward the definition. Then lean heavily on examples and analogies. Compare continuations to all kinds of control-flow concepts: functions; goto; return and break as functions; savepoints in video games; exceptions; generators; longjmp. Explain that all of these can be implemented fairly easily using call/cc. Then talk about continuation-passing style.

    I also want to investigate just what abstraction boundaries are violated by first-class continuations in a language that has state. I am kind of down on continuations as a language feature at the moment.

  • Read about continuations. In particular, Andrzej Filinski's thesis introducing the symmetric lambda calculus.

21 June 2007

Rooftop lab

I just started a new blog for my Mozilla work. It's called Rooftop Lab, and it'll be pretty technical.

19 June 2007

On the Cape

If you happen to be on Cape Cod sometime, visit Scargo Pottery, just off route 6A. That whole stretch of 6A is like a sprawling crafts fair, but Scargo is something special. The little pictures on their web site don't do it justice.

We were on Cape Cod for my birthday. It's a few weeks yet before the tourist season, but the weather was cooperative. Breezy, cool, sunny. Buzz was on his best behavior all day.

First we went to the Cape Cod Potato Chip factory. There's not much there, but you get to see a real factory floor. And eat potato chips. So.

Then we went to a little clock shop on Route 6A. Buzz was entranced. We talked about the clocks for, I don't know, it must have been half an hour. His favorite part was the clocks that had their faces removed so you could see the works.

After that, we went back to the beach, flew kites, climbed around on some rocks, and cooked out. A lovely day. If kites hate you, what you want is a Pocket Parafoil (here's one on eBay). Fun and ridiculously easy.

17 June 2007

Strawberry alert

If you happen to live in Nashua, NH, there's a small farmer's market on the Main Street bridge over the Nashua River. It's there every Sunday, all summer. A booth there is selling strawberries that were picked this morning.

15 June 2007

Woody goes to the park

A. is 22 months old now and insists on being called “Woody”, except when she's “Steve”.

Mommy: What did you do at the park? Did you slide down the slide?
Woody: Yeah.
Mommy: Did you run around?
Woody: Yeah.
Mommy: Did you meet some other kids?
Woody: Yeah.
Mommy: Did you eat some blueberries?
Woody: Yeah.
Mommy: Did you see a zebra?
Woody: Yeah.

08 June 2007

Productivity through eccentricity

From The Pmarca Guide to Personal Productivity (click the link, it's interesting):

[R]efuse to commit to meetings, appointments, or activities at any set time in any future day.

As a result, you can always work on whatever is most important or most interesting, at any time.

Want to spend all day writing a research report? Do it!

Want to spend all day coding? Do it!

Want to spend all day at the cafe down the street reading a book on personal productivity? Do it!

When someone emails or calls to say, “Let's meet on Tuesday at 3”, the appropriate response is: “I'm not keeping a schedule for 2007, so I can't commit to that, but give me a call on Tuesday at 2:45 and if I'm available, I'll meet with you.”

Or, if it's important, say, “You know what, let's meet right now.”

Clearly this only works if you can get away with it. If you have a structured job, a structured job environment, or you're a CEO, it will be hard to pull off.

But if you can do it, it's really liberating, and will lead to far higher productivity than almost any other tactic you can try.

There's more; if this bit appeals to you at all, the whole thing is worth reading.

By odd coincidence, pmarca is Marc Andreessen, cofounder of Netscape.

Credit: Marginal Revolution.

05 June 2007

Danger!

In Scheme and Ruby, procedures that modify variables or data structures are marked with a ! by convention:

  // Java
  static <T>
  void arraySwap(T[] arr, int i0, int i1) {
      T tmp = arr[i0];
      arr[i0] = arr[i1];
      arr[i1] = tmp;
  }

  ;; equivalent Scheme
  (define (vector-swap! v i0 i1)
    (let ((tmp (vector-ref v i0)))
      (vector-set! v i0 (vector-ref v i1))
      (vector-set! v i1 tmp)))

What's the point of this convention? Well, Scheme thinks this kind of behavior is slightly dangerous and needs to be highlighted. A little explanation:

If your variables or data structures can be changed, expressions have different values in different places within a program. The order of operations matters. The order of lines within a procedure matters. Procedures can subtly change global data as a side effect, and this can lead to rather subtle (mis)behavior.

Theorists call this language feature state. The word is intended in the sense of status, as in “the state of affairs” or “state of the Union”. C, C++, Java, Perl, Python, Ruby, and so on are stateful languages.

Maybe it sounds kind of, er, sweeping, or maybe absurd and stupid, to say that state is bad. But you already know this is true, at least sometimes: Global variables = bad. Unobvious side effects = bad. Leaving an operation half-finished when you throw an exception = bad (usually). Global state + threads = migraines.*

But what's the alternative? Well, there are alternatives, but it's beyond the scope of a quick blog post. Just like the boundary between static-typed and dynamic-typed code, the boundary between stateful and pure-functional code is getting a lot of attention these days. Interesting times.

What Scheme gets wrong here is that state-related bugs don't necessarily happen at the places where data is modified. In fact, the whole idea of marking the danger spots is a total whiff. The problem with state is that it permeates your program. The danger is everywhere a stateful variable or data structure is used, whether for read or write, directly or indirectly.

*It's not just that there are race conditions and potential crashes here. You can eliminate that with a single global lock. The problem is that a single global lock often isn't fine-grained enough; it creates a bottleneck. So you go to a fine-grained locking scheme, and that's where the migraines come in.