問題描述
在 Python 中,哪些最常見的操作會導致使用 NumPy 或 SciPy 時產(chǎn)生的 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?
推薦答案
如果您執(zhí)行以下任何一項操作而沒有在浮點環(huán)境中徘徊,您應該得到一個之前沒有的 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) + inf
或inf + (-inf)
或(-inf) -(-inf)
0 * inf
和inf * 0
(在兩個因子上都簽名)sqrt(x)
當x <0
fmod(x, y)
當y = 0
或x
是無限的;這里fmod
是浮點余數(shù).0/0
(either sign on top and bottom)inf/inf
(either sign on top and bottom)inf - inf
or(-inf) + inf
orinf + (-inf)
or(-inf) - (-inf)
0 * inf
andinf * 0
(either sign on both factors)sqrt(x)
whenx < 0
fmod(x, y)
wheny = 0
orx
is infinite; herefmod
is floating-point remainder.
機器算術(shù)這些方面的規(guī)范參考是 IEEE 754規(guī)范.第 7.1 節(jié)描述了無效操作異常,這是在您即將獲得 NaN 時引發(fā)的異常.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.
許多特殊的函數(shù)實現(xiàn)記錄了它們在嘗試實現(xiàn)的函數(shù)的奇異點處的行為.例如,參見 atan2
和 log
的手冊頁.
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 引擎蓋下發(fā)生的機器算法"還是我在詢問 eig()
之類的東西".我假設是前者,但這個答案的其余部分試圖與 NumPy 中的高級函數(shù)建立模糊的聯(lián)系.基本規(guī)則是:如果一個函數(shù)的實現(xiàn)犯了上述罪過之一,你會得到一個 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
左右或更大并且無聲,您很可能會得到 NaN
s如果您的輸入值在 1e-1010
左右或更小,則會丟失精度.不過,除了真正可笑的縮放輸入之外,使用 fft
是相當安全的.
For fft
, for instance, you're liable to get NaN
s 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
.
對于涉及矩陣數(shù)學的事情,如果您的數(shù)字很大或您的矩陣非常病態(tài),NaN 可能會突然出現(xiàn)(通常通過 inf - inf
路線).關(guān)于如何被數(shù)值線性代數(shù)搞砸的完整討論太長了,不屬于答案.我建議您花幾個月的時間閱讀一本數(shù)值線性代數(shù)書(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.
在編寫和調(diào)試不應該"生成 NaN 的代碼時,我發(fā)現(xiàn)有用的一件事是告訴機器在發(fā)生 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);
這篇關(guān)于numpy 或 scipy 有哪些可能的計算可以返回 NaN?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!