Re: [SciPy-user] unittests for scipy.stats: assert_almost_equal question
Hmmm, I think that I would like to be testing in terms of significant digits as opposed to decimal places, especially when working with floating point. Since you are asking for such a test, here it is. The attached file has a function meant to go into the scipy_test.py module. I wrote assert_approx_equal following the same form as assert_almost_equal assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=1): compares 'actual' and 'desired' and determines the first 'significant' significant digits and checks for accuracy. Let's see, I think I counted significant digits correctly. Can anyone tell? Louis
From: "eric" <eric@scipy.org> To: <scipy-user@scipy.org> Subject: Re: [SciPy-user] unittests for scipy.stats: assert_almost_equal question Date: Sun, 20 Jan 2002 05:36:31 -0500 Organization: enthought Reply-To: scipy-user@scipy.net
Hey Louis,
<previous message snipped throughout>
A thousand blessing upon you. I immediately commited it to the CVS!
<snip>
The assert_almost_equal function is being called using the
default
setting of decimal=7 Since here it seems I have 10 digits of accuracy, why did this fail?
7 decimals is not 7 significant digits but actually 7 decimal places to the left of 0. The way almost_eqaul works is by using the following comparison: round(abs(desired - actual),decimal) == 0 In this case, abs(desired-actual) is 470. rounding it to the 7 digits past the decimal leaves it as 470 which fails the test of being equal to 0. Maybe a test function needs to be added that tests to a specified number of significant digits for cases like these.
Thanks tons for working on this,
eric
===== When I consider your heavens, the work of your fingers, the moon and the stars, which you have set in place, what is man that you are mindful of him, the son of man that you care for him? Psalm 8:3-4 Y!M, AOL, jabber.com: lluang __________________________________________________ Do You Yahoo!? Send FREE video emails in Yahoo! Mail! http://promo.yahoo.com/videomail/ def assert_approx_equal(actual,desired,significant=7,err_msg='',verbose=1): """ Raise an assertion if two items are not equal. I think this should be part of unittest.py Approximately equal is defined as the number of significant digits correct """ msg = '\nItems are not equal:\n' + err_msg desired = desired/pow(10,math.floor(math.log10(desired))) actual = actual/pow(10,math.floor(math.log10(actual))) try: if ( verbose and len(str(desired)) < 100 and len(str(actual)) ): msg = msg \ + 'DESIRED: ' + str(desired) \ + '\nACTUAL: ' + str(actual) except: msg = msg \ + 'DESIRED: ' + str(desired) \ + '\nACTUAL: ' + str(actual) assert math.fabs(desired - actual) < pow(10.,-1*significant), msg
Hey Louis,
Hmmm, I think that I would like to be testing in terms of significant digits as opposed to decimal places, especially when working with floating point.
We need both in different situations.
Since you are asking for such a test, here it is.
Thanks. In my cursory checks, it looks to me like you've counted digits correctly. I'll add this to scipy_test. Does anyone have better names for these. They are quite arbitrary and indistinguishable right now. How about: assert_significant_equal assert_decimal_equal These names give a little more info about what is happening in the function. thoughts? eric
participants (2)
-
eric
-
Louis Luangkesorn