You have 10 seconds

I was walking along the Orchard Road (a busy “consumercial” street) in Singapore. There was this guy who approached me with maybe 2 pieces of paper and a blue plastic envelope/bag in his hands. He looked a bit flustered, and started talking to me and pointing to the top piece of paper.

I heard words such as “help”, “ex-convict” and “get jobs”. I saw the word “ex-convict” on the piece of paper he’s pointing to. The foremost thought on my mind was, “what does he want me to do?“. Do I need to sign a petition? Do I donate some money? Do I have to give out contact information?

After, I don’t know, 30, maybe 40 seconds, he finally got to the point. “Please donate.” And that’s what the plastic envelope was for. I’m not sure if you’ve seen this. It’s a flat carrier, with a small slit near the handle on top, large enough for coins to be slotted. You can even slot dollar notes if you fold them a little.

I donated some money and I was off. I am aware of this movement; I read some poems written by inmates on those service tray paper in Coffee Bean. It’s the Yellow Ribbon movement in Singapore.*

There was another incident where I was having lunch with my colleagues, and this young lady approached us and started her conversation with the words “I’m not here to sell you anything”. Loosely speaking, she was selling us something. I’ll get to that in a moment.

I can’t even remember what she was representing. She pointed to her badge hung around her neck, said something to the effect that she’s authentic and real and part of an authorised movement. She started talking about her cause. For, I don’t know, 30, maybe 40 seconds.

Then she got to the point. “Please donate”.

I have every respect for your cause. I empathise with the people you want to help. I also treasure my time very much. So please tell me exactly what you want me to do, and then tell me why I should do it.

Get to the point

Let’s look at this in a simple situation. A tourist asking for directions. “Hi there, I’m kinda lost. Can you tell me how to get to such-and-such building? I’ve been walking up and down this street for a long time.”

Short, simple and under 10 seconds. From your look, your mannerisms, your dressing, your behaviour and then with your question in context, I have already made up my mind about what I want to do with you. Say a stranger walked up to me, and from appearances I could guess he’s a tourist. Facial expressions and body movements suggested tiredness and slight frustration. I could probably tell if he’s lying, if his words matched his outward appearance.

Frankly speaking, when the stranger walked up to me, I probably already decided that he’s lost and he’s going to ask me for directions. And I’ve already decided to help him if I could. Verbalising the question was just a formality.

What happened for the 2 people above was that, when they approached me, I’ve already made many unconscious decisions and arrived at an answer. They were asking for donations, and maybe spread awareness of some kind. They were asking for help.

I’ve already decided they’re probably asking for donations, and I was waiting for them to confirm that. I could be wrong. So I waited for them to tell me if they wanted a donation, and if it was, what was I donating to.

I waited and waited. I didn’t interrupt them since it would be rude. And waited for 40 seconds. I was deciding if I wanted to donate, and what else they wanted with me. And that thought occupied my entire thinking process while they were trying to educate me about whatever it was they stood for.

Make it simple and painless

Look, it’s hard to approach someone as a stranger, and start trying to educate that person about something within minutes. That person is already busy trying to figure out what you want with him. You’re making it harder by introducing a concept that might possibly be unfamiliar to the person.

Now, this might sound a little odd. Anytime you ask for something, you’re actually selling something. It’s just that sometimes, your price isn’t monetary. In fact, if you’re reading this far, I’ve already sold you something. I sold you information, and it’s free. In monetary price anyway. The price was actually your time, your attention. And it’s very valuable to you and to me.

For donations, the product being sold is the feeling of helping someone or a cause.** It might sound materialistic and cynical. It took me a while to wrap my brain around this. If it helps take the edge off, you can think of the product sold as just helping someone or a cause.

Donations obviously have a monetary price tag on it. Is there a need for people to pay with time and attention too? There are empathic people. There are also empathic people with busy schedules and limited time. You can spread awareness about your cause in another fashion. The street is not the most conducive environment to do it.

There’s something I read, written by David Eddings. It’s fiction, but I think it has real life roots. Eddings wrote about this beggar. The beggar was obviously asking for donations. The “trick” was to get the message across, and make it as painless for the donor to donate as possible. A missing leg, dressed in rags, covered in dirt, and crying out “Charity!” in the feeblest, most pathetic voice while sticking out a donation bowl worked wonders.

The wordless exchange

There’s another incident where I was having my meal at a diner. It was relatively empty, only another lady and me. A person walked into the diner and laid something on that lady’s table. “I’ve seen this before.” was my first impression. And that person came over to me and laid something on my table too. It’s a card and a key chain, shaped like a key. The card read something to the effect of “I’m dumb and please buy this key chain from me.”.

When that person walked in and placed something on that lady’s table, I already came up with an image. A dumb (as in unable to speak) person asking people to buy something, almost like a donation. That person placed his card and key chain on my table, and left. His body movements, and my previous memories of similar encounters, and the card told me everything I needed to know. And I knew what he wanted, to buy his key chain (in this case).

He didn’t stay to explain his situation. He didn’t have to explain his cause. He couldn’t tell me even if he wanted to. Yet within seconds, I knew what he wanted me to do.

He returned after a while, and came to retrieve the cards and the key chains. If the person wanted to buy, great. If not, he’d just collect his card and key chain.

He came to me. I bought his key chain. I took out the required amount and handed the money over to him. He took out a set of key chains and gestured. Oh, I get to choose the colour, and chose gold. He took out a new gold key chain, and handed it to me. Then he took out another card and showed it to me. Oh, it acts as a bottle opener too.

Key chain shaped like key

I smiled at him. He smiled back, and then left. I didn’t feel stupid at all, despite some fumbles over communication, despite the fact that no words were ever exchanged. I felt good.

Impassive interface

Human face-to-face interactions have the benefit of facial expressions and body movement to add context. With computer programs, your users miss out on this benefit. Suppose the user already know what the interface is for, for example, an online sales order form. Is the interface intuitive enough that the user knows what he needs to do next?

I’m not talking about people who have the attention span of a gnat, who clicks away within 2 seconds if the web page doesn’t load fast enough, or the initial look isn’t interesting enough. I’m talking about people who already know what they need to do, and why. I’m talking about people on that web page, or program interface, and they are confused about what they need to do to make what they want happen.

Make the steps they need to do obvious. Make the text fields and required input format obvious. Have appropriate text for action buttons. Some of these people could be busy on the phone, attending to customers. The last thing they need is a confusing interface. Make it obvious to them what they need to do.

Because on the screen, you have 10 seconds to tell them what to do.

* I went to the web site, and it turns out there’s a pop-up warning about donation soliciting on streets. Maybe I was conned?

** There’s this idea that when you buy, you’re actually buying a feeling. That car you bought? You’re buying the feeling of freedom, of convenience, of status. That dinner you bought? You’re buying the feeling of staving off hunger, of being satiated. Just a different perspective.

Encryption, bijection and reversibility

The whole point of this article is to tell you that you need to write reversible programs, or programs with results that are easy to undo. There, you’ve got the point. That’s it.

You still here? Then get ready for a bucket-load of concepts below.

The swap

How do you change DAN from a him to a HER? Add 4. Suppose you have the entire alphabet on one row and another set shifted by 4 placings:

ABCDEFGHIJKLMNOPQRSTUVWXYZ
EFGHIJKLMNOPQRSTUVWXYZABCD

Can you see the correspondence? We can even undo the sex change, reversing HER back to DAN.

This is one of the simplest forms of encryption. It’s also easily broken with the use of computers (and programming of course). Still it illustrates the concept of changing something that’s RIGHT to something unintelligible like VMKLX.

The whole point of encryption is to scramble a plain text message into something obscure so no one except the intended can read it. There’s no point in having an obscure message without being able to get the plain text message back, is there? So encryption has to be reversible.

Now we’re going to do math.

Please, no trees

Let ALPHA be the set of all 26 English alphabets in upper case. Suppose a function F behaves such that if F(x) equals F(y), then x must be equal to y, where x and y belong to the set ALPHA. Then the function F is said to be injective.

Using the encryption model above, where our function F swaps the letters from the top row to the letters in the bottom row, let’s have the final letter be D. There’s only one letter that can map to D, and that’s Z. F is injective.

It might be a bit hard for you to visualise that. Let’s use the contrapositive version of the statement. If x not equal to y, then F(x) not equal to F(y). So if P not equal to Q (which is true), then F(P) = T which is not equal to U = F(Q).

Injectivity is a one-to-one relation. No tree-like structure, no linking of nodes, no multiple results from the same starting point, no multiple starting points ending in the same result.

Must start somewhere

Let ALPHA be the set of all 26 English alphabets in upper case. Suppose a function F behaves such that for every letter y in ALPHA, there’s another letter x in ALPHA such that F(x) = y, then F is said to be surjective.

Using the encryption model above, every letter in the bottom row has a corresponding letter in the upper row. So our function F that swaps the top letter to the bottom letter is surjective.

Surjectivity is a there-exists-a-starting-element concept. On a philosophical note, your life journey can be likened to surjectivity; you started somewhere didn’t you?

Two way street

