views:

1747

answers:

5

Before you start laughing at such a simple question let me explain:

I am trying to determine how much change (percentage %) there is in an account over various indicators. This is not particularly hard. But how do you generally handle the cases where the current value is zero or the previous value is zero?

i.e.

This week: Earnings = $25.6

Last week: Earnings = $0.0

I currently calculate the % difference by the following formula:

If (CurrentValue > 0.0 && PreviousValue > 0.0) {
   return (CurrentValue - PreviousValue) / PreviousValue;
}  return 0.0;

if the earnings were zero in the previous week - what should the % difference be? +Infinity?
And inversely if the current week is zero? -Infinity?

Then to complicate things how would you handle this in a Linq-To-SQL query

Upside_Earnings = (statistics.Where(d => d.DateTime > first_startdate && d.DateTime <= first_enddate).Average(e => (double)e.Earnings) > zero &&
                                     statistics.Where(d => d.DateTime > second_startdate && d.DateTime <= second_enddate).Average(e => (double)e.Earnings) > zero) ?
                                   ((statistics.Where(d => d.DateTime > first_startdate && d.DateTime <= first_enddate).Average(e => (double)e.Earnings) -
                                     statistics.Where(d => d.DateTime > second_startdate && d.DateTime <= second_enddate).Average(e => (double)e.Earnings)) /
                                     statistics.Where(d => d.DateTime > second_startdate && d.DateTime <= second_enddate).Average(e => (double)e.Earnings)) : zero,
+1  A: 

I remember my instructors calling it 'undefined' as opposed to infinity.

From wikipedia:

Division of any number by zero (where the divisor is zero) is not defined. This is because zero added to zero, no matter how many times the equation is repeated, will always result in a sum of zero. Entry of such an equation into most calculators will result in an error message being issued.

Ken Browning
Where both values are zero - i'm quite happy with calling the difference zero. It is the cases where one of the values is zero
Harry
Yeah, I'm pretty sure its undefined, x/0 is not infinity.
Iuvat
Harry
+3  A: 

You arbitrarily set % change to X where X is the reasonable default for your problem space. However it's not really a programming question. It's a question of what you expect. What should happen as any number approaches zero is a function of the problem space. For instance I worked once on an accounting "function". The function was nothing special, it just translated a scoring method to code. The problem was in the scoring method and that no one had ever defined what the default values were as some denominators went to zero. For any given accounting score, what happened at zero might be full points, partial points, or 0 points (on the grading scale) depending on the details of what was being measured.

Determine what you are measuring and substitute a reasonable default for zero. If you are measuring performance, make sure someone can't game the system by intentionally having $0 weeks followed by high dollar weeks (for instance).

Russell Steen
good point, the statistics in my case are not likely to suffer from any tampering.
Harry
+1  A: 

What to do in that situation is subjective, and likely depends on your business needs.

I would prefer Not Available if there is no other choice.

100% may make sense.

Maybe use 1 instead of zero may also make sense resulting in 2560% in your example.

ScottS
Great idea. 2560% is potentially more meaningful to the user. And it allows the various zero cases to sort amoungst themselves.
Harry
+1  A: 

I would store either NaN or infinity, or whatever value makes sense, but when displayed to the user, I would leave the percent blank or somehow indicate that a percent doesn't make sense then.

Mike Cooper
Yes it seems this is the real problem with my question - it is one of expectations. What does the user expect when they sort the list by percentage difference? Will infinity make sense? or should i reduce it to zero?
Harry
A: 

While technically the answer is either undefined (in the $0/$0 case) or infinity/-infinity (all other cases) the human isn't going to like either answer. I would display something like an or the like if I couldn't find out from the users what would be the best thing to do here.

Loren Pechtel