在Python中可能导致NaN的所有可能的计算是什么? [英] What are all the possible calculations that could cause a NaN in Python?

查看:2359
本文介绍了在Python中可能导致NaN的所有可能的计算是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在四处搜寻,似乎在不同的编程语言中分散讨论 NaN s,包括一些特定情况,但没有详尽或清楚。 / p>

使用NumPy或SciPy时,最常见的操作是在Python中产生 NaN

解决方案

如果您执行以下任何操作,而不考虑浮点环境,则应该得到一个NaN以前没有一个:


  • 0/0 底部)

  • inf / inf (或者在顶部和底部注明)
  • inf-inf ( - inf)+ inf inf +(-inf)( - inf) - (-inf)

  • 0 * inf $ lt; code> sqrt(x) / code>当 x < 0

  • fmod(x,y) when y = 0 x 是无限的;这里 fmod 是浮点数余数。

    $ b $

    这些方面的规范参考机器算术是 IEEE 754规范。 7.1节描述了无效的操作异常,这是在你要获得NaN时引发的异常。 IEEE 754中的异常与其在编程语言环境中的含义不同。



    许多特殊函数实现记录了它们在尝试函数的奇点时的行为执行。例如,查看 atan2 log 的手册页。



    你在具体询问NumPy和SciPy。我不确定这是否仅仅是说我正在问NumPy引擎下发生的机器算术还是我正在询问 eig()和东西。我假设前者,但是这个答案的其余部分试图与NumPy中的更高级函数进行模糊的连接。基本的规则是:如果一个函数的实现提交上面的一个罪行,你会得到一个NaN。

    对于 fft ,例如,如果您的输入值在 1e1010附近,您可能会得到 NaN code>或更大,如果您的输入值大约小于code> 1e-1010 或更小,则会导致无声的精度损失。除了真正可以缩放的输入外,尽管对于 fft 来说是非常安全的。

    对于涉及矩阵数学如果你的数字很大,那么NaN可能会出现(通常通过 inf-inf 路径)关于如何通过数值线性代数搞定的完整讨论太长而不能归结于答案。我建议在几个月的时间内通过一个数值线性代数书(Trefethen和Bau是流行的)来代替。



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

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


    I've been searching around, and there appear to be scattered discussions about NaNs in different programming languages, including some specific cases, but nothing exhaustive or clear.

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

    解决方案

    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:

    • 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.

    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.

    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.

    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.

    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.

    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.

    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);
    

    这篇关于在Python中可能导致NaN的所有可能的计算是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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