Playing with the bits of double variables

[Most of my readers will not understand the following, but the mathematicians will love it]

A couple of years ago, a mathematician friend told me some problems he had about optimizing a program (maths calculations), asking me for suggestions. I answered with some ideas, but then he asked me about the double format, the way the computer stores a double variable, with its separated sign, mantissa and exponent (actually, IEEE 754). He was pretending to manipulate directly those parts, to improve the calculation’s speed. I didn’t help him in a clear way.

In the last few months I’ve been taking part in some programming contests, on TopCoder. Today one of the problems was really boring but at the same time really interesting. In brief, it says that the division is a computationally expensive operation, except if the divisor is a power of 2 (then you can use just bit shifts to do so). The statement asks to program a division, approximating the result using a series of divisions by powers of 2.

I tried to use bit shifts and bit masks to get the exponent of a double (keeping in mind the double binary format), but I discovered that C++ (as C) doesn’t allow bit operations with doubles.

Nevertheless later I asked in the forums, and got an interesting idea. Using an “union you bind two or more variables, sharing the same memory position. That way you can manipulate the value in memory from different angles.

Here is an example in pure C:

#include <stdio.h>

struct Double{
    unsigned long long mantissa : 52; /* 52 bits */
    unsigned exponent : 11; /* 11 bits */
    unsigned sign : 1; /* 1 bit: 0 is positive, 1 is negative */
};

int main(){
    union{
        double value;
        Double parts;
    } u;
    u.value = 3.1451592; /* Direct manipulation */
    u.parts.exponent++; /* Multiply by 2 */
    u.parts.sign = 1; /* set negative */
    printf("%f",u.value); /* -6.28... */        
}

A farmer

I should have been a farmer, or a gardener.
Every single plant grows on my rough hands.
Is it me?, or is the nature so beautiful?

Even a garlic clove!
Garlic growing

Renfe.es: a bad UI case

In the last year I flew 24 times. Too many times, according to those ecological footprint quizs. So I decided to give another try to the train, for my usual Barcelona-Alicante trip. Unluckily in Spain the train’s company, “Renfe”, is really prehistoric: there are always delays, tickets are expensive (domestic flights are cheaper and quicker), and is the focus of a lot of political scandals.

Last Christmas they announced a new web, with a new modern system of buying tickets, and discounts up to 60% in the normal price. So far, the reservation system was terribly bad. Can they evolve into something “normal”?

I tried to buy a return ticket, Barcelona-Alicante. Firstly, the old fares are still there. Actually there is a new thing, a Captcha that gives you access to the classical fixed prices… whenever you want to do your trip, the prices are always the same for the same trains, so why do they need to hide them under a Captcha?

But the best part is to arrive… If you want to get access to the new “web fare” (60% off), you must choose “One way” ticket at the first form. This means: you must do the process 2 times, one for your going ticket, and one for the return ticket. Later you have to find a small combo box that let you choose among the fare list… the default is the most expensive option.

Finally a frame with you bank web is opened, to verify you credit card details. You press “Ok”, and suddenly Renfe.es says “this train is not available”… at the end of all the process. Monkey do better jobs.

After 2 hours I accomplished the mission, I got my ticket with a cheap price. But I suspect that it has been deliberately implemented in this weird system. I imagine people, elders, in the marketing department of Renfe, smiling at the time they say “hehe, we have offered a modern look, but we are not going to let anybody a cheap price ticket”.

Welcome 2008, warming up again

Big benI stayed one week in London, during the end of 2007 and the beginning of 2008. I had a really nice time there, took part in a Go tournament with excellent results, and did a lot of sightseeing beneath the cold and dark sky. I took a lot of pictures, as well. It was an interesting city, that surprised my assumptions (so maybe one day I might live there). But the most curious thing is the fact that I really disconnected from my normal life for a week. It seems that I need more than a 4-day trip (my usual trip duration) and/or an intense event (like a tournament) to clean my mind.

Now I’ve come back, and I must start again with my small and big projects. I’ve decided to apply myself to a strict schedule, a timetable for every single day. Because when you are unemployed, the time goes by even faster than when you are an employee. And my big project needs a lot of time, so do the collection of small ones.

At the moment, I have to finish an issue: remove the rhinitis from my nose. 3 days working on it, and it’s still here!

I won the lightning tournament!!

One of the sides events of the London Tournament was the Lightning Tournament, where you play Go (with handicap) with a really short time per player. During the qualifications the time was 10 minutes per player (sudden death, i.e. without byo-yomi), and in the knock-out finals it was 12 minutes. I won 5 in a row in the qualifications, and then 3 more games in the finals, getting the FIRST position, with a prize of 100£. Incredible!!

Moreover, I formed a group with Lluis (my teacher, 6 dan from Korea), for taking part in the Rengo tournament (2 players against other 2), and we did a good job, getting the first position (without prize money).

