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

前端 未结 11 1889

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:14

    Here's a take.

    def nearly_equal(a,b,sig_fig=5):
        return ( a==b or 
                 int(a*10**sig_fig) == int(b*10**sig_fig)
               )
    

提交回复
热议问题