Disclaimer: No, I didn't find any obvious answer, contrary to what I expected!
When looking for code examples wrt. the arithmetic mean, the first several examples I can turn up via Google seem to be defined such that the empty sequence generates a mean value of 0.0
. (eg. here and here ...)
Looking at Wikipedia however, the Arithmetic mean is defined such that an empty sequence would yield 0.0 / 0
--
A = 1/n ∑[i=1 -> n](a[i])
-- so, possibly, that is NaN in the general case.
So if I write a utility function that calculates the arithmetic mean of a set of floating point values, should I, in the general case:
0.
for the empty sequence?(Q)NaN
for the empty sequence?An empty sequence is a (finite) sequence containing no terms. Thus an empty sequence is a mapping from ∅ to S, that is, the empty mapping. Thus by definition an empty sequence is a (finite) sequence whose length is 0.
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 S0.
In mathematics, an arithmetic sequence is a succession of integers in which the value of each number grows or decreases by a fixed amount each term. When an arithmetic sequence has n terms, we may construct a formula for each term in the form fn+c, where d is the common difference.
Solution: A sequence in which the difference between all pairs of consecutive numbers is equal is called an arithmetic progression. Therefore, the 25th term is 147. 1 What is the 25th term of the arithmetic sequence 7 11 15? 2 How do you find the 25th term of an AP? 3 What is the 25th term if the first term is and the common difference is?
Find the Missing Terms in the Arithmetic Sequence Calculator: If you are interested to calculate the missing terms then use our missing terms in the arithmetic sequence calculator tool that gives the best error-free results. Give some input and immediately get some output.
An arithmetic sequence is a set of numbers that has a difference between two consecutive terms that are constant. The sequence of numbers is represented as commas, i.e., 1,2,3,4,..... a n = nth term of the sequence. a 1 = 1st term of the sequence. d = common difference.
There isn't an obvious answer because the handling depends on how you want to inform calling code of the error. (Or even if you want to interpret this as an "error".)
Some libraries/programs really don't like raising exceptions, so do everything with signal values. In that case, returning NaN (because the value of the expression is technically undefined) is a reasonable choice.
You might also want to return NaN if you want to "silently" bring the value forward through multiple other calculations. (Relying on the behavior that NaN combined with anything else is "silently" NaN.)
But note that if you return NaN for the mean of an empty sequence, you impose the burden on calling code that they need to check the return value of the function to make sure that it isn't NaN - either immediately upon return or later on. This is a requirement that is easy to miss, depending on how fastidious you are in checking return values.
Because of this, other libraries/programs take the viewpoint that error conditions should be "noisy" - if you passed an empty sequence to a function that's finding the mean of the sequence, then you've obviously doing something majorly wrong, and it should be made abundantly clear to you that you've messed up.
Of course, if exceptions can be raised, they need to handled, but you can do that at a higher level, potentially centralized at the point where it makes more sense to. Depending on your program, this may be easier or more along the lines of your standard error handling scheme than double checking return values.
Other people would argue that your functions should be robust to the error. For maximum robustness, you probably shouldn't use either NaN or an exception - you need to choose an actual number which "makes sense" as a value for the average of an empty list.
Which value is going to be highly specific to your use case. For example, if your sequence is a list of differences/errors, you might to return 0. If you're averaging test scores (scored 0-100), you might want to return 100 for an empty list ... or 0, depending on what your philosophy of the "starting" score is. It all depends on what the return value is going to be used for.
Given that the value of this "neutral" value is going to be highly variable based on exact use case, you might want to actually implement it in two functions - one general function which returns NaN or raises an exception, and another that wraps the general function and recognizes the 'error' case. This way you can have multiple versions, each with a different "default" case. -- or if this is something you're doing a lot of, you might even have the "default" value be a parameter you can pass.
Again, there isn't a single answer to this question: the average of an empty sequence is undefined. How you want to handle it depends intimately on what the result of the calculation is being used for: Just display, or further calculation? Should an empty list be exceptional, or should it be handled quietly? Do you want to handle the special case at the point in time it occurs, or do you want to hoist/defer the error handling?
Mathematically, it's undefined as the denominator is zero.
Because the behaviour of integer division by zero is undefined in C++, throw an exception if you're working in integral types.
If you're working in IEEE754 floating point, then return NaN since the numerator will also be zero. (+Inf would be returned if the numerator is positive, and -Inf if the numerator is negative).
I suggest to keep the same behavior as for a 0.0 by 0 division, whatever it is. Indeed, one can adopt the as-if rule. This way you remain coherent with other operations and you don't have to make the decision yourself.
(You could even implement it as such, by returning 0.0/0, but the compiler might optimize this in unexpected ways.)
I like defensive coding, so I would throw an exception. You can make it either a specific exception (like empty_sequence_exception) or a division by 0, since the divider is the length of the sequence which is 0.
0.0 is debatable since there is no data (sequence).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With