久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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 時產(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) + infinf + (-inf)(-inf) -(-inf)
  • 0 * infinf * 0(在兩個因子上都簽名)
  • sqrt(x)x <0
  • fmod(x, y)y = 0x 是無限的;這里 fmod 是浮點余數(shù).
    • 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.

    機器算術(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ù)的奇異點處的行為.例如,參見 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 引擎蓋下發(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 左右或更大并且無聲,您很可能會得到 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.

    對于涉及矩陣數(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)!

    【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數(shù)點/精度)
Converting Float to Dollars and Cents(將浮點數(shù)轉(zhuǎn)換為美元和美分)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數(shù)字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
Function to determine if two numbers are nearly equal when rounded to n significant decimal digits(用于確定兩個數(shù)字在舍入到 n 個有效十進制數(shù)字時是否幾乎相等的函數(shù))
主站蜘蛛池模板: 亚洲电影一级片 | 一区二区三区欧美大片 | 孕妇一级毛片 | 欧美性jizz18性欧美 | 波多野结衣中文视频 | 久久久久久国 | 欧产日产国产精品v | 国产永久免费 | 特黄色一级毛片 | 亚洲一区国产精品 | 精品一区二区久久久久久久网站 | 99国产视频 | 午夜视频一区二区三区 | 欧美福利在线 | 一区在线观看视频 | 日韩视频在线免费观看 | 国产精品无码专区在线观看 | 久久久久九九九女人毛片 | 国产成人自拍av | 狠狠入ady亚洲精品经典电影 | 天天干天天草 | 欧美精品在线一区 | 国产精品视频偷伦精品视频 | 亚洲一区久久 | 成人精品一区二区三区中文字幕 | 99久久婷婷国产综合精品电影 | 一区二区三区精品在线 | 91美女在线观看 | 中文字幕国产 | 国产成人99久久亚洲综合精品 | 日韩精品视频一区二区三区 | 久久综合狠狠综合久久 | 欧美一级在线免费 | 日本成人中文字幕在线观看 | 日韩成人在线网站 | 成人免费黄视频 | 国产精品成人一区二区三区吃奶 | 亚洲先锋影音 | a久久久久久 | 日日夜夜草 | 亚洲日日操 |