If you really want your values in the correct range

The other day I noticed a subtle bug in one of my Java visualization prototypes. I somehow got NaNs everywhere, where there should absolutely be none of them.
The math I am using is correct, so I had to find the place where the NaN was produced.
I would really like it if the whole application just blows up into my face when it tries to calculate something with NaN instead of a usable value, but anyway, I had to trace the NaNs to its origin and after some debugging I found it.

Turns out Math.acos (which I was using) has the following in its javadoc:

If the argument is NaN or its absolute value is greater than 1, then the result is NaN.

Duh. Better if I had read the javadoc before blindly throwing my values at the function, because, it turns out, I was passing a 1.000000000002 at some time, for which I got rewarded with NaN.

So, for a quick fix, I decided to ensure that my values are in the correct range of -1.0 to 1.0.

Instead of writing something horrible like

if (val > 1.0)  
    return 1.0;
if (val < -1.0)  
    return -1.0;

Leo gave me a much simpler and nicer looking solution:

return Math.max(Math.min(value), 1.0), -1.0);  

These kind of floating-point errors are really nasty and I should probably take the time for reading "What Every Computer Scientist Should Know About Floating-Point Arithmetic" (Goldberg, 1991), finally.



I am writing these a-programmers-diary posts, for several reasons (at leats these are the ones to bring me starting this with this post).
The first one is, that I want to use this blog more for some kind of diary while programming. So do not assume that this will always be the best solution I am posting. Normally this is just the one I found working and seemed to me not as a crime against humanity.
Another reason is, that I sometimes think 'Oh, I had this problem before, how do I solve this'. So I can use these posts for some kind of reference.
The third reasons is that maybe someone reads this and finds a better solution or spots any mistakes I am making. If you do so feel free to correct me, I really like to learn. Just contact me via mail or twitter and I will add your suggestions as an update to the post, so it stays with the text of the post and does not get lost like outsourced comments like to do.

Location-aware Functional Keys

The problem

The problem with the Apple Media Keys is that they hide the standard function keys. This is great for media consumption, as I can adjust the display brightness or sound volume with just one hand.
However in Eclipse, the current IDE I spend most of my working hours in, the F-keys are mapped to often used actions, such as debug stepping and running code. This is great, as I can debug my code step by step with just a few keys with my eyes staying on the display.

The solution

OS X provides a setting for either one of the two modes directly accessible without touching the Fn key, but this is tedious doing it everytime I switch from home to work or vice versa.

Enter ControlPlane. This app lets you do Actions based on your Context. For instance, most of my Contexts are activated, when my MacBook is connected to a certain monitor or sees a certain WiFi network, but other Evidence Sources are also possible.

The missing link between the automatically detected environment (work or home) and the correct setting (media keys or function keys) is the often neglected, because miserably documented, AppleScript.

I found a script for toggling the setting in a forum post. I took it and created two separate scripts, one for activating the setting (at work) and one for deactivating (at home).

Next I created a Shell Execution Action for each of the scripts (osascript path/to/script), one when entering the work Context, and one when leaving.

But wait! We are not done yet.

Mavericks introduced fine-granular settings for Accessibility. Each Accessibility feature has to be enabled for specific apps, when Apps request those features. But ControlPlane does not request the feature to script UI elements, so it does not get added to the list, thus our AppleScript execution fails. Giving the AppleScript Editor the rights is not enough, because when ControlPlane is executing the script the AppleScript Editor is no longer the executing app.

So, how to get the some executing app listed there?

The trick is to wrap both scripts in an Automator app; and so I wrapped both scripts and told ControlPlane to execute both apps and hide them (just to be sure I do not see them executing).

I put the code and the basic instructions into a gist:

Migrating a private repository from GitHub to Bitbucket

Today my GitHub student coupon expires, which gave me the Micro plan for free for two years.

Everything I use private repositories for, currently, is storing my bachelors thesis and occasionally collaborating with a friend.

Bitbucket offers unlimited private repositories with up to five collaborators for free, without applying for a student coupon. So that's exactly what I need right now.

To migrate my private repositories from GitHub to Bitbucket, I used the GitHub Importer Page on Bitbucket.

To migrate all my Issues I had in the GitHub repository, I used the great gem git2bit. Pro-tip: use a Personal Access Token to authenticate the gem with your GitHub Account, unfortunately Bitbucket does not have these currently.

Update 25.11.2013

After applying for two additional years of student discount, GitHub immediately granted my request.
Blimey, I did not leave an empty private repo there to watch what happens with it in the time I could not have had private repositories.

Hello World!

puts 'Hello World!'
echo 'Hello World';
System.out.println("Hello World!");
LOGGER.info("Hello World!");

This seems like a really nice environment. Not fond of the database backend, but we'll see how this compares to my previous content files in a git repository approach.

One advantage is the editor (especially for mobile postings). It is nicer than having to git push everything to the server (toto) or having a built environment for the site (jekyll/prose.io).