Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

I have two programs that both behave nearly identically: they both take in any numbers you give them and can tell you the average and how many numbers were given. However, when you don't give them any numbers, one says the average is 0.0, and the other says it's NaN ("Not a Number"). Which of these answers, if any, is more correct, and why?

Note: Although I use "programs" as a metaphor here, this isn't a programming question; I could've just as easily said "computers", "machines", "wise men", etc. and my question would be the same

share|improve this question
The Empty sum is normally considered to be zero. Your second program perhaps computed the empty sum and then divided by the number of terms being summed... that is, $0/0$. –  Mike Miller Aug 26 '14 at 4:39
NaN is better because how can you find the average without numbers. –  Freddy Aug 26 '14 at 4:40
From a programming standpoint, NaN is better than 0, because 0 is a possible legitimate average. (Sample: { -1, 1 }, or { 0, 0, 0 }). –  friedo Aug 26 '14 at 7:09
As a programmer, I'd expect to get (and would supply) null rather than either NaN or 0... –  Brian Knoblauch Aug 26 '14 at 14:42
@Cruncher, in Java, I would expect Float.NaN or Double.NaN over null. After all, my method should be returning a float or double, and null can only be returned for Object types. –  Brian S Aug 26 '14 at 16:28

11 Answers 11

up vote 80 down vote accepted

From a statistical point of view, the average of no sample points should not exist. The reason is simple. The average is an indication of the centre of mass of the distribution. Clearly, for no observations there can be no way to prefer one location vs. another as their centre of mass since the the empty set is translation invariant.

More mathematically, the average is linear, which means if you add a constant $c$ to each observation, then the average $a$ becomes $a+c$. Now if you add $c$ to each observation in the empty set, you get the empty set again and thus the average will have to satisfy $a+c=a$ for all $c$, clearly nonsense.

share|improve this answer
Couldn't one say, then, that there is zero mass for all values of $x$ between $-\infty$ and $\infty$? In that case, the sum of all zeroes would be, itself, $0$ –  Supuhstar Aug 26 '14 at 14:53
Adding 1 to every number should shift the mean by 1 (i.e., make it 1 instead of 0). But $1 + \mathbb{R} = \mathbb{R}$. –  asmeurer Aug 26 '14 at 19:03
@Mehrdad yes, the empty set does not change upon a translation. But the mean does translate with a translation, and thus one can't associate a translation consistent value for the mean of the empty set. That is what the second paragraph is saying and I'd appreciate it if you say what is wrong with that. –  Ittay Weiss Aug 27 '14 at 10:27
@Mehrdad you need to understand the difference between: "translate the set first, then take the mean", and "first take the mean, then translate it". The mean has the property that these operations yield the same result. What the second paragraph does is conclude no assignment of the mean to the empty set still has this property and uses it as a reason against assigning any value to the mean of empty mean. This is standard extrapolation from general properties to extreme cases. –  Ittay Weiss Aug 27 '14 at 10:35
@Mehrdad The arithmetic mean satisfies this identity: $\text{mean}\ (\text{map}\ (c+{})\ X) = c + \text{mean}\ X$. Substituting $\{\}$ for $X$, we get $\text{mean}\ (\text{map}\ (c+{})\ \{\}) = c + \text{mean}\ \{\}$. Mapping over the empty multiset gives the empty multiset, so $\text{mean}\ \{\} = c + \text{mean}\ \{\}$. That's a contradiction ($c$ is arbitrary), so either the identity doesn't hold or $\text{mean}\ \{\}$ doesn't exist. The least objectionable choice is the latter. –  James Wood Aug 27 '14 at 14:16

The Fréchet mean generalizes the concept of mean to arbitrary metric spaces. It is the point which minimizes the sum-of-squared distances between elements of the dataset $X$: $$\text{arg}\min_\bar{x} \sum_{x\in X} d(\bar{x},x)^2$$

In the case that $X=\varnothing$, the summation is the empty sum and hence $0$, thus there is no minimizer and the mean is undefined.

However, in general there are multiple points that minimize this sum (consider the dataset consisting of a pair of antipodal points on the sphere), so we should not speak of the mean, rather we should consider the set of such points as mean. Then, the empty set has as mean the entire space (presumably $\mathbb{R}$ in this case).

To directly answer the question, NaN would be better, since being undefined is certainly not-a-number and, likewise, a set is not-a-number.