A function F is said to be bijective if it is both injective and surjective. It is one-to-one, and for every destination, there’s a starting point. So you can go back and forth in a unique manner without fear of getting lost.

Bijections allow reversibility. Encryptions are bijections. Thus encryptions are reversible. Actually, I just thought I’d introduce you to the concept of encryption and bijection. Their relation to reversibility is hand-wavily tenuous… But I’m forging on anyway, so…

Can you write a bijective program?

I would have to say no, in general. There’s usually more than one way to do something, and that violates the injective rule. But if you conform to a stricter set of rules, you can approach the situation where there are fewer ways of writing code to do whatever you need done. So you get an almost injective state. Then you get a reversible program under strict rules.

What’s the whole point of this? A program whose results can be easily reversed is useful. Suppose the result isn’t satisfactory. Do you need to do a lot to undo that damage? A computer virus is (generally speaking) not useful. It’s effects are not easily reversible. In fact, its intent is to be as irreversible as possible.

A reversible program, a program that you can rerun with minimal fuss and effort. That’s useful. Why would reversibility be useful?

Because it allows humans to make mistakes.

And we will make mistakes, make no mistake about that *smile*. When users make a mistake, how have you written your program so it’s easy to recover from it?

Only quarters counted

Last week, at the end of the article, I presented a problem on floating point values:

Given the range of non-negative 2-decimal-point values less than 1, what are the values that can be represented exactly?

I was referring to dollars and cents, monetary values. As of this writing, there’s no answer submitted. I will assume that you’re very modest, and so will provide my version of the answer. First, we dissect the question. “non-negative” means cannot be negative, so zero and positive numbers only. “less than 1” means, well less than 1. And the values are restricted to numbers with up to 2 decimal points of accuracy.

So this means the set of values 0.00, 0.01, 0.02 … 0.97, 0.98, and 0.99 (1.00 not counted).

Before writing this article, I already an answer, intuited without actual calculations. Then I went to do some research of mine to verify that answer. I was mortified that my understanding of the IEEE floating point standard was wrong. I dug up my old university programming textbook, where there’s a section on internal floating point representation, and confirmed my misunderstanding.

My error was in how the exponent and mantissa were represented. In the end, I realised I had to convert all the 100 values (0.00 to 0.99) into binary representation to check. I didn’t relish doing long division by hand for a possible indefinite number of iterations (up to 32 anyway), and for 100 values. So I took the easier way out. I wrote a program. *smile*

decimal decBinary = 0.5m;
decimal decBuffer = 0;
int i, j;
for (i = 0; i < 100; ++i)
{
    decBuffer = i * 0.01m;
    Console.Write("{0} ", decBuffer.ToString("f2"));
    decBinary = 0.5m;
    for (j = 0; j < 32; ++j)
    {
        if (decBuffer < decBinary)
        {
            Console.Write("0");
        }
        else
        {
            Console.Write("1");
            decBuffer -= decBinary;
        }
        decBinary /= 2;
    }

    if (decBuffer != 0)
    {
        Console.WriteLine(" non-terminating");
    }
    else
    {
        Console.WriteLine(" terminated");
    }
}

It's in C# and there's not much comments. But you should still be able to follow much of the code, even if you aren't familiar in C#. This is assuming a 32-bit is used to represent a float. I ran the long division iteration up to 32 times, even though in a real representation, only 23 bits are used. Here's a Java applet IEEE binary converter you can play with too.

What happens is that, if after 32 bits are assigned (a 0 or 1), if more bits need to be assigned (not terminated), then the floating point representation isn't exact (since there's extra information truncated). You will then note that only 0.00, 0.25, 0.50 and 0.75 have terminating binary representations. This means, only those 4 values can be represented in a float exactly.

So, what was my original intuitive answer? Let's look at only 4 binary places in the mantissa. So 1010 represents 1*2^(-1) + 0*2^(-2) + 1*2^(-3) + 0*2^(-4), which is 0.5 + 0.125 = 0.625

Look at the value with only the 3rd mantissa digit as 1, 0010. It represents 0.125 in value. How do you "get rid" of the 0.005 to get 0.12 or 0.13? You can't, not from switching on bits in any of the mantissa digits. So what does this mean? The moment the 3rd mantissa digit is a 1, the floating point representation can never represent any of the 100 2-decimal-point values exactly.

In fact, the moment any mantissa digit that's the 3rd or after (4th, 5th and so on) is a 1, that representation is inexact already. So only the 1st and 2nd mantissa digits are "allowed" to be 1. So 0000 is 0.00, 0100 is 0.25, 1000 is 0.5, and 1100 is 0.75.

