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

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

What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
本文介紹了numpy 或 scipy 有哪些可能的計算可以返回 NaN?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

在 Python 中,哪些最常見的操作會導(dǎo)致使用 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)境中徘徊,您應(yīng)該得到一個之前沒有的 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) 當(dāng) x <0
  • fmod(x, y) 當(dāng)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() 之類的東西".我假設(shè)是前者,但這個答案的其余部分試圖與 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 是相當(dāng)安全的.

    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ù)學(xué)的事情,如果您的數(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)試不應(yīng)該"生成 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ù))
主站蜘蛛池模板: 色网站在线 | 色婷婷影院 | 天天操人人干 | 久久一区二区三区四区五区 | 久久精品国产亚洲 | 国产精品1区 | 蜜臀久久99精品久久久久野外 | 黄色成人在线观看 | 欧美啪啪| 1204国产成人精品视频 | 亚洲视频欧美视频 | 久久成人国产精品 | 国产成人啪免费观看软件 | 嫩草视频在线看 | 欧美一区二区三区在线播放 | 国产精品自拍啪啪 | 91美女在线观看 | 超碰人人人人 | 高清免费在线 | 成人免费视频网站在线观看 | 色婷婷av777| 国产久| 日韩精品 电影一区 亚洲 | 日韩一区中文字幕 | 久久蜜桃精品 | 日韩精品一区二区三区四区视频 | 久久国产高清 | 日本成人在线观看网站 | 日韩精品久久一区二区三区 | 国产精品一区二区不卡 | 天堂免费看片 | 日干夜操| 日韩一区二区久久 | 日日网| 亚洲国产一区二区三区 | 草久久| www操操| 欧美日日 | 国产91丝袜在线18 | 蜜桃毛片| 91 视频网站 |