[pypy-svn] r49536 - pypy/dist/pypy/rpython/test

antocuni at codespeak.net antocuni at codespeak.net
Fri Dec 7 20:36:03 CET 2007


Author: antocuni
Date: Fri Dec  7 20:36:02 2007
New Revision: 49536

Modified:
   pypy/dist/pypy/rpython/test/tool.py
Log:
(antocuni, xoraxax)

a better way to compare floats



Modified: pypy/dist/pypy/rpython/test/tool.py
==============================================================================
--- pypy/dist/pypy/rpython/test/tool.py	(original)
+++ pypy/dist/pypy/rpython/test/tool.py	Fri Dec  7 20:36:02 2007
@@ -22,9 +22,16 @@
         return x == y
 
     def float_eq_approx(self, x, y):
-        diff = abs(x-y)
-        error = diff/y
-        return error < 10**-self.FLOAT_PRECISION
+        maxError = 10**-self.FLOAT_PRECISION
+        if abs(x-y) < maxError:
+            return True
+
+        if abs(y) > abs(x):
+            relativeError = abs((x - y) / y)
+        else:
+            relativeError = abs((x - y) / x)
+
+        return relativeError < maxError
 
     def is_of_type(self, x, type_):
         return type(x) is type_


More information about the pypy-svn mailing list