Same answer as from my laborious calculations. Out of the 100 values, only combinations of quarters or 25 cents have an exact floating point representation.

I hope this has been an interesting thought process for you. Has this changed the way you think about storing monetary values?

Seriously? I got in?

I wasn’t sure. I was nervous. I was also hungry. I pored over the email I typed out. Was it too brief? Should I take out some words? What am I talking about?

Request for inclusion in Alltop, specifically the Programming category.

I looked at the email request I had in front of me, and thought “I’m going to go crazy if I don’t send this soon.” And hit the Send button. And went for lunch.

Alltop. Seriously?! I got in?

I didn’t think about it until a day later (I do have a life you know?). I checked my email inbox and there it was, an email from Guy Kawasaki, the man behind Alltop (together with Will Mayall and Kathryn Henkens). Suffice to say, I got in. You are now looking at a proud member of the Alltop Programming family.

Here are some sites listed in that category that I also read:

Please go check out some of the other categories and sites at Alltop too.

I also noticed that the favicon for the Programming category is a white capital P on black background. Hmm… I seem to have seen that somewhere… oh right, my favicon is like that too. The difference is that mine is flat at the top and bottom of the curves of the P.

Ok, I’m going to go do cartwheels in exhilaration or something. Thanks Guy!

Mind Trap – Subterra

Leaving the strange ruins behind him, Ryan descended slowly into the enfolding darkness. The afternoon sun was going to set soon, and without any light source, Ryan decided to go as far down the stairs as he could. Placing his right hand on the wall, he focused his attention on the steps, hand feeling along the uneven wall, careful not to blunder and fall down.

Nearing the steps where he could no longer see very much beyond them, his right hand encountered a protrusion. He looked up and made out what appeared to be a rusty metal ring set in the rough stone wall. Several things clicked in his mind from reading medieval stories and adventures.

“Torch bracket!” he thought.

He felt around the torch bracket and felt the reassuring smoothness of a cylindrical object held loosely in its embrace. Much too cylindrical. It was also plastic instead of wood.

“What the?”

“You idiot! You gave him an electric torch?”
“I’m sorry, I panicked ok?”

Turning the flashlight curiously in his hands, Ryan found the on-off switch. And flicked it on.

“Hey, it works!”

“Why don’t you throw him a laptop too?”
“I said I was sorry, didn’t I?”

Beaming at his unexpected good fortune, Ryan decided to ignore the question of what a modern torch was doing in a place like that. Waving the flashlight to his left, he found … nothing. There was nothing beyond the left side of the stairs. Ryan shuddered from the disastrous possibility he averted by using the right wall to guide his descent.

He looked at the sunlit entrance behind him. Chasing darkness away with his flashlight, he peered at the seemingly unending flight of steps descending in front of him. He gave a sigh.

“Here we go.”

The illumination from the flashlight was weak. Ryan could barely see a few steps in front of him. He didn’t have any spare batteries, and there weren’t any around the torch bracket. Despite the possibility that the flashlight might give out at any moment, Ryan still proceeded carefully. One step, two step, three step…

And then a click sounded in the silence. Ryan froze. His left foot landed on a step that submerged slightly. A whimper escaped his mouth. Heart pounding, yet too afraid to lift his foot, he squeezed his eyes shut and waited in the deathly quiet.

“I thought I told you to take that thing out.”
“It adds excitement, don’t you think?”

After several seconds, a soft crackle was heard from his left. Ryan instinctively hunched down a little. When the expected flying spears or spikes or whatever unpleasant surprise didn’t happen, he risked a peek. A crack appeared in the air, blue light streaming out of it. Then it was gone in a second.

Taking in air in ragged breaths, Ryan quickly pointed the flashlight at his left foot. Still there. He looked up ahead, and saw a faint orange glow. He pointed the flashlight at the steps before him, and made a decision. He took a deep breath and started bounding down the steps, two or three at a time, towards that questionable yet hope-giving orange light.

He reached the bottom of the stairs quickly. The right stair wall joined up with a wall in front of him. Ryan turned to his left and found the source of the orange light, a real fire burning torch in a torch bracket this time. It was at the end of a corridor, perhaps 12, 15 metres long.

He walked slowly towards the torch, not daring to run. Several nerve-wracking moments later, he stood at the end of the corridor. Another corridor branched to the right. As Ryan turned, he noticed some writings carved into the stone below the torch.

His army’s SIZE was PUNY,
And he’s said to be loony.
His troops fell, FORTY to EIGHT.
So tell me, was that his fate?

There’s another seemingly senseless set of carvings beneath that poem.

