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

用于確定兩個數字在舍入到 n 個有效十進制數字

Function to determine if two numbers are nearly equal when rounded to n significant decimal digits(用于確定兩個數字在舍入到 n 個有效十進制數字時是否幾乎相等的函數)
本文介紹了用于確定兩個數字在舍入到 n 個有效十進制數字時是否幾乎相等的函數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我被要求測試第 3 方提供的庫.眾所周知,該庫精確到 n 個有效數字.任何不太重要的錯誤都可以安全地忽略.我想寫一個函數來幫助我比較結果:

I have been asked to test a library provided by a 3rd party. The library is known to be accurate to n significant figures. Any less-significant errors can safely be ignored. I want to write a function to help me compare the results:

def nearlyequal( a, b, sigfig=5 ):

此函數的目的是確定兩個浮點數(a 和 b)是否近似相等.如果 a==b(完全匹配)或者如果 a 和 b 在以十進制形式寫入時舍入到 sigfig 有效數字時具有相同的值,則該函數將返回 True.

The purpose of this function is to determine if two floating-point numbers (a and b) are approximately equal. The function will return True if a==b (exact match) or if a and b have the same value when rounded to sigfig significant-figures when written in decimal.

任何人都可以提出一個好的實施方案嗎?我寫了一個迷你單元測試.除非你能在我的測試中看到一個錯誤,否則一個好的實現應該通過以下:

Can anybody suggest a good implementation? I've written a mini unit-test. Unless you can see a bug in my tests then a good implementation should pass the following:

assert nearlyequal(1, 1, 5) 
assert nearlyequal(1.0, 1.0, 5) 
assert nearlyequal(1.0, 1.0, 5) 
assert nearlyequal(-1e-9, 1e-9, 5) 
assert nearlyequal(1e9, 1e9 + 1 , 5) 
assert not nearlyequal( 1e4, 1e4 + 1, 5) 
assert nearlyequal( 0.0, 1e-15, 5 ) 
assert not nearlyequal( 0.0, 1e-4, 6 ) 

補充說明:

  1. 值 a 和 b 可能是 int、float 或 numpy.float64 類型.值 a 和 b 將始終屬于同一類型.轉換不會給函數帶來額外的錯誤,這一點至關重要.
  2. 讓我們保留這個數字,因此轉換為字符串或使用非數學技巧的函數并不理想.該程序將由一位數學家審核,他希望能夠證明該函數完成了它應該做的事情.
  3. 速度...我要比較很多數字,所以越快越好.
  4. 我有 numpy、scipy 和標準庫.其他任何東西對我來說都很難得到,尤其是對于項目的這么小部分.

推薦答案

numpy.testing中有一個函數assert_approx_equal(來源這里)這可能是一個很好的起點.

There is a function assert_approx_equal in numpy.testing (source here) which may be a good starting point.

def assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=True):
    """
    Raise an assertion if two items are not equal up to significant digits.

    .. note:: It is recommended to use one of `assert_allclose`,
              `assert_array_almost_equal_nulp` or `assert_array_max_ulp`
              instead of this function for more consistent floating point
              comparisons.

    Given two numbers, check that they are approximately equal.
    Approximately equal is defined as the number of significant digits
    that agree.

這篇關于用于確定兩個數字在舍入到 n 個有效十進制數字時是否幾乎相等的函數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Python 3 Float Decimal Points/Precision(Python 3 浮點小數點/精度)
Converting Float to Dollars and Cents(將浮點數轉換為美元和美分)
What are some possible calculations with numpy or scipy that can return a NaN?(numpy 或 scipy 有哪些可能的計算可以返回 NaN?)
Python float to ratio(Python浮動比率)
How to manage division of huge numbers in Python?(如何在 Python 中管理大量數字的除法?)
mean from pandas and numpy differ(pandas 和 numpy 的意思不同)
主站蜘蛛池模板: 午夜视频一区 | 青青五月天 | 99热网站 | 波多野结衣亚洲一区 | 日韩欧美综合 | 精品国产乱码久久久久久蜜柚 | 亚洲视频一区 | 五十路av| 青青草免费在线视频 | 免费精品视频 | 欧美日韩高清 | 日韩视频一区二区三区 | 午夜av在线播放 | 亚洲黄色在线视频 | 91青青 | 免费一级黄色录像 | 午夜视频在线免费观看 | 日韩在线视频一区二区三区 | 懂色av一区二区三区 | 亚洲欧美视频在线观看 | 青青伊人网 | 中文字幕第5页 | 久久国产美女 | 国产美女免费视频 | 国产高清在线观看 | 国产又粗又黄又爽又硬的视频 | 欧美日韩免费在线观看 | 久久麻豆视频 | 国产日韩精品一区二区 | 黄色福利视频 | 国产无精乱码一区二区三区 | 日本黄色三级视频 | 免费av在线网站 | 欧美丰满少妇 | 国产午夜激情 | 日韩在线免费观看视频 | 在线观看网址你懂的 | 涩涩97| 日本成人精品 | 国产精品永久久久久久久久久 | 超碰在线人人 |