In the main tournament, my result was more normal (4 win, 3 lost), but I especially enjoyed all the side events. A really interesting tournament to keep in mind.

Digitalizing the “Clot Roig” and the “Clot Negre”

Red stones next to "Clot Roig"During my stay in Novelda, my hometown, for Christmas I had an idea: “digitalize” some almost secret places near the river Vinalopó, which is next to the city. The river itself is quite small, with a width of around 2 meters, but the bed is quite wide, due to scattered floods. Walking up next to the river you can find a lot of small salt water sources. And some decades ago, some people decided to build natural pools to have baths in those waters, and get benefit from its mineral composition.

The biggest pool is called “el Clot” (the Hole), and it’s quite famous among the local people. During summer there are groups having baths every day. It’s around 2 meters deep in the center, and quite long (you can swim some meters). It’s 1 Km far from the river’s bed, and gets the salt water from “Salinetes” a gully with some sources. The best part of having a bath is the concentration of salt: you float like a cork, and it’s almost impossible to drown. When you go out, your skin is covered with a layer of salt!

Black stones next to "Clot Negre"But I knew there are more “Clots”, smaller but with different features, in the river’s bed itself. So I started to (re)find them. I’ve taken pictures, and geo-localized them: now are part of Internet (photos on Flickr)! The first one is the “Clot Roig” (red hole-pool), with a strong red colour, due to its ferrous waters, that covers the nearly stones. The second one, quite more difficult to find, is the “Clot Negre” (black hole-pool), with (guess it) black water. You can see the difference in these two pictures: surprising colours. Moreover, both of them have a lot of bubbles coming from the deepest part in the center.

If only it was summer, to have some baths…

Possible places to visit

I write this here just to keep the list in an accessible place 😉

Destination Reason Dates
Amsterdam, The Netherlands Go May 10-11
Carcassonne, France Tourism a weekend
Canada Tourism
Cormons & Venezia, Italy Go May 31-June 1
Costa Rica Tourism
Dublin, Ireland Go March 1-2
Dubrovnik, Croatia Tourism
Formentera, Spain Tourism January
Granada, Spain Tourism
Hamburg, Germany Go May 17-18
Helsinki, Finland Go February 9-10
Isle of Man, UK Go August 17-22
Loire Castles, France Tourism
London, UK Go December 27-January 3
Malta Tourism
Mont Saint Michel, France Tourism
Prague, Czech Republic Go
Rome, Italy Tourism
Wales Go June

*Go means “Go Tournament” plus “Tourism”

Any other suggestion?

DIY greenhouse

DIY greenhouse How can I build a greenhouse, a small one that fits in the balcony, to let my tomatoes become red?

This question have been bouncing inside my mind during some weeks. With the cold weather my tomato’s plant can’t grow red tomatoes. I was expecting its death, but for some reason the plant continues blossoming. So I decided to give it an opportunity. And the way to let it do its best is giving it a house. But how can I build a greenhouse?

The plastic is a bathroom curtain (1 euro at Ikea). I took the opportunity to change the old curtain and gave it a second use. On the other hand, this morning I found a clothes horse (to dry the laundry) on the street. Somebody had decided to throw it, because one leg was broken. And I gave it a second use. I joined the pieces, and… voilà, a greenhouse. It’s so warm inside 🙂

I hope I’ll have red tomatoes and peppers soon!

3 meals per day: when, why, how many?

Korean lunch Last Saturday I was invited to a Korean lunch. It was delicious, but we had it at 12h, when I usually have lunch at 14:30 or 15h. This shift of 3 hours made me feel sleepy around 20h, when I decided to move forward my dinner, usually at 22h. During the whole evening I was wandering what are the correct times for the 3 meals…

I know that Spanish schedule for the meals are a bit weird for the rest of the world: we usually have breakfast at 8h, sometimes a second breakfast at 10h, lunch at 14h or 15h, and dinner from 21h onwards. This is probably due to our time zone: we use the Central European Time, but we are quite West from the center of Europe. So this implies we have sunsets later than the rest of Europe, and this could be the reason for this lunch-time and dinner-time shifts. But which are the best times to have lunch and dinner?

It’s more, think about the amount of food. We (Spaniards) are used to having a heavy lunch, just the opposite of most countries in Europe, where they have dinner as the largest meal of the day. A lot of dietitians say that the heaviest meal should be breakfast, but we don’t follow this suggestion (and in my case, I can’t eat anything for up to 2 hours after waking up).

Maybe they should study the energy we use during the day, and draw some graphics, to find the optimal times for meals, the amount of meals per day, and the correct amount/type of food. It’s like a big equation. I’m sure they have done these studies, but: 1- I can’t find any useful one using Google, and 2- most of the people are too bound to their cultural ways of eating. But we can’t take our diet lightly, “we are what we eat”. And it’s almost certain that we are quite far from our optimal way… how can we optimize our lives without thinking about a thing we have 3 times per day. Doesn’t this worry you?