KYTUPEGINY

Shrugging with confusion, Ryan turned to move along the corridor on his right. Up ahead was what appeared to be a brightly lit large room. Realising that he’s still pointing the flashlight in front of him, Ryan switched it off and placed it in his pocket, since there’s enough light to see.

As he walked along that corridor, there was a noticeable drop in temperature. Ryan shivered. Near the end of the corridor, he saw two bronze statues, armoured with helms, chest plates and wielding in a raised position, the ugliest pain inflicting weapons he had ever seen. It looked like a sword, with two hooks at the end. Ryan wondered what a weapon like that would do to him, then realised it would do as much damage sliding into his body as it would do coming out.

Imagining the statues hammering down on him while he’s passing through, Ryan backed up a little. Then proceeded at a run and made a mad dash through to the room, rolling into a ball and laid on the floor in a foetal position. No sound and movement from the statues. Ryan got up and dusted himself off.

“I hate this place.”

He looked around the room. Three torches burned in the middle of the left, right and front walls of the room. At the corners furthest away from the corridor were two stone statues, a goat on the right corner and a lion on the left. Ryan turned around and saw a dragon statue on the right corner and a tall smooth plaque on the left. There appeared to be writings on the plaque, so he moved towards it to investigate.

There were two rows of numbers at the top:

935
9351246780

Then there’s a drawing of a fire symbol in the middle, and this below it:

RBETTOLCQUCQJVE

Confused again, Ryan looked around the room. Dead end.

“There’s got to be something here,” he consoled himself. “Some brick panel, or contraption or…”

Ryan looked up and grinned. “Or puzzle.”

What should Ryan do?

The answer will be published here in a comment a week after, or when a reader submits an answer. I think I cut off the fun too soon for the last puzzle, so I’m going to wait a few days first, even if a reader answers correctly.

When you submit an answer, please include your reasoning too. Part of the fun of giving the answer is explaining it. Anyone can come up with an answer. Not everyone can fully explain it though. Oh man, I hope I gave sufficient clues…

No torches were harmed in the writing of this puzzle. Flashlights and batteries each sold separately.

[UPDATE: There were no takers for this puzzle. So I answered my own puzzle. View answer.]

Light bulb at the brink

This is another one of my “lessons learnt from video games” article. The role playing game in question is Romancing Saga. To illustrate my point, I’m going to describe some of the game battle mechanics.

Brief gist of the game. You, the player, can control up to five characters. Characters can be recruited during game play. Characters can master a variety of weapons (such as the sword, broadsword, staff and even martial arts) and magic (one of my favourites is the “sunlight”. Simple, unassuming, cheerful and very anti-violence in name. Still does good damage too.).

Sunlight on lake with mountain background

The difference between Romancing Saga and other RPGs is the method of levelling up. Normal RPGs have an experience point system. The more monsters you kill, and the more vicious they are, the higher the experience points. Once you accumulate a particular number of experience points, you level up, where one or more of the following increases: strength, speed, intelligence, health points (reach 0 and it’s game over), magic points (for casting spells) and other kinds of character statistics.

Romancing Saga, on the other hand, has an incremental system. After every battle, there’s a possibility that one or more of the character’s statistics will increase. I’m going to focus on 2 particular stats, the WP and the JP. The WP determines how many weapon skills a character can unleash. The JP determines how many spells a character can cast. What do they stand for? I’ve only got a hunch. See image below.

Ji Shu kanji

The 2 Chinese characters, or kanji in Japanese, is pronounced “ji” and “shu” respectively (in Chinese). Their Japanese pronunciation are “wa za” and “jiu tsu” respectively. They refer loosely to technical skills and intellectual skills respectively. Can you see it now? Wa za points, or WP. Jiu tsu points, or JP.

I’m focusing on WP and JP because, along with HP (health points?), they fluctuate in battle, usually in the undesirable direction of down. And if you have a long series of battles ahead of you, you want your characters to start with as high a value for each as possible.

So I, uh, sometimes go bully some weaker monsters. I will start off my bullying spree by wasting every single point of WP and JP. Why? This could be some superstitious belief of mine, but I found that when I’ve got 0 WP and 0 JP, my characters seem to improve their base WP and JP more often.

It’s like the game felt that because I’ve got nothing left, I probably don’t have enough WP and JP to begin with, so rewards me with increases of base WP and JP. Then when I rest up (which I hardly ever do), I have higher base stats.

The light bulb

