Fractional Bits (Part III)

18/10/2016

A long time ago, we discussed the problem of using fractions of bits to encode, jointly, values without wasting much bits. But we considered then only special cases, but what if we need to shift precisely by, say, half a bit?

But what does shifting by half a bit even means?

Read the rest of this entry »


Respace

19/04/2016

The two seemingly trivial and unimportant problems of what kind of whitespaces and how to use them are still not solved. Some still use hard-coded tabs in their source code, and because they set tabs to be two spaces wide in their favorite editor, they expect the rest of the planet to have done so. The result is that spacing will break in another person’s editor, and the code will look like it’s been written by a four years old. Also, when tabs and spaces are mixed, and randomly interpreted, the indentation, the general aspect of how the code is presented, is broken.

turbo-napkin

While marking assignments, I encountered a number of such pieces of code. So I decided to fix that with a simple Emacs command.

Read the rest of this entry »


LaTeXify C/C++ code snippets

26/01/2016

So I’m still writing lecture notes. This time, I need to include kind of larger pieces of C or C++ code, and \LaTeX environments do not really help me a lot. Some are better than others, but you still have to escape and fancify text yourself. This is laborious and error-prone, and is an obvious target for automation. A script of some sort. The task isn’t overly complicated: highlight keywords, and escape symbols like { } _ and & that make \LaTeX unhappy. This looks like a job for
sed.

Read the rest of this entry »


Search all your Bibtex files

12/01/2016

When I write papers or other things, I tend to create separate bib files, so that I don’t end with a giant unsearchable and unmaintainable blob. Moreover, topics tend to be transient, and the bibliography may or mayn’t be interesting in a few year’s time, so, if unused, it can safely sleep in a directory with the paper it’s attached to.

book_stack

But once in a while, I need one of those old references, and since they’re scatted just about everywhere… it may take a while to find them back. Unless you have a script. Scripts are nice.

Read the rest of this entry »


Single-Pointer Doubly-Linked Lists

08/12/2015

Old computer science books aren’t perceived as being of much use, since everything is so much better now. Of course, that’s not entirely true, especially when we are interested in the techniques used in the days where 32KB core memory was “a lot”. Leafing through one such book, Standish’s 1980 Data Structure Techniques, I found a method of maintaining doubly-linked lists using only one pointer. Let’s see how it works!

chain

Read the rest of this entry »


Rational approximations of π (Divertimento)

10/11/2015

While reading on the rather precise approximation 355/113 for π, I’ve wondered how many useful approximation we could find.

pi-pie

Read the rest of this entry »


Coding by ear

22/09/2015

I just got one of my computer’s fans repaired. It was still working, but noisy like a diesel engine. While I started savoring the relative quietness of my study again, I noticed that I lost something with that repair. I lost a source of information about what my computer is doing.

vroum-vroum

The fan speed—and noise—varied with the CPU temperature, which in turn varies with its clock speed and workload. The harder the machine works, the noisier it got. At first, you night think (correctly, for that matter) that having a noisy computer is annoying, but I think you can turn that to your advantage while coding.

Having an actual, noisy, grinding fan is an annoyance. But what if we paid attention to the noise our computer makes, use it as a development tool?

It’s easy to forget that a computer is just a machine like any other, and that an intensive computation, in addition of taking time, also consumes power, dissipates heat, and cause wear—especially when disk IO is used intensively. Therefore being aware of what our code does to our computer may make us better coders.

If you don’t benefit from a noisy fan, you can still use the machine’s sensors to keep you informed on what goes on in your box.

  • Sensors. You can use Linux’s sensors command to get a snapshot of your box’s health. The package you need to install varies from a distribution to another, on Debian-like it’s the package lm-sensors. Invoking sensors, maybe within a pretty-printing script, combined with watch (say, watch -n 1 sensors) will let you see what happens.
  • htop, iotop, and nethogs. htop is a prettier, configurable, version of top. It does the essential: processes, process trees, threads, etc. The iotop command needs to be run as root, but will show you what happens with disk IO. nethogs does the same thing for network bandwidth.
  • Conky and other system monitors. A good while ago, I wrote a piece on conky, a simple but highly configurable system monitor. It’s kind of old school (no rounding plasma 3D widgets) but gets the job done. I’ve integrated sensors to it so it also displays the CPU temperature and fan speeds.

*
* *

Listening to your computer doesn’t replace good software practices (good algorithms, profiling and hunting for hot spots) but complements them. If you hear your HD scratching a whole lot more that it should, you have gained a few bits of information—something unexpected is happening.

*
* *

In a way, that noisy fan helped me refine my code in more than one occasion. It reminded me that I should be coding for a Pentium III (something I think I advocated before), and that if my program was making my computer noisier, then it probably consumed more resources—CPU, IO, power—than it should have. Keeping my computer quiet forced me to write better, faster, more frugal code.

Keeping an eye on the machine exertion symptoms may help you write better code, especially when scaling is considered. If your program adds 20 degrees to your computer’s internal temperature, how will it scale to service a hundred time more requests?


King Solomon’s Bath

08/09/2015

In 1 Kings 7 (King James version), we read the description of Solomon’s molten sea:

And he made a molten sea, ten cubits from the one brim to the other: it was round all about, and his height was five cubits: and a line of thirty cubits did compass it round about.

bath

…which I take is some kind of bath. Superficially, it seems to state that \pi=3, since the circumference of a circle of diameter d given by \pi d=2\pi r. But what if “round about” doesn’t strictly means “perfect circle”?.

Read the rest of this entry »


Optimizing JPEG for bandwidth

01/09/2015

Optimizing web content is always complicated. On one hand, you want your users to have the best possible user experience, but on the other hand, you don’t really want to spend much bandwidth delivering the bits.

compteur-small

This week, let’s have a look at how we can optimize images for perceptual quality while minimizing bandwidth. While we could proceed by guesswork—fiddling the parameters until it kind of looks OK—or we can take 5 minutes to write a script that searches the parameter space for the best solution given a constraint, say, perceptual quality.

Read the rest of this entry »


Yet more LaTeX indexing woes

21/04/2015

Still more indexing woes. LaTeX indexing, while superficially easy to use, turns out to be rather finicky, especially for anchor placement—that is, where the index (hyper)link actually points to.

index-woes

Let’s see what I have found to circumvent some of the difficulties.

Read the rest of this entry »