numpy 或 scipy 有哪些可能的计算可以返回 NaN?

What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的计算可以返回 NaN?)
本文介绍了numpy 或 scipy 有哪些可能的计算可以返回 NaN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在 Python 中,哪些最常见的操作会导致使用 NumPy 或 SciPy 时产生的 NaN?

What are the most common operations that would cause a NaN, in Python, which originate while working with NumPy or SciPy?

例如:

1e500 - 1e500
>>> nan

这种行为的原因是什么,为什么它不返回 0?

What is the reasoning for this behavior and why does it not return 0?

推荐答案

如果您执行以下任何一项操作而没有在浮点环境中徘徊,您应该得到一个之前没有的 NaN:

If you do any of the following without horsing around with the floating-point environment, you should get a NaN where you didn't have one before:

p>

  • 0/0(顶部和底部都可以)
  • inf/inf(在顶部和底部签名)
  • inf - inf(-inf) + infinf + (-inf)(-inf) -(-inf)
  • 0 * infinf * 0(在两个因子上都签名)
  • sqrt(x)x <0
  • fmod(x, y)y = 0x 是无限的;这里 fmod 是浮点余数.
    • 0/0 (either sign on top and bottom)
    • inf/inf (either sign on top and bottom)
    • inf - inf or (-inf) + inf or inf + (-inf) or (-inf) - (-inf)
    • 0 * inf and inf * 0 (either sign on both factors)
    • sqrt(x) when x < 0
    • fmod(x, y) when y = 0 or x is infinite; here fmod is floating-point remainder.

    机器算术这些方面的规范参考是 IEEE 754规范.第 7.1 节描述了无效操作异常,这是在您即将获得 NaN 时引发的异常.IEEE 754 中的异常"与编程语言上下文中的含义不同.

    The canonical reference for these aspects of machine arithmetic is the IEEE 754 specification. Section 7.1 describes the invalid operation exception, which is the one that is raised when you're about to get a NaN. "Exception" in IEEE 754 means something different than it does in a programming language context.

    许多特殊的函数实现记录了它们在尝试实现的函数的奇异点处的行为.例如,参见 atan2log 的手册页.

    Lots of special function implementations document their behaviour at singularities of the function they're trying to implement. See the man page for atan2 and log, for instance.

    您具体询问的是 NumPy 和 SciPy.我不确定这是否只是简单地说我在询问 NumPy 引擎盖下发生的机器算法"还是我在询问 eig() 之类的东西".我假设是前者,但这个答案的其余部分试图与 NumPy 中的高级函数建立模糊的联系.基本规则是:如果一个函数的实现犯了上述罪过之一,你会得到一个 NaN.

    You're asking specifically about NumPy and SciPy. I'm not sure whether this is simply to say "I'm asking about the machine arithmetic that happens under the hood in NumPy" or "I'm asking about eig() and stuff." I'm assuming the former, but the rest of this answer tries to make a vague connection to the higher-level functions in NumPy. The basic rule is: If the implementation of a function commits one of the above sins, you get a NaN.

    例如,对于 fft,如果您的输入值在 1e1010 左右或更大并且无声,您很可能会得到 NaNs如果您的输入值在 1e-1010 左右或更小,则会丢失精度.不过,除了真正可笑的缩放输入之外,使用 fft 是相当安全的.

    For fft, for instance, you're liable to get NaNs if your input values are around 1e1010 or larger and a silent loss of precision if your input values are around 1e-1010 or smaller. Apart from truly ridiculously scaled inputs, though, you're quite safe with fft.

    对于涉及矩阵数学的事情,如果您的数字很大您的矩阵非常病态,NaN 可能会突然出现(通常通过 inf - inf 路线).关于如何被数值线性代数搞砸的完整讨论太长了,不属于答案.我建议您花几个月的时间阅读一本数值线性代数书(Trefethen 和 Bau 很受欢迎).

    For things involving matrix math, NaNs can crop up (usually through the inf - inf route) if your numbers are huge or your matrix is extremely ill-conditioned. A complete discussion of how you can get screwed by numerical linear algebra is too long to belong in an answer. I'd suggest going over a numerical linear algebra book (Trefethen and Bau is popular) over the course of a few months instead.

    在编写和调试不应该"生成 NaN 的代码时,我发现有用的一件事是告诉机器在发生 NaN 时进行陷阱.在 GNU C 中,我这样做:

    One thing I've found useful when writing and debugging code that "shouldn't" generate NaNs is to tell the machine to trap if a NaN occurs. In GNU C, I do this:

    #include <fenv.h>
    feenableexcept(FE_INVALID);
    

    这篇关于numpy 或 scipy 有哪些可能的计算可以返回 NaN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

patching a class yields quot;AttributeError: Mock object has no attributequot; when accessing instance attributes(修补类会产生“AttributeError:Mock object has no attribute;访问实例属性时)
How to mock lt;ModelClassgt;.query.filter_by() in Flask-SqlAlchemy(如何在 Flask-SqlAlchemy 中模拟 lt;ModelClassgt;.query.filter_by())
FTPLIB error socket.gaierror: [Errno 8] nodename nor servname provided, or not known(FTPLIB 错误 socket.gaierror: [Errno 8] nodename nor servname provided, or not known)
Weird numpy.sum behavior when adding zeros(添加零时奇怪的 numpy.sum 行为)
Why does the #39;int#39; object is not callable error occur when using the sum() function?(为什么在使用 sum() 函数时会出现 int object is not callable 错误?)
How to sum in pandas by unique index in several columns?(如何通过几列中的唯一索引对 pandas 求和?)