There’s another reason why I waste all my skill points away when I’m trying to level up. In Romancing Saga, you can learn new weapon skills right there in the heat of a battle. You could use a rapier to attack normally, and suddenly, a “kring” sounds, a light bulb appears, and your rapier wielder unleashes an elegant whip-like slice to the monster’s aorta, resulting in fantastic arterial spray (muahahaha). Or you could use a weapon skill, say a double slash, and you learn the next version, the triple slash. It is an awesome sight to behold when your characters are “kringing” left and right…

And… it seems that if you’ve got 0 WP, the light bulbs come a bit more often. It’s like the game senses you’ve got nothing left, and to help you continue the battles, it rewards you with new (and usually devastating) skills so you can vanquish the monsters and actually stay alive.

I love this light bulbing so much that when one of my characters master a weapon (no more skills of that weapon to learn), I switch weapons just so that character continues to “kring”. It’s that much fun.

The obscure kringer

In Romancing Saga 3, there’s this female character you can recruit. In the story, she’s very sick, and cannot leave her home for too long. She’s gentle and sweet and frail. When she joins your party, your first impression of her is that she’s strong in magic, but she’s too weak in other areas to function well. On an overall character evaluation, you might think she’s not a good addition to your party members.

That is probably what the game developers wanted you to believe.

You see, once you can actually get her to survive some battles, her JP starts shooting up. She can learn magic spells faster than anyone else. She can wipe out a group of monsters with a flick of her little finger. She can bring one of your characters back from the brink of death with her healing spell. As a powerful magic user, she’s unbeatable. Until she runs out of JP.

That’s when her secret comes out.

She, is the most prolific kringer I have ever had the pleasure of playing.

Give her a sword, and don’t think she might drop the piece of steel. She can learn up to 3 new skills in a single battle. That’s about an average of one new skill per attack in the entire battle. Not enough money to buy weapons? Start her on unarmed combat. She’ll master the mid level martial arts skills so fast you’d think she’s on steroids. And maybe because the game feels she’s too weak, and grants higher level skills to her. It’s just so amazing to watch.

Sure, her new skill doesn’t do a lot of damage compared to another character performing the same skill, since her strength isn’t high. But as battles go by, and her base stats go up, her polymath abilities to master both weapon skills and magic spells make her a compatible character to whatever group you have in mind.

And get this; her name is Muse.

So what’s the lesson?

You’re at your wit’s end at solving a problem, say finding that elusive bug that’s driving you crazy. Maybe you’re not really at your wit’s end? Give more, give everything you’ve got. Scrutinise every single line of code. Question every single for loop structure. Scan like an infrared sensor and inspect like a telescope.

And sometimes when you’re tired, your vision is blurry, your shoulders are slumped and you’ve got nothing left to give, your brain does something magical. A light bulb “krings”. Your brain presents you the simplest solution requiring the least amount of effort, because you’ve got nothing left.

You light bulbed at the brink.

The float, the finance and the folly

When I was studying mathematics in university, I was introduced to 3 number “lines”, for lack of a better word. They were the integers (denoted by Z with a double line at the slant), natural numbers (denoted by N with a double line at the slant) and real numbers (denoted by R with a double line at the vertical). There were others, such as complex numbers, rational and irrational numbers. Let’s keep this simple, shall we?

Mathematical symbol for natural, integer and real numbers

Yeah, that’s how I wrote those symbols. I particularly like writing the Z with the 2 disconnected strokes. Ah, those times of writing mathematical proofs… To forestall a question you might have, the I is used to denote imaginary numbers, that’s why Z is used for integers. As to why Z is used for integers, Z

stands for Zahlen (German for numbers)

Integers (or whole numbers) are numbers without fractional parts, such as 5 or 72 or -3, including zero. Natural numbers are numbers for counting, referring to zero and positive integers (sometimes referred to as non-negative integers). Depending on context, zero might not be included. Real numbers are every single existing number available. Numbers such as 1.23456 and 3.14159 and 1.414. They also include integers, and naturally include the natural numbers.

Where am I going with this?

When I learned programming, the idea of having 2 sets of variable types dealing with numbers, one of integral types and one of non-integral types (which I gave a basic introduction here and here), was very natural. On the one hand, we have byte, short, int and long. On the other, we have float and double. I also learned the limitations of each variable type and the appropriate usage of each type.

You should be familiar with the integral types. It’s the non-integral types, or the floating points that I’m worried about. You will note that, while the integrals can’t represent all integers (they have a limit, like 2 ^ 31), they do represent the particular integer value exactly. A 100 is definitely a 100, for example (unless you change the form of representation).

The float and double variable types present a problem. They only hold a number value that’s close to the value you want (most of the time). It’s something about the IEEE representation, the mantissa and the exponent. Research on your own; it’s good for your learning.

