Written by Vincent

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.

Enjoyed reading this? Share it!
  • HackerNews
  • Reddit
  • Slashdot
  • FriendFeed
  • StumbleUpon
  • Facebook
  • del.icio.us
  • Posterous

Tags: , , , ,

Published on 28 November 2008

Comments

5 Responses to “Percentage calculation with negative numbers”

  1. Will Dwinnell on 14 December 2008 4:54 pm

    I will respectfully disagree about taking absolute values just to make percentage calculations “work”. I suggest instead, that one considers the meaning which is usually impart to the idea of a percentage: A percentage normally represents a fraction of a whole. In this context, neither the fraction nor the whole could ever be negative. My conclusion is that if you are trying to take percentages where one or more numbers might be negative, then you may want to seriously reconsider what you are doing.

    Will Dwinnells last blog post..Parallel Programming: A First Look

  2. Vincent Tan on 14 December 2008 8:33 pm

    The case in point dealt with monetary values. The values are supposed to be non-negative. Actually, they’re supposed to be all positive.

    Some business logic changes later, and financial adjustments (such as credit and waivers) came into the picture. So the possibility of having zero or even negative values became real. (And has now happened)

    It’s a financial report, and the users want a percentage for each value, which represents how much it contributes to the sum total. Based on this, do you have a suitable solution, Will?

    Frankly, I don’t see a right or wrong answer… business-wise. Mathematically though…

  3. Is math important to programming? | Polymath Programmer on 6 April 2009 5:01 pm

    [...] math, despite their math origins. How hard can it be to sum up figures, do discrete prorating, calculate percentages or find out the price based on existing [...]

  4. Steve on 7 August 2009 8:15 am

    There are at least two other possible answers to the question “How much does each zero contribute to the sum of 0″. Since zero is equal to zero, each of the 0’s you added together contributed to the sum equally. Therefore, you could say that each 0 contributes 100 percent divided by the number of zeros (so if you add together two 0’s each contributed 50% to the sum, three 0’s would each contribute 33 1/3%, four would each contribute 25%, etc.

    In computer floating-point arithmetic, since zero divided by zero is ambiguous (zero divided by a non-zero number is 0, so 0/0 should be 0; but anything divided by zero is either plus or minus infinity) the operation 0/0 results in a number called NaN, or Not a Number. [Yes, Not a Number is a number. It's counterintuitive, but it's true.] Because of that, a fourth possible answer is that each zero contributes NaN percent to the sum.

  5. Vincent Tan on 7 August 2009 9:22 pm

    Hi Steve, those are good answers.

    What would happen if the numbers were 0, 0, 0, 0, 4, 6? The first 4 should contribute 0%, the 5th number 40% and the last number 60%? It doesn’t make sense to me to give percentage contributions to the first 4 zeroes…

    So the first answer you suggested has a specific condition. Which makes sense. And equally easy to explain to a non-tech, non-math layperson as the “all are 0%” explanation.

    Your second answer is also correct. It’s a little hard to explain NaN to a layperson though… the context is a financial report. The user doesn’t care how edge cases are handled, only that they’re handled, and easily understood.

    Thanks for your comment!

Leave a Reply

I reserve the right to delete offensive, spammy and/or unintelligible (based on context) comments. I do read all comments, even if it takes a while for me to respond. Polite criticism is fine. Rude ones will be deleted (right after I correct the error of course).