I lost my mind and …

… coded a recursive parallel FBP mergesort, in bash using named pipes:

http://sam.ai.ki/code/mergesort/mergesort.sh

Also, and more usefully, I learned how to read a line correctly in bash:

IFS= read -r line

Without -r, it processes escaped characters such as \t and \n!

Without IFS=, it strips leading space.

All in all, another couple reasons not to write code in bash!  haha

Posted in Uncategorized | Leave a comment

making GNU screen, bash and vim work with a new X server

I want to keep GNU screen, with bash and vim, running nicely
when I start a new X server and the DISPLAY number changes.
(We are using Xming on Windows with a Linux server on the LAN).

There’s no ‘nice’ way to do this, as far as I can tell.

My hacky solution reloads DISPLAY from a file each time you press enter in bash,
and provides a :R = Restart() command in vim, so I can easily restart vim
with a new DISPLAY, but preserving the current session (files, windows, etc.).
It also provides a default –servername (process ID).

I’m somewhat embarrassed to post this!

details here:  http://sam.ai.ki/survive-X-crash.txt

Posted in Uncategorized | Leave a comment

Five Yoga Exercises for a very healthy back

Do you have back trouble or discomfort?
Would you like to try some simple and quick daily exercises?

Please take a look at my little mashup webpage,
with 5 quick and simple yoga exercises.

        http://sam.ai.ki/yoga/

These exercises can give you a happy back,
and maybe even eternal youth!

Feel young, happy and healthy as long as you live,
that’s not a bad outcome, for 5 minutes a day.

Posted in Uncategorized | Leave a comment

rounding and formatting with printf

I’ve committed and seen several rounding / formatting bugs this week, so I had a look at how to do this right.  It very important to do rounding and formatting correctly when dealing with money.

The problem is that floats and doubles represent most decimal fractions imprecisely,
the wrong sort of rounding can turn 0.999999 cents into 0 cents:

> printf “%.30f”, 0.7
0.699999999999999999989157978275

It turns out that good old printf/sprintf/snprintf is perhaps the best way to round and format floats.  The formats work much the same in C, perl, sh, python, php, etc, which is nice.  `man 3 printf` for all the gory details, but it’s not much fun reading it!

An example, to multiply an amount by 100 (dollars to cents) and print it rounded to the nearest cent, right-justified and 0 padded to 15 chars, the following line of perl works nicely:

> printf “%015.0f\n”, 111.2399 * 100;
000000000011124

Too easy, really!

Breaking it down:
%f -  print a float
%.0f – … with 0 decimal places, rounds to the nearest int
%15.0f – … right aligned and padded with spaces in a 15 character field
%015.0f – … padded with 0 digits

You can even specify the field width as an argument:

> $width = 10;
> printf “%0*.0f\n”, $width, 111.2399 * 100;
0000011124

Here ends the lecture on printf.  :)

P.S. other good rounding functions in C are C99′s round(3) and floor(3).  In C89, you can round to nearest with floor(f + 0.5).  Don’t use (int)(f + 0.5), it behaves differently for negative numbers, and don’t use printf(“%d”, f) to round either!

Posted in Uncategorized | Leave a comment

Tuning Firefox for Speed – enable HTTP Pipelining, etc.

The Internet is amazing, if you have a server in the same country everything seems to happen almost instantly… but with many servers overseas, it’s good to reduce the number of TCP round trips needed to view your web pages.

Web page authors can do all sorts of hacks to make things load faster (e.g.  check out Y-slow), but you can get great improvements just by tweaking your browser settings. Here’s how to do that with Firefox:

essential – HTTP pipelining:

1. Enter ‘about:config’ in the URL bar, and click ‘I’ll be careful’.
2. Search for ‘pipe’.
3. Enable all the pipelining options, if not already.
4. Set ‘network.http.pipelining.maxrequests’ to 100 or some large number.

already – HTTP keep-alive:

