Logarithmic Cabins

So here’s another fun thing I encountered lately.

So a side project of mine that I’ve been working on is a most probable number calculator iOS app. What, pray tell, is a most probable number calculator, you may ask? Well, if you have to ask, it’s certainly not a product for you now, is it? But for those of you that are curious, MPNs are used in certain laboratory experiments involving serial dilutions and detecting bacterial colonies, blah blah blah. It’s all incredibly dry stuff, and honestly it’s not something I ever made use of when I was in the pharmaceutical research field, so I’m only moderately clued-in to its uses.

But the bottom line is that this calculation is a thing that is used, and while there’s a dedicated desktop application written when Windows XP was the hot new shit and some Excel templates floating around, there’s no dedicated app to this. Having it on a mobile device would just be a handy thing to have, rather than having to go back and forth between your computer and your work, or–God forbid–doing it by hand.

So I made one.

One of the great things about coding is that math functions are just pretty much baked into modern programming languages. So given an Excel spreadsheet that has the equations and calculations, it should just be a matter of translating that syntax into, in this case, Swift 3 and swapping out cell references for variables. That was simple enough.

Except, God damn it all, it wasn’t giving me the right answers.

The test case that I was using for dummy data at one point has the following equation:

-(1/0.01)*log((3-1)/3) Excel spits out 17.609 as a result, which is also what you get if you just cut and paste the equation into Google’s search bar. Nothing too terribly complicated here, it’s maybe high school-level at best? If you have a scientific calculator handy, it’s literally only a few button presses.

So why the fucking fuck was Swift telling me the answer was 40.456?

I mean, even allowing the possibility that mathematics as we know it might not be a universal constantsurely we can at least assume that it is at a localized scale? Something was rotten in the state of… some sort of programming-related Denmark pun or something.

What? I can’t be “on” all the time.

Fortunately with a little bit of consultation with the more mathematically inclined, I was able to figure out the problem. See, when you invoke a “log” function in Excel, Google search, or every god damn calculator that has a fucking “log” button, the default is log base 10. But oh ho ho, not in Swift! In Swift, it turns out that a naked “log” is actually finding the natural log of that number, or log base e

Now a natural log function in Excel, Google search, and every god damn calculator that has a fucking natural log button, is typically designated with an “ln.” Which, honestly doesn’t seem like it would make as much sense as “nl,” but that’s neither here nor there. The point is, there’s kind of a wide consensus on how to designate this mathematical concept, but Swift bucks the trend for reasons.

Okay, this isn’t actually even limited to Swift, as this seems to be a “feature” of most programming languages in use these days. Possibly because it was a feature of C, which is the Great Satan from which the Little Satans spring, and we’re dealing with a decades’ long questionable decision. I mean, I’m pretty sure Assembly used “ln,” so it’s not as though this is without precedent.

Okay fine, it’s not even limited to programming languages, as it turns out that there are some scientific and mathematical contexts where this is the case too, and I just generally look like a loud-mouthed idiot for writing a rather lengthy blog post ranting about it. Chalk this one up to my own naivete and general ignorance on this particular subject, but this strikes me as something a little more significant than the sort of confusion that might be caused by the British referring to cookies as biscuits. This seems more akin to randomly having some instances of an operator like “*” suddenly means “factorial” instead of “multiply.”

Pages: 1 2