Percentage calculation with negative numbers
Suppose you have 2 numbers. You want to sum them up, and calculate the percentage of each number upon that sum. Say, 4 and 6. So 4 contributes 40% and 6 contributes 60% to their sum 10.
What happens when you have negative numbers?
I did some simple research, and the relevant articles have someone trying to calculate percentage changes from one number to another. Like financial growth reports.
My question is more of, how much each component number contributes to the sum, as a percentage. The problem comes when one of the numbers is negative. Consider the trivial case, 1 and -1. The sum is 0. You already get a division by zero error when calculating the percentage (1/0) * 100.
The solution, which is the same as that in my research, is to take the difference between the two numbers and use that as the basis. So difference of 1 and -1 is 2. So 1 contributes (1/2)*100 = 50%.
What about -1? Use the absolute function. ( abs(-1) / 2 ) * 100 = 50%.
The difference method works fine if you have only two numbers. What if you work with several? My friend actually posed this question to me. Suppose you have 6 numbers, and you want to calculate the percentage contribution for each number towards their sum.
My suggestion? Absolute everything. The percentage contribution of n1 is
abs(n1) / ( abs(n1) + abs(n2) + abs(n3) + abs(n4) + abs(n5) + abs(n6) ) * 100
Then my friend posed a killer question. What if all the numbers are zero? What’s the percentage for each number then (even though each number is zero)?
It’s for a reporting application, and my friend was wondering how to calculate the percentages. Now the sum of a bunch of zeroes is also zero. You hit the division by zero error. Even the absolute-everything method fails, since each number is zero, so there’s nothing to “absolute”.
Since there’s no defined way of calculating when all the numbers are zero, I gave the 2 obvious solutions. The first is that, since each number is zero, and the sum is zero, therefore each zero contributed 100% to the zero sum. The second is, since each number is zero, therefore each contributed 0%.
My friend chose the second solution. The most compelling reason for that choice was that it’s easier to explain the logic behind that choice to the user. It’s an edge case. When there’s no right answer, choose the answer which is easier to explain.
Random quote - I feel productive
Some time ago, my colleague gave birth to a beautiful baby girl. As such, she cannot lunch with us for a while, because she needs the lunch hour to fulfil her motherly duties. She might not be able to bring her baby to the office to nurse, but she can definitely uhm, produce milk for uhm, takeaway.
So one fine day, I had a particularly effective morning, cranking out code while listening to my favourite songs. When my other colleagues and I were getting ready for lunch, the mother called out to me and said she would be joining us. When I asked about her uhm, motherly duties, she said she did it a bit earlier, and so she could join us.
We were happily talking, and strolling along to take the lift down. We caught up with the happenings of each other.
“Oh how are things with your baby?”
“She’s so cute. I’m so tired. Hey, how’s your trip?”
We went into the lift and down it went. Somewhere during the descent, there was a slight pause in our conversation flow. And the mother blurted out:
I feel productive
I couldn’t stop laughing…
Here come weaker viruses
A particular hosting company, McColo, was taken down some time ago. While there are reports of decreased amount of spam, as experienced by Terry Zink, I remain wary.
For around the same period of time, I experienced an increase in spam at this blog.
Granted, Zink was referring to mail spam. I just have a theory. Based on my research in epidemiology, when a major virus gets wiped out, the weaker viruses start to fill in the gap. Nature abhors a vacuum.
For all we know, the major virus was keeping the weaker viruses at bay. Once we hit some kind of equilibrium, we sort of coexist with the major virus, despite our distaste for it. We establish methods for dealing with that virus. We maintain some sort of control.
Once that major virus was taken out (say, the hosting company hosting its operations), a void was left. And if we weren’t prepared for the weaker viruses (because we’re so focused on that one virus), we’ll be in trouble.
The above was a bit of a generalisation. I’m not deliberately being pessimistic. I’m just saying something will happen. Hopefully, that something is us warriors of light taking advantage of the situation.
Pure programming knowledge is not enough
I’m short-sighted. The degree of blurriness isn’t terrible, so I can still make do without glasses. When I first started working, I thought working with computers the whole day warrants the wearing of glasses, so I got myself a pair. Turns out to be more trouble than it’s worth…

