Function to determine if two numbers are nearly equal when rounded to n significant decimal digits

前端 未结 11 1865

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

11条回答
  •  既然无缘
    2020-12-03 03:10

    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.
    

提交回复
热议问题