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

前端 未结 11 1857

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 02:48

    Oren Shemesh got part of the problem with the problem as stated but there's more:

    assert nearlyequal( 0.0, 1e-15, 5 )

    also fails the second definition (and that's the definition I learned in school.)

    No matter how many digits you are looking at, 0 will not equal a not-zero. This could prove to be a headache for such tests if you have a case whose correct answer is zero.

提交回复
热议问题