久久久久久久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一区二区天堂 | 一级毛片高清 | 天天综合干 | 精国产品一区二区三区四季综 | 欧美性区 | 婷婷久久综合 | 日韩在线中文字幕 | 亚洲va欧美va人人爽午夜 | 一级黄色绿像片 | 国产成人精品久久 | 久久人体| 久久不卡视频 | 久久综合久色欧美综合狠狠 | 4h影视 | 久久综合一区 | 国产视频亚洲视频 | 欧美日韩精品一区 | 精品少妇一区二区三区在线播放 | 国产精品av久久久久久久久久 | 亚洲性视频网站 | 精品小视频 | 自拍偷拍亚洲视频 | 日日夜夜精品视频 | 91精品国产一区二区三区蜜臀 | 亚洲欧美视频在线观看 | 欧美高清一级片 | 一区二区在线免费观看视频 | 亚洲精品国产电影 | 成人精品一区二区 | 日本三级黄视频 | 91av视频在线免费观看 | 精品一区二区三区免费视频 | 国产一区二区在线免费观看 | 一区二区精品视频 | 在线观看精品视频网站 | 天天影视综合 | 4h影视| 精品一区二区三区在线观看国产 | 国产精品久久久久久久久免费樱桃 | 国产免费一区二区三区 |