I've been pretty far behind on feeds lately, which means catching up in binges, which means missing out on things that take some time to process and follow up on. luckily jauricchio always seems to be looking out for me.

take for example the Academia vs. Business comic from xkcd, clearly written with an academic's bias (as pointed out by Wil Shipley). I left it at that, completely skipping the tooltip, which referred to the value 0x5f375a86 as being special. luckily jauricchio caught it and looked it up: it was part of the fast computation of inverse square roots (used a lot in 3-D graphics) and was later revised to the current value, 0x5f3759df. the code, courtesy Wikipedia:

float InvSqrt (float x)
{
    float xhalf = 0.5f*x;
    int i = *(int*)&x;
    i = 0x5f3759df - (i>>1);
    x = *(float*)&i;
    return x*(1.5f - xhalf*x*x);
}
the Wikipedia page also contains the math that isolated the correct magic number.

I love hacks like this.


I just let teleport update itself on my powerbook 12", and this is what I got:

sigh. why did you even prompt me to upgrade?

I'd be more stoic if this was my first time running into something so obviously broken like this, but let me take you back to July of 2007 when Mac OS X 10.5 was converging, and I was using DTrace as part of a project…


this blog generates its pages by shell script, and it's been a bit of a challenge to make the engine portable, so generated content and the code itself can be run anywhere.

one way this is made easier is with the <base href="$path" /> tag, which tells the user agent to prepend every source and reference with $path. as I developed on Safari, everything worked.

then I asked for some friends to look at it, and it turned out that the base tag doesn't quite work like I'd hoped: you can't use a relative path as your base href, and firefox enforces this strictly.

there's two options for fixing this: the blog engine itself has to know where it lives on the server, so it can populate the base href correctly, making generated content less portable. alternately, the output generation could be smarter and append the relative base href to the beginning of every source and reference it sees, making the output code messier.

luckily, if you're ok with requiring your clients to support javascript (which I am), there's a third option. behold:

<script type="text/javascript">
    document.write('<base href="' +
        document.baseURI.substring(0, document.baseURI.lastIndexOf('/')) +
        '<?= $blogroot ?>" />');
</script>

put that in the beginning of your <head> and it will emit the correct absolute base href at runtime.

thanks to Christian Hammond for the code and debugging help.

edited on thursday september 10th, 2009 at 22:38 at 11:58 :

as usual, this doesn't work everywhere. notably: Opera, IE, and Konqueror, all versions. I ask myself "do I care?" and respond "no."

edited on friday february 19th, 2010 at 21:38 at 11:58 :

this blog no longer uses this hack, because base hrefs were interfering with footnotes. were it up to me I would still use this. because it is great.

the PHP library makes me sad sometimes, like earlier tonight.

I've been working on the blog engine, trying to figure out how to get various bits of markup working that I don't want to have to write by hand every post1, and it requires doing XML manipulation in PHP.

if you take the time to look around on the internet (and I did), you'll see a lot of people who want to manipulate HTML or XML in PHP, and all the replies recommend things like SimpleXML, which can't remove or change tags, or DOM, which also didn't work for my needs2. exhausting these options, every thread ends with "use str_replace" or "use preg_replace". sigh. what's the point in having these libraries if they're not actually useful?

I want to do this right, damnit, and I'm going to use a tool that parses my pseudo-HTML fragments into a tree and allows me to add, change, or remove tags at will!

luckily, as I was resigning myself to writing a library from scratch, I discovered simplehtmldom. unlike other libraries I've tried using recently, simplehtmldom worked right out of the box with no issues whatsoever. code example follows.

1 especially footnotes
2 DOM only accepts properly formed XML or HTML with an all-containing root node and will only create output with a doctype and a single root node. I wanted something that would create output as close as possible to the input.

cute snippet for checking if a number is a power of 2:

n & n - 1 == 0 && n != 0