Real numbers can have a precision stretching up to infinity. Obviously, our float and double have a bit of a problem with that. Actually this reminds me of a song I remembered from a children’s show (from way back when I was a child):

Thaaaaaaattt’ss iiiiinnnfinnnity
Yooouu can count forever
There’ll always be one more

That’s infinity
Count from dusk till dawn
You’ll never reach infinity
It’ll just go on, and on, and on, and on, and on…

You know, maybe I’ll sing and record it down… you know what? Maybe not. I’ll spare you the agony…

Ok, where was I? Oh yes, precision.

Games don’t need it

More precisely, precision usually isn’t an issue in games. floats and doubles are used in games for storing values such as the (x,y,z) positional coordinate of a player for instance. They don’t have to exact. They just have to be relatively close to the exact value. It’s not like you’re going to notice a 0.00000005 unit left shift, or you rotated like 0.000003 radians clockwise more than is required.

This is why floats are preferable to doubles in games (I might be dated on this…), because they’re faster to add and multiply to, and you don’t need the extra precision.

Financial applications depend on it

If you work with financial applications, it’s a whole new story. The Singapore and American currency use the dollar and cents. 100 cents equal a dollar. It’s typical to use $12.34 to represent 12 dollars and 34 cents (my apologies to the comma-toting Europeans). So the dollar is correct (and exact) up to 2 decimal places.

Does anyone see a problem with using floats on this?

Sure, the float is more than capable of displaying just 2 decimal places. But it cannot represent the smallest unit of currency 0.01 exactly. To illustrate this, try the following piece of code:

float f = 0f;
int i;
for (i = 0; i < 100; ++i)
{
    f += 0.01f;
    Console.WriteLine(f.ToString("f8"));
}

It's in C#, and you should be able to translate to C or some other language you use. Note the output. 0.01 is not 0.01 when stored in a floating point variable type.