share|improve this answer
+1. Dumb question: why is $\text{arg}\min_\bar{x} 0$ undefined? $\min_\bar{x} 0$ is obviously $0$, then $\text{arg}\min_\bar{x} 0$ should be the set of values $\bar{x}$ for which the sum is $0$. That's any (presumably real) number. –  nikie Aug 26 '14 at 13:58
@nikie This is just a technicality: the Fréchet mean is defined to be the unique minimizer. When there is no uniqueness, it is undefined. You are correct, however, that $\operatorname{arg min}$ is $\mathbb{R}$ when $X=\emptyset$. –  Slade Aug 26 '14 at 17:40

The correct answer is "Error: Cannot compute the average without any numbers. Please enter at least one number."

$0$ is incorrect, because division by $0$ is undefined, not $0$: $\frac00\neq 0$. If you have $0$ elements, you simply cannot compute their average.

NaN is slightly better, but still kind of wrong. It's a special value of the IEEE floating point standard and represents the result of a calculation that is undefined. It's an implementation detail of how numbers work under the hood, not a proper result to show to the user. You should at least translate it into something like "undefined" or "N/A". Or display a message to explain why there is no result (see above).

share|improve this answer
How is NaN an improper result to show to a user? "undefined" or "N/A" would just be different versions of NaN. Why invent two types of the same object? –  asmeurer Aug 26 '14 at 15:57
I would agree in the general case that NaN is not a value you should be showing the user (depending on target audience there may be exceptions). However, I disagree that NaN is the wrong result of the computation. –  Brian S Aug 26 '14 at 16:32
@asmeurer The average user has no idea what NaN means. It's jargon. –  Sebastian Negraszus Aug 26 '14 at 17:22

The average of an empty collection of numbers is clearly undefined, as is the centroid of the empty set (or a set of measure zero, for that matter). Therefore the value $0$ given by one of your computers is wrong.

What a clever computer should say in such a case depends on the implementation. I'd expect at least some sort of error message, but certainly not an overflow alert.

share|improve this answer
This is my favourite answer, "as is the centroid of a set of measure zero" (+1) –  this is much healthier Aug 26 '14 at 11:24
NaN is like an embedded error message. Just overflow is expected to be +Inf. –  Vi0 Aug 26 '14 at 12:15
I doubt $10^{gogol}$ would overflow into NaN. NaN as a float is $x 2^{11111111_b}$ =) –  Brian S Aug 26 '14 at 16:36
@thisismuchhealthier As an example, the center of mass of Cantor's set (a supspace of $[0,1]$) does not exist, wrt. the Lebesgue measure? –  Jeppe Stig Nielsen Aug 28 '14 at 9:46

The average of $n$ numbers is their sum divided by $n$.

If $n=0$ then the sum of $0$ numbers is $0$. But dividing by $0$ will result in a computation error.

Either answer can be taken as correct, depending on your needs.

  • If you want the average of $0$ numbers to be defined, make it $0$ (since it's the only sane choice),
  • and if you want it to be indeterminate (e.g. you want to make a claim like "the average of $a_1,\ldots,a_n$ is the unique $a$ such that $n\cdot a=a_1+\ldots+a_n$", in which case for $n=0$ any $a$ would work) then leave it as an indeterminate.
share|improve this answer
To be philosophical, I'd argue against the first answer, or at least would refine it by saying that the 'sane choice' for the average depends on context. For example, suppose we have $n$ bicycles, and that $a_n$ is the number of wheels on the $n$th bicycle. Then the average $a$ will be $2$ for any $n > 0$; and for $n=0$, wouldn't the sane choice be to again define $a=2$? –  Théophile Aug 26 '14 at 5:16
Théophile, well, I argue that $0$ is still a sane choice in that case. The case of no bike is clearly related to a psychotic episode, and anything which has a positive number of wheels clearly shows symptoms of insanity. :-) –  Asaf Karagila Aug 26 '14 at 5:54
What possible reason is there to consider $0$ a sane choice? Averages are supposed to be translation-invariant, so there's nothing to distinguish $0$ from any other real. Maybe we're averaging wheels, or degrees Fahrenheit. What is the average of no temperatures? Zero? Zero what? –  Slade Aug 26 '14 at 9:26
@you-sir-33433: IF you insist that the average of no numbers is to be defined, then my claim is that $0$ is the sane choice. Note that boldface capital letters "if". I never said that one answer is preferable to another. –  Asaf Karagila Aug 26 '14 at 9:43
I agree that 0 is a sane choice if it needs to be defined - and in practical scenarios, it might need to be. I would also suggest that if you are taking a mean of something with a known or expected population mean, some other value might be a good default to choose, such as said expected mean. Imputation basically works this way - when NaN doesn't suffice, and you need a point for each day, picking a point that makes sense with the other points is a reasonable choice.' –  Joe Aug 27 '14 at 19:38

