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.
UPDATE: Steve has given 2 more alternative solutions.
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.
Sign up now to get your free ebook of "How to self-publish an online magazine". Your email is kept confidential, and is used only to send information about the magazine.



Hi! I write about maths and programming and other topics of esoteric interest. I'm also the editor of the online magazine Singularity, and you can get the latest issue at the top (it's free!).