This presents some exceedingly infuriating debugging sessions while figuring out why certain values don't work out correctly. It's also why you cannot do equality checks with floating point values (I mentioned this in my recent newsletter, and if you're not on it, please go sign up).

If you're using C# or some other modern language, there should be a variable type for fixed-point numbers (fixed number of digits after radix [or the decimal point]), the decimal type being the one for C#. Or the numeric data type in databases, but stay away from the money type. I don't know, I find it funny to store my monetary values in a money variable type. *shudder*

If you're using C or some other language without fixed-point number variable types, then my advice is to avoid calculation with floating points as much as possible. Do them in the environment where they are still exact.

For example, you want to retrieve some values from the database and bind them to local program variables. Then you're going to do some calculation with the variables, and update them back into the database. Can you do the calculation entirely in the database environment?

You might not have grasped the significance of the code above. Sure, the values are still exactly represented for most of the 100 decimal values. Imagine adding, subtracting, multiplying and dividing those values tens of times, hundreds of times. Ever heard of rounding errors? Errors don't disappear, they accumulate. If you're lucky, they cancel each other out. If not...

Here's a question for you. Even though it's a folly to represent the 2-decimal-point currency values with a floating point variable, there are values which a float can represent exactly. Given the range of non-negative 2-decimal-point values less than 1, what are the values that can be represented exactly?

I'll publish my answer and reasoning in another (later) article. I also want to hear your answers too. Add your answer in a comment. Explain your reasoning too. Frankly, I'd much rather publish your answers, and add in some of my comments.

Mind Trap – Strange Ruins

He blinked. Ryan could feel the light falling on his eyelids. His head still hurt, so he kept his eyes closed.

Birds were singing. “That’s a good sign,” he thought.

There was a whiff of musty damp earth. He clenched his hands, and clutched a handful of leaves. Ryan took a deep breath, and coughed. Blinking his eyes slowly open, he surveyed his surroundings. He was propped sitting up against a tree. Slivers of sunlight slipped through the leafy canopy above. This was definitely not the city.

Panic gripped Ryan and he scrambled up. He coughed again.

“Where am I?”

“You couldn’t find anyone else?”
“It has to be him.”

Trying to calm down, Ryan hazily recalled the events of the evening before. Something about a taxi and the driver turning around to face him and spraying something into his face.

He had to start moving. In the distance, he could see a massive stone structure. He started walking. There was a soft crackling to his right. He only caught a glimmer of bluish light before it faded together with the sound.

After what seemed like an eternity, he reached what appeared to be the entrance to some ruins. Aztec, or maybe Incan. It didn’t matter. It looked pretty run-down. There was a tall white totem pole on the left of the ruins. At an equal distance reflected to the right, an ebony statue stood in stark contrast to the surroundings.

And straight ahead in the centre, sat a curiously squarish structure. Ryan moved closer and saw 2 stairways set in the structure, leading downwards, like twin nostrils of a slumbering giant. Brushing aside the unpleasant feeling of venturing into the nasal stone passages, he noticed some carvings above the stairways.

51 82 68 87 65 89
66 69 72 73 78 68
83 84 65 84 85 69

Ryan was still thinking about which of the two uninviting routes he’s going to try when he looked at those carvings again. Smiling again, he chose his route.

What was Ryan’s choice?

The answer will be published in a comment here after a week (on 23 May). Or if some genius reader adds in a comment. Whichever is earlier.

[UPDATE: Christopher has already solved the puzzle. View answer.]

Continue the adventure…

Heaps of by-products

Do you maintain many programs? Do you maintain many programs, each producing an inordinate amount of log files, output files, intermediate files and database records? Do your programs create heaps of digital by-products?

Hansel and Gretel

Are you paranoid? No? Then stop spawning unnecessary log files and other output. I’ve seen a simple requirement being mangled into several programs, and each program absolutely must create a log file and an output file for the next program to consume as input. There were database tables to check for sequencing purposes, holding tables for data and tables to store the progress of the programs. All for one simple requirement.

The original programmer might have wanted to modularise the requirement. Fine. But an entire program to take the place of a function?

Perhaps the programmer designed it that way so it’s easy to debug and trace errors. Hence the copious littering of log files to keep track of where the program executed. Like Hansel and Gretel leaving breadcrumbs. Then why were the log files not containing enough useful information?

Stop creating digital waste by-products for the sake of creating them. If that intermediate step doesn’t require a log file, don’t write one “just in case”, or just so you can “keep track”.

Disk space may be getting cheaper, but that’s not the point. The next programmer have to wade through heaps of rubbish just to find out what went wrong. And that’s without looking at the source code.

Databases do more than store data

“But what if I need intermediate output files?” you ask.

For?

“The Unix shell script can sort data records in a text file.”

So can a database. That’s what the “order by” clause is for.

“I need to concatenate records from multiple files.”

So can a database. That’s what tables are for, inserting and storing data.

“I need to check for duplicates, and the duplicates can be stored in a text file and I can use shell scripts to sort and…”

So can a database! In fact, a database can probably do a better job than whatever crazy algorithm you can come up with. Primary keys can prevent duplicates. If you still want to store and keep track of the duplicate records, a simple “group by” clause can retrieve the duplicates easily.

People seem to forget that databases can be used to do more than just store data. You can perform a good many different operations in a database environment.

“But wouldn’t a C program run through a file and sum up a number in a certain column based on certain criteria be faster to execute on the Unix machine?”

Perhaps. I don’t understand why the concern over performance and memory management, and that Unix C programs and shell scripts are better than anything done on Windows (no this isn’t a *nix/Windows comparison/war), and better than anything done by a database engine. Use the right tools.

It’s hard to parse a text file. Yes, I know all about the magical grep and other commands of the Unix environment. And there are many text files, with little to help narrow down searches.

In the database environment, I can sort records, do simple math calculations, do complicated update statements involving several tables. I can pump all bad records to another table in a single statement. I can remove bad records from a table in a single statement.

And I can search records, even by a specific column. How do you do that with a normal input text file? Use the right tools. I seem to have said this before…

Landfills and the Delete button

Physical waste is traditionally buried in landfills. We’re starting to run out of land. Digital waste is handled by the ubiquitous Delete button. And we seem to have no shortage of disk space.

Do not abuse this freedom in treating “stuff”. Just because you can create stuff in the digital landscape doesn’t mean you have to create trash.

Podcast: Why programmers write stupid code

Need I tell you, again, how my first recording failed abysmally? Probably not. And here I am, doing another audio recording. Plus the fact that I got a new headset and I want to play with it.

To be honest, I learned a few things. I speak quite fast normally, and when my speech gets into recorded form, my words just slurred together. I just think faster than I can write or speak.

Be clear. That was main goal this time. So I spoke slower and slightly louder. I did some post production work and here it is:

Download mp3 [~ 2:40 minutes ~ 1.22 MB]

Noticed that I haven’t said anything about the subject at hand. Well, here’s a synopsis of the recording.

  • Murder on the Orient Express by Agatha Christie
  • Courting danger in racing simulations
  • Why programmers write stupid code

You’ll have to listen to the recording to find out how all 3 points fit together. For the impatient ones out there who just wants to know the final answer, here’s a hint: First word of first four paragraphs.