In support of the Frechet mean argument, the mean of no numbers is defined as 0/0. This is the solution of the equation x * 0 = 0 (from the definition of division) which is solved by any x. So the mean is any number.

Note that 0/0 is different from a/0 where a != 0, because x * 0 = a has no solutions.

However there is no way to represent "any number" as a double, so if you want a function returning double I would say NaN is the best. Or in C++ you could throw different exceptions to distinguish these cases say NoNumberException and AnyNumberException.

The correct behaviour of a program depends upon the use for which it is intended. I would need to see your User Requirements :-)

share|improve this answer

For IEEE 754-1985, the correct result would either be ±infinity with the Division by zero error flag set if you view the average as sum / count. The other option, if you view the empty set to be the correct answer, would be to return 0 with the Underflow error flag set indicating that the answer is incorrect, but 0 is the nearest value.

The determination as to which is more correct would require a more detailed knowledge of the context.


share|improve this answer
Under IEEE 754, the result of $\dfrac{\pm0}{\pm0}$ is $\rm NaN$, not $\pm\infty$. –  Ilmari Karonen Aug 28 '14 at 15:10
Precisely, Ilmari –  Joe Blow Aug 28 '14 at 15:17
@IlmariKaronen I provided my source, please provide yours. –  psaxton Sep 2 '14 at 19:36
Thank you for the reference, however that is not the behavior defined in IEEE 754-1985 as I have specified and referenced. –  psaxton Sep 3 '14 at 20:36

The average of $n$ real numbers is defined to be their sum divided by $n$. When there are no numbers, i.e. when $n=0$, we have that the average is $\frac{S}{0}$. It doesn't matter what $S$ equals to (even though by convention the empty sum $S$ is usually taken to be $0$ because of some technical and conceptual reasons) - any real number divided by zero is undefined because it breaks math.

share|improve this answer

Others have already given a number of excellent answers, however I'll give one more idea for why an empty average shouldn't be zero, but should instead be undefined. My reasoning is somewhat in line with Ittay's. The point I'd make is that averages of non-empty sets make sense in affine spaces, which look like a vector space where we've forgotten the privileged basepoint.

For example: think about Newtonian Physics or even Special Relativity. In these cases space(time) has a fundamentally affine underlying model. Nevertheless we can take averages of points, for example when looking for centers of mass. But to say the empty average is zero is ridiculous, as zero was arbitrarily chosen.

share|improve this answer

Is this a mathematics question, or a programming question?

If you are asking about the proper behavior for the two programs you have, then my proposal is that the correct output would be throwing an exception (exiting with an error message if they are standalone programs).

Returning zero is unacceptable: the input (empty set) is clearly incorrect for the operation performed (averaging), so the return value can not be a valid one. Returning a NaN is roughly equivalent to the proposed solution of raising an exception/exiting with error message.

share|improve this answer
this is math. I use the programs as a metaphor in the same way "computer", "machine", "wise man", etc. has been used in the past. –  Supuhstar Aug 27 '14 at 14:01

You can have an average of whatever is input (assuming we are not looking at PI which I do not know as having an ending). If you press Enter or whatever method is used to cause an input into the program, then the programming language or input design of the program will interpret that information, likely perform some calculation, then give the result of the calculation.

What I mean is, you can just press enter (with no other value entered) and (depending on the programming language or design of the program) the program can interpret your value (if reading integers) as Zero. Some languages may interpret the input as a non value or nul and may output something similar to your 'NaN'.

However, in answer to your first question, the average of one thing is always that thing. Unless you can only have an average until you have two or more of something to create an average. So whatever is input, as long as it was only one input, would result in that input as being the average.

To answer your ending question, I would say the NaN is more correct because just pressing enter (assuming that is how the data is input) and is done so with the intent of at least not giving a specific value. That is the answer for the person inputting the data. The answer may also depend on the programmers intent on what type of input they are looking for --- numerical (0 may be correct) or non-numerical (Nan is correct).

share|improve this answer
-1 you're just restating my question in a confusing way that makes me think you don't know what you're talking about. If you can boil down your answer to one paragraph and clear up your English, I might vote it up –  Supuhstar Aug 27 '14 at 2:21
You nearly lost me entirely at the $\pi$ thing. I took the time to read through the rest, however, and your last paragraph, and your last sentence in particular, almost seem reasonable. –  String Aug 28 '14 at 8:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.