5. Search for ‘alive’.
6. Enable all the keep-alive options, if not already (they should be).
7. You can increase ‘network.http.keep-alive.timeout’, but not much need.

optional – paint delay, max connections:

8. Right click, new → integer, name ‘nglayout.initialpaint.delay’, value 0.
9. Search for ‘connections’.
10. You can increase these four max-connections values, but not much need.

All these speed hacks are well-known, but the top-ranking page on Google for
‘firefox pipelining’ was an incorrect blog post by someone who had completely
misunderstood what HTTP pipelining is.  So maybe it’s worth explaining it.

Pipelining just means your browser will send all (or many) outstanding requests
at once, through a single channel; rather than the normal slow cycle:
request 1, read response 1, request 2, read response 2…  So, pipelining
avoids a lot of unnecessary latency, with no need to open parallel connections.

I wrote a somewhat longer page about this, here http://sam.ai.ki/pipelining.html

I hope that this short advice, with correct explanations, will help you enjoy
a better browsing experience with Firefox!  If you have some correction for me,
or other browsing speed tips, please let me know and I’ll update my page.

Posted in Uncategorized | 1 Comment

Ronald Jenkees is the 21st century god of music

Maybe you like music?  Then, go listen to Ronald Jenkees’ superb music on this youtube playlist, and go support indie music and buy his CDs at his homepage.

Stay Crunchy, Y’all  :)

Posted in Uncategorized | Leave a comment

> “…a STANDARD for the storage of contacts…”

(Is what for which they cried out, in the long dark night of IT bogosity. Then a clarifying light came, and a voice replied… “This is the way, this is the fresh light of day, this is how K&R would do it…”)

May I, as a seasoned (if not veteran) coder, please propose a sensible one – a standard for the storage of contacts – that we might promote into the limelight… and ever after be fondly remembered (if not loudly celebrated) by hackers everywhere, for having done so?

Plain, Simple, “key: value” text files, like apt and dpkg lists, email and http headers, with records delimited by a blank line.  This is the best file format for nearly everything.  Not CSV, not SEXPS, not gzipped XML (spit), not binary bitblob monstrosities.  Simple records of key value pairs.

We use this format for HTTP and Email headers, and for your favourite Linux Distros’ package lists, etc., etc., because it’s very good: it’s human readable/writable, and it’s ultra simple.  Even a mouse can understand it, with a little coaching.  It’s still sadly underused.

Look, something like this:

Name: Fred Nurk
Address: 1234 Numpy St
  Worthingthwaite
  UK 123545
Phone: 012345678
Phone: 564387347
Email: fred@nurk.foo
Email: frednurk99123412@gmail.com

Name: Emily Jane Nurk
Address: 256 Bitwise St
  Camelhide
  Australia
  3243

Is it simple?  It is simplest.
Is it good?  It is good.
Comprehensive too?  Verily So!

I’m sure you all get the idea.  If not, I can specify the simplest and preferable variant in meticulous detail within 1/4 page less flowery / befuddled prose.  You can even store binary values in it!  (if don’t mind the long lines).  A double blank line may mark the EOF, can be useful.

You can put all the data from a whole RDB in one of these files: just add a “table: foo” line, or I prefer “type: foo” to each.  It’s in fact more flexible than a set of fixed-width tables, more like an LDAP directory.  If you’re worried about the disk used by repeated key names, and “: ” tokens, compress the file.  You’ll find it turns out much much smaller than a normal binary database, probably even when you compress that too.

A longer (615K) example file, formatted by me, loads in 0.13 sec on my slow netbook: http://sam.ai.ki/code/talaga/dict   Maybe no ‘advanced’ features such as repeated keys or multi-line value there.

To use ANY other file format for most any sort of records is bare-naked insanity, for this is the file format of the Gods, simplicity itself: it’s exactly what any sensible (English speaking) Human would write on paper for an address (well, we might skip the Keys since we have more brain).  It’s trivial for computer programs to parse and generate it (10 lines of C?), it’s completely generic, and – thank the Gods especially for this – it’s not XML.  Your IRL dead-tree address book looks like this too.

