久久久久久久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 的意思不同)
主站蜘蛛池模板: 久久久婷婷 | 国产在线精品一区二区三区 | 亚洲国产成人精品女人久久久 | 欧美 日韩 国产 在线 | 日韩三级一区 | 久久男人 | 黄免费在线 | 久久久久久久国产 | 蜜臀av日日欢夜夜爽一区 | 中文一区二区 | 国产欧美日韩一区 | 羞羞的视频在线看 | 成人激情免费视频 | 日本黄视频在线观看 | 羞羞涩涩在线观看 | 成人av激情 | 色综合av | 亚洲码欧美码一区二区三区 | 99成人在线视频 | 中文字幕欧美一区二区 | 日本aaa视频| 成人精品久久 | 国产精品美女久久久久久久网站 | 999久久久久久久久6666 | 亚洲精品一区二区三区 | 国产乱码精品一区二区三区忘忧草 | av大全在线 | 日本久久一区二区三区 | 成人免费视频在线观看 | 日韩中文字幕在线观看 | a级毛片免费高清视频 | 伊人激情网 | 天天精品在线 | 中国美女撒尿txxxxx视频 | 久久偷人 | 免费99精品国产自在在线 | 日韩在线免费 | 午夜色播| 亚洲高清在线观看 | 午夜av成人 | 美女一区二区在线观看 |