[image by Gene Chutka]
Anyway, I found out about it when I was in university, when I couldn’t see the notes on the projector screen or on the board properly. I’d have to sit in the first 3 rows if the words were to be clear. But the first 3 rows were occupied by uhm, zealous students, so I took seats slightly further away.
Since I couldn’t see much of the notes, I squinted. To forestall some of the obvious questions, no, vanity had nothing to do with not wearing glasses. Glasses were expensive, and so were contact lenses. And they’re really troublesome to wear on a regular basis. So yeah, no glasses.
To compensate for my “loss” of sight, my other abilities stepped up to take on the responsibility. After 2 and a half years of serving the nation (National Service in Singapore), I was suddenly very productive. I paid better attention to the lecturer. I listened carefully.
Since I can’t see very well, I got context from surrounding words. My language skills helped a lot, filling in grammar, spelling and sentence structure. In both proper and “broken” English no less. I was studying math, and my math background helped in making sense of the symbols and the esoteric language used, filling in gaps where my language skills were useless.
With perseverance, lots of context filling (from language and math), I got most of the lesson. I actually took very little notes, since I couldn’t copy them fast enough anyway. I opted instead to understand what the lecturer was teaching and try to quickly absorb them.
I’m not so much concerned with seeing the words correctly, but interpreting them correctly.
The squinting… Now the squinting seemed to blur the words even more. But I saw the words more “clearly”. It’s like holding a picture close to your face. It’s blur, and you can’t make out anything. Take it further away and you can see the picture. It’s something like that. I needed to “blurrify” the words so they became meant for far viewing in my case. Sort of like Nazca Lines.
After I graduated and started working, I got myself a pair of glasses. Then I stopped wearing them. I haven’t needed to read words at a fair distance. And the computer screen seems to read fine. I take regular breaks to rest my eyes. Besides, wearing glasses gives me a headache and dries my eyes easily. Sometimes, I still wear them, and only for reading books and playing video games (otherwise I can’t see the dialogue on the TV screen).
The lesson?
When I read code, be it other people’s code or mine, I use a lot of context filling. Half the time, I’m using my knowledge of business logic, human psychology and languages (English or otherwise) to understand the code. The other half is programming knowledge.
It’s when I do this that I figure out patterns, of decisions made (where devoid of comments). That I understand where I needed to make changes for new business logic. That I can do simple refactoring (the refactoring is simple. It’s the understanding that’s hard. Different programmers do the same thing differently).
I found that when reading code, whether because of debugging or adding changes, pure programming knowledge isn’t enough. For example, if I knew the original programmer wrote that piece of code far in the past, I could make a guess as to why he wrote it that way. Say he’s trying to optimise a piece of code, but the optimisation is no longer relevant in modern times.
It bears repeating. Pure programming knowledge isn’t enough.
Math, culture and programming languages
Can a programmer’s background determine whether he’ll be a great programmer? By background, I mean his upbringing, the values learnt, his primary (and perhaps secondary) spoken/written language and so on.
I don’t know. However, I have arguments for and against the proposition. Let’s start with…
Learning to count
You think counting is easy? Apparently not. Recently I read a book, Outliers by Malcolm Gladwell. According to Gladwell, American children the age of four can, on average, count up to fifteen. Chinese children at that age can, on average, count up to forty.
His reasoning is that, the system of naming numbers is different in English and in Chinese.
For example, think about counting from twenty to thirty (I’m deliberately using the English form instead of the Arabic numerals to highlight the difference). You have twenty, twenty one, twenty two and so on till twenty nine and thirty. How about thirty to forty? Thirty, thirty one, thirty two and so on till thirty nine and forty.
Consider counting from ten to twenty. Ten, eleven, twelve, thirteen and so on till eighteen, nineteen and twenty. The pattern is different and irregular.
Before I show you the Chinese method of counting, let me show you the first 10 numbers, shown by the Arabic numeral, the English name and the (closest) Chinese pronunciation I can give:
1 one (yi, like in “yeast”)
2 two (er, like in “brighter”)
3 three (san, “sahn”)
4 four (si, do a short hissing sound with the s)
5 five (wu, “woo” and keep it short)
6 six (liu, “li” and “ou” as in “shoulder”, and string li-ou together quickly)
7 seven (qi, “cheese” without the s, and shorter in length)
8 eight (ba, as in “barter”)
9 nine (jiu, “gi” as in “gin” and “ou” as in “shoulder”. Like that of six.)
10 ten (shi, like that of four, with the h)
There are actually 4 tonal inflections for a Chinese character pronunciation, and we’ll ignore that for this discussion.
So to count in Chinese from ten to twenty, we have shi, shi yi, shi er, shi san, shi si, shi wu, shi liu, shi qi, shi ba, shi jiu, er shi. They are literally “ten”, “ten one”, “ten two”, “ten three” and so on till “ten nine” and “two ten”. There’s an implicit “one” in front of the “ten”, so it’s “one ten one” for eleven. For twenty three, it’s “er shi san”, or literally “two tens and three”.
Gladwell says this gives structure to the counting system, so children are able to grasp larger numbers quicker. The faster you can count to larger numbers, the more operations you can do on them. Additions, subtractions, summations and so on.
Ok, I’m not saying the Chinese number naming system is better than the English system. It’s just different. Gladwell says this difference also makes memorising short number sequences easier. For example, I can remember my Identification Card Number (equivalent to the Social Security Number in America) easier in Chinese than in English.
It explains why when someone asks for my phone number in English, I have a problem. Because I’m mentally translating my memory of the phone number from Chinese to English. Did you know it’s kinda hard to say out 8 digits in English while translating them from a Chinese memory? I can even mentally picture the numbers. It’s the speaking out that’s taking up mental processing time.
Gladwell also made a point about Cantonese (a Chinese dialect) pronunciation of numbers. So I tried saying out numbers in Cantonese (yes, I’m multilingual), and wow, it is easier to say and memorise! The Cantonese pronunciation of numbers are short in length, which makes it easier to spit them out *smile*
And the relation to programming? Programming is made up largely of counting and solutions formed from abstract ideas. When I first learned C, I was surprised that many fellow students had difficulty counting the number of iterations in say,
for (i=0; i<10; ++i)
{
if (i>7) break;
// do something
}
Counting and iterating leads to lists of data, or sets of data. In SQL, you can manipulate sets of data as if it’s one unit, abstracting away the fact that the data is actually iterated one by one. For example, you can select information from another data set, or a subquery as it’s known.
Of course, there are still some people who have difficulty visualising SQL data sets as one unit, hence their need to iterate over that one record by one record at a time, even when there’s no need. What does that tell you about these people?
Hard work is valued
Gladwell also made a point about culture. That successful people seem to grow up with a culture of valuing hard work. The ability to think on a problem long enough to come up with a solution.
He said something worth thinking about. There’s a educational researcher by the name of Erling Boe at the University of Pennsylvania. Boe says that one can know if a child will do well in math without asking that child a single math question.
The example in the book was a fictional Math Olympics. Before the test, there was a questionnaire to be filled in. There were tons of questions inside, none related to math. Boe asserts that a child who finishes that questionnaire will also do correspondingly just as well in the math test.
It’s a question of perseverance, the willingness to put one’s mind to work, even if one doesn’t feel like it.
Hard work is something valued in the countries of “wet-rice agriculture and meaningful work”, as Gladwell puts it. Based on the research of Boe, the top countries are Singapore (yay!), South Korea, Taiwan, Hong Kong and Japan. Though I’m not so sure of my fellow countryman’s motivations… The parents and children could be driven more by the race for top scores rather than the culture passed on by our forefathers.
Well, I haven’t read much on Boe’s research, but the hardworking nature of my countrymen is fairly accurate. Students are willing to study for long periods of time (most of them anyway…). Adults are willing to work just a little longer, a little harder at work. For example, I recently knew that my friend worked overtime till 4 am at the office. Personally, I think that’s crazy, but to each his (in this case, her) own.
The native language
There was a question in StackOverflow about coding in other spoken languages, which was highlighted in Jeff and Joel’s recent podcast.
The reserved words in a programming language are fixed. Usually they are in English, though there are programming languages in say Chinese. I didn’t know there was a Chinese version of BASIC!
Once, I took up an SQL reference book, written in Chinese. I want to mention that I cannot read a programming reference book written in Chinese. That is to say, I can certainly read the Chinese characters, but I can’t understand the heck what it means.
I need a reference book written in English for SQL, because the native (human) language for SQL is English! Unless there’s a variant I don’t know about…
I’ve seen some code written in Spanish before, I think. Can’t remember. Anyway, the native language for the programming language (C, I think) was English, so the code reads fine. The variable names look different, but I didn’t have too much difficulty.
My guess is that my math background prepared me for abstract notions and symbols, and still be able to work with them. So I treated variables named in a foreign language as just another symbol. And continued to read the code based on that.
And this brings me full circle to…
So does background really matter?
I am unfamiliar with how an American (or English, or French) grows up. I don’t really know the values valued, or the culture surrounding the upbringing of a child.
I do know mine. I’m brought up learning two languages (English and Chinese), two Chinese dialects (Hokkien and Cantonese). I taught myself to read Japanese characters. I’m brought up around people who wake up before dawn to work, and work long hours, regularly and consistently and over long periods of time.
Personal values, personality, genes and luck. I agree they play a part in the makings of a great programmer. In particular, I believe that one’s background influences personal values and personality, so in that sense, background does matter.
And specifically, I think my math background makes grasping programming concepts easier for me.
Of course, everything you’ve just read could be hogwash, because I’m still telling (interesting, I hope) anecdotes to illustrate points as Joel points out vehemently. I haven’t read a lot about computer science and its history in America (or pretty much anywhere in the world). I research just enough so that what I write is as true as I understand it (sometimes I don’t research at all!).
I admit that I’m still naive and easily impressed. I still pretty much trust what I read as true. It’s only when I start internalising the information that I really think about them.
So what do you think?

