空序列的算术平均值是多少? [英] What is the arithmetic mean of an empty sequence?

查看:57
本文介绍了空序列的算术平均值是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:不,与我的预期相反,我没有找到任何明显的答案!

查找代码示例时.在算术平均值上,我可以通过Google看到的前几个示例似乎已定义为使得空序列生成平均值 0.0 .(例如,此处算术平均值的定义是,使得空序列会产生 0.0/0 -

  A = 1/n ∑ [i = 1->n](a [i]) 

-因此,在一般情况下为NaN ./p>

因此,如果我编写了一个实用函数来计算一组浮点值的算术平均值,那么在一般情况下,我应该这样做吗?

  • 返回 0.表示空序列?
  • 返回(Q)NaN 以获得空序列?
  • 如果序列为空,抛出异常"吗?

解决方案

没有明显的答案,因为处理方式取决于您要如何告知错误的调用代码.(或者即使您想将此解释为错误".)

某些库/程序确实不喜欢引发异常,因此对信号值进行所有操作.在那种情况下,返回NaN(因为表达式的值在技术上是不确定的)是一个合理的选择.

如果您想无声地"通过其他多次计算将值转为正值,则可能还想返回NaN.(依靠NaN与其他任何东西结合在一起的行为是无声地" NaN.)

但是请注意,如果您以空序列的平均值返回NaN,则会给调用代码增加负担,他们需要检查该函数的返回值以确保它不是NaN-或者在返回时立即返回或以后.这项要求很容易遗漏,具体取决于您检查返回值的准确性.

因此,其他库/程序都认为错误条件应该是嘈杂的"-如果您将空序列传递给寻找序列均值的函数,那么您显然做错了很多事,并且应该向您清楚地表明您已陷入困境.

当然,如果可以引发异常,则需要对其进行处理,但是您可以在更高级别上进行处理,有可能集中在更有意义的地方.根据您的程序,这可能比标准错误处理方案更容易或更有效,而不是仔细检查返回值.

其他人会争辩说您的函数应该对错误具有鲁棒性.为了获得最大的鲁棒性,您可能不应该使用NaN或例外-您需要选择一个有意义"的实际数字作为空列表平均值的值.

哪个值将高度特定于您的用例.例如,如果您的序列是一个差异/错误列表,则可能返回0.如果要对测试分数进行平均(得分为0-100),则可能要为空列表返回100 ...或为0,取决于您的起始"分数的哲学.这完全取决于返回值将用于什么目的.

鉴于此中立"值的值将根据确切的用例而变化很大,因此您可能希望在两个函数中实际实现它:一个返回NaN或引发异常的通用函数,另一个返回包装通用函数并识别错误"情况.这样,您可以拥有多个版本,每个版本都有不同的默认"大小写.-或如果这是您正在做的很多事情,则甚至可以将默认"值作为您可以传递的参数.

同样,这个问题没有一个答案:一个空序列的平均值是不确定的.您要如何处理它,直接取决于计算结果用于什么:仅显示还是进一步计算?空列表应该是例外,还是应该安静地处理?您是否要在特殊情况下及时处理特殊情况,还是要提升/推迟错误处理?

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:

  • return 0. for the empty sequence?
  • return (Q)NaN for the empty sequence?
  • "throw an exception" in case of empty sequence?

解决方案

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?

这篇关于空序列的算术平均值是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