For indexing, if needed, we can easily enough write a ‘textmap’ program that for a file “addresses.txt” will create “addresses.ix” its index.  The index could also be a text file, or a binary file, but with fixed width fields.  The well-esteemed Postfix does similar for its text databases with the ‘postmap’ program.

If you are concerned about complete index rebuilds, please don’t be, at least not for this application.  Indeed this also can be solved by using padding within the main database text file, but we lose some simplicity then.

Go forth, spread the word, join the revolution, baptise those confused hackers in the name of that great god Simplicity.  Thx for reading anyway.  Comments here, as usual, are published only at my whim, so be polite, smart and unscathing… and they’ll likely reach the WWW.  If not (polite, …), there’s no’ a chance (… WWW).  This is my personal blog (somewhat like a home).  It’s not some flame-festfully bogus unmoderated web forum, email list, IRC channel, or netnews group.  Be kind to me.

Have a nice day.  :)

Posted in Uncategorized | Leave a comment

US bombing Libya? So, oil?

Just on a hunch I googled “Libya oil”:

“Oil reserves in Libya are the largest in Africa and the ninth largest in the world.”

No surprise at all.

map of global oil resources, aka targets

The US does not bomb people in order to help them politically, nor to protect US civilians from “terrorism”. Civilians are not a valuable commodity. They do bomb people to get oil, aka “the black gold”, and for no other reason in recent history as far as I can tell.

“The Afghanistan oil pipeline project was finally able to proceed in May 2002. This could not have happened if America had not taken military action to replace the government in Afghanistan.”

“President Bush’s Cabinet agreed in April 2001 that ‘Iraq remains a destabilising influence to the flow of oil to international markets from the Middle East’ and because this is an unacceptable risk to the US ‘military intervention’ is necessary.”

I must be getting really paranoid. I googled “Vietnam oil” too, and found these:

“Herbert Hoover, later to become President of the United States did a study that showed that one of the world’s largest oil fields ran along the coast of the South China Sea right off French Indo-China, now known as Vietnam. – Denny, Ludwell, We Fight of Oil, Alfred A. Knopf, New York, 1928.”

“US analyst Ludwell Denny in his book “We Fight for Oil” noted the domestic oil shortage and says international diplomacy had failed to secure any reliable foreign sources of oil for the United States. Fear of oil shortages would become the most important factor in international relations, Denny said.

“That empire in Southeast Asia is the last major resource area outside the control of any one of the major powers of the globe….I believe that the condition of the Vietnamese people, and the direction in which their future may be going, are at this stage secondary, not primary.” (Senator McGee, D-Wyo., in the U.S. Senate, Feb. 17, 1965) “

So, while I remain very ignorant, it’s clear enough to me that oil is more of a concern to the US war-makers than “terrorism” or “communism”.

Iraq, Afghanistan, perhaps Vietnam, and now Libya: wars for oil.

Posted in Uncategorized | 3 Comments

I have one of these, and I like it!

Pandora vs Floppy Disk

Posted in Uncategorized | Leave a comment

ask a genius (me): cause of floods in queensland?

Cause of floods in queensland: it’s not CO2 or climate change, it’s lack of trees / forests. Forests accumulate soil, buffer the water and release it into the rivers gradually.

Idiot humans cut down the forests and put cows, sheep, crops. Then when it rains heavily, almost all the rain goes directly into the rivers, causing a flood.

The fact that humans have cut down half of the Earth’s forests over the last few thousand years is also the major cause of CO2 accumulation and climate change; it’s more significant than human emissions of CO2. http://sam.ai.ki/co2 [ unscholarly citation of myself ;) ]

Go plant some trees, e.g. with Ray Thomas and the Regent Honeyeater Project. And murder a lumberjack don’t waste too much paper.

Posted in Uncategorized | 1 Comment