久久久久久久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 時產生的 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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
Function to determine if two numbers are nearly equal when rounded to n significant decimal digits(用于確定兩個數字在舍入到 n 個有效十進制數字時是否幾乎相等的函數)
主站蜘蛛池模板: 免费的黄色小视频 | 亚洲一区二区三区视频 | 午夜免费小视频 | 黄色一级在线观看 | 国产日韩一区二区 | 在线黄网 | 成人在线视频免费 | 狠狠久 | 日日操日日干 | 亚洲国产精品一区二区三区 | 欧美日韩免费视频 | 日韩免费在线视频 | 欧美精品久久久久久久 | 91激情网| 四虎久久 | 日韩精品视频免费在线观看 | 成人一级黄色片 | 亚洲五月婷婷 | 免费av一区 | 美女国产精品 | 国产黄色片在线观看 | 久久久久久久影院 | 欧美日韩少妇 | 日韩一二区| 欧美日韩成人一区二区三区 | 久久高清免费视频 | 国产色播 | 天天干天天做 | 精品国产成人 | 日韩av一级| 美女久久久久久 | 久色成人 | 久久久久国产一区二区三区 | 欧美成人一级片 | 欧美顶级黄色大片免费 | 国产高清自拍 | 黄色激情视频在线观看 | 成人不卡 | 成年免费视频黄网站在线观看 | 人人超碰人人 | 真实的国产乱xxxx在线 |