Demmel, Applied Numerical Linear Algebra, SIAM, 1997. But the correct value depends on both your application and your algorithm. I noticed in the documentation for version 2.6.1 that there is an epsilon attribute in sys.float_info, so I would use twice that value as the default epsilon. If something besides a floating point value is provided, a TypeError is raised.Īt this point, the only difficult thing is setting the correct value for epsilon. If either the exact or the approximate value is zero, than the error is equal to the value of the other. Raise TypeError,"Second argument is not a float." Raise TypeError,"First argument is not a float." def roundoff_error(exact, approximate):ĭef float_equal(float1, float2, epsilon=2.0e-9): Does not verify that the arguments are floating point values.Division by zero error if the exact value is Zero.There are a couple obvious deficiencies with this code. Return (roundoff_error(float1, float2) < epsilon) Floating point equality def float_equal(float1, float2, epsilon=2.0e-9): Roundoff error def roundoff_error(exact, approximate): I haven't used python in some time and only have version 2.4.3, but I'll try to get this correct. (2) Check that the roundoff error is less than some epsilon. I solve it based on the discussion in Section 1.5 of Demmel. 1 possible duplicate of What does plus equals (+) do in Python AndiDog at 8:22 3 AndiDog While it's true both questions are about the (+) operator, the one you linked is about a sophisticated usage and subtle problem, and the OP here is probably not able to follow the reasoning there (yet). This is a fairly common issue with floating point numbers. Anything else will be hard for me to get, especially for such a small part of the project. I've got numpy, scipy and the standard-library.I've got to compare a lot of numbers so the faster the better. This program will be audited by somebody who is a mathematician who will want to be able to prove that the function does what it is supposed to do. Lets keep this numerical, so functions that convert to strings or use non-mathematical tricks are not ideal.It's vital that conversion does not introduce additional error into the function. Values a and b will always be of the same type. Values a and b might be of type int, float or numpy.float64.Unless you can see a bug in my tests then a good implementation should pass the following: assert nearlyequal(1, 1, 5) 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.Ĭan anybody suggest a good implementation? I've written a mini unit-test. The purpose of this function is to determine if two floating-point numbers (a and b) are approximately equal. I want to write a function to help me compare the results: def nearlyequal(a, b, sigfig=5): Any less-significant errors can safely be ignored. The library is known to be accurate to n significant figures. I have been asked to test a library provided by a 3rd party.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |