January 6, 2010
SDL and OCaml

In case you’re a minority like me and are working with OCaml (I don’t know why, but somehow I doubt that) and thinking about trying to play with OpenGL, you may be interested to know that I have forked glcaml project and put that on the GitHub: http://github.com/einars/glcaml/

Glcaml provides an interface to OpenGL and SDL libraries for OCaml in a nice cross-platform-compatible way. Until now, it didn’t support SDL_mixer (and some other SDL_* libraries), thus playing sounds and music was quite difficult. No more! — as I’ve added support for the mixer, and more changes will come, when my need arises.

For your convenience, there’s an example of using the mixer interface available, showing various features of the library.

And, most importantly: Have fun!

December 23, 2009
The New Turing Omnibus: A chapter on Gödel’s Theorem

http://bugpipe.org/misc/goedel.txt

This is a chapter which deals with the Gödel’s Theorem from the A. K. Dewdney’s most wonderful book “The New Turing Omnibus”.

I can’t recommend this book enough, the insight it gives into various topics through its short articles makes me feel omnipotent. I hope that the publishers won’t find^W object me publishing one of the articles.

Fun fact: even though it’s XXI century, I typed the article in manually so that the owner of the book — who found a sudden surge of interest for this exact article — would let me keep it for a longer while.

November 12, 2009
In defense of slower calculations

…or 2^64 should be enough for everyone.

Floating points

    % python
    >>> 2.5 - 0.1
    2.39999999999999

The usual answer to complaints about such behaviour is mumbling about standards, traditional numerical representations and things like that. That’s a cop out. It’s about time it would be treated as a bug.

Nowadays computers are insanely fast and have huge amounts of memory. Still, it’s surprisingly hard to tell that by number “2,5” I really mean discrete 2,5. IEEE-754 is a premature optimization, and I feel that there is a need to search for new real number representations.

There are algorithms, CPU support and long history of working with traditional, highly inexact floating points, and thus the calculations are very fast. Yet, as soon as you’d like to sacrifice some speed for the correctness, it turns out that you’re on your own. Multiplying 123456789 with 987654321 is possible, but as soon as it’s 123456789.01, you’re welcome to the land of huge errors and approximations.

Platform-specific integer representations

Even google’s new language Go is short-sighted with having fixed-size integers, forcing you to do the computer’s job of cherry-picking how much memory you’ll allocate to each of your integers. Will it be 8 bits, 16 bits, or maybe 32 bits? Oh, and then there are platform-dependant integers, which will work nicely on my 64-bit machine, but will overflow on your 32-bit, and no tests will catch that. It’s 2009 already! Just use for the integers as much you need!

Integer overflows

As if it wouldn’t be bad enough to pick some integer size and stick with it, it’s worse when it silently overflows. Ocaml, for example, with its emphasys of speed and type correctness allows you shoot in the foot:

    % ocaml
    # 9876543210 * 9876543210;;
    - : int = -3910986626405429788

I would really have expected at least throwing an exception.

Bottom line

C, C++ and their ways of doing things won’t go anywhere. But for the new languages there’s a wondeful chance to question the stagnant traditions, not sacrificing the correctness of the results for some speed.

November 3, 2009
Okular, one of the best document viewers around

If you’re somewhat like me and read alot of books or technical documentation in pdf or djvu format, then I’d like to suggest you checking out the Okular document viewer. It is a part of KDE project, and it has at least two excellent features which make it well worth setting it as the default document viewer.

Review mode

Review mode is a bunch of tools that allow you to make the notes and scribbles inside the document you’re reading. The following picture shows the document with a custom note and review toolbar visible:

The notes themselves are saved transparently and independently from the document you’re viewing (under ~/.kde4/share/apps/okular) and they won’t mess with the originals — unlike e.g Foxit reader for windows, which insists on saving the file with the notes into a new pdf.

Trimmed margins

When reading documents from the monitor, the large margins that look nice when printed tend to get in the way and waste space. This feature removes them, making continuous reading easier, like this, original with margins visible:

And the trimmed margin view:

These little things make working with the pdfs and reading from the screen much more enjoyable. Try it out — it should be easily found in the software repositories of your OS.

P.S The scary pictures come from the free book Introduction to Statistical Thought by Michael Lavine.

P.P.S I would really love to hear about how cool the pdf readers are under mac. While I’m an avid linux user, I’m very interested on how things are done in other systems.

October 31, 2009
Custom KDE–style window shortcuts in Gnome

http://github.com/einars/dotfiles/blob/master/bin/focus-or-run

KDE, both the good old 3.5 and new clunky 4, has vast options for customizing the window behaviour. For example, you can ask the chat windows to automatically open in one workspace, gimp — in other, and so on.

When using Gnome, the feature I’m missing most is a possibility to assign window shortcuts: it is incredibly convenient to get rid of the alt-tab and use shortcuts like Alt-A to switch to firefox from anywhere.

I’ll show how to emulate this using a custom script in Gnome, and even make it better, opening the application in question if it’s not running.

You’ll need:

  1. a folder where you store all your own scripts. If you’re using linux and working from console, you’ve probably already figured out that you need a place to drop all your scripts and helpers and whatnot. Usually ~/bin is used — just make a folder “bin” in your home folder, if you don’t have any.

  2. wmctrl. Wmctrl is a small script which allows other scripts to communicate with windows on screen and control them. It is available in stock ubuntu repository (universe) as “wmctrl”, the same in Arch Linux community repository, and generally you shouldn’t have problems finding and installing it.

  3. zsh. Zsh is an interpreter which will run our script. The default shell interpreter in most of the linux distributions is called bash, yet zsh is easier to use and offers prettier scripting capabilities. There is no reason why the script couldn’t be written in bash or other interpreter of choice: I just use zsh for everything myself.

The script

You can view and dowload the complete script here: http://github.com/einars/dotfiles/blob/master/bin/focus-or-run

It is called “focus-or-run” and it takes two parameters: the first is window class or a fragment of the title. Script will try to find the matching window and bring it to foreground.

The second is an optional program name you wish to run in case the window was not found.

So, this command:

focus-or-run firefox /usr/bin/firefox

will attempt to find a window which belongs to “firefox” and, if found, focus that. If the window was not found, it will run /usr/bin/firefox and then focus the window again.

focus-or-run firefox

Will actually work just fine, too.

Assigning the hotkeys

This is where the System-Preferences-Keyboard Shortcuts applet comes in handy. Some time ago it was reworked so that adding custom shortcuts now is a breeze.

First, you have to create a new action by clicking Add, and filling in the information like this:

Adding a custom shortcut

You’ll find the new command in the list below; you can now assign any keyboard shortcut to it:

Created a new shortcut

That’s all. Now you can press Alt-A anytime you want to get to that firefox window, and it’ll pop up.

I’ll leave assigning other most frequently used software to shortcuts as a home exercise :)