summaryrefslogtreecommitdiff
path: root/numpy/testing/utils.py
diff options
context:
space:
mode:
authorDavid Cournapeau <cournape@gmail.com>2009-10-30 10:26:52 +0000
committerDavid Cournapeau <cournape@gmail.com>2009-10-30 10:26:52 +0000
commitb89aaa473462dae66fc3eaaa9712702d30e86dcf (patch)
treea0a9a03182426dc7509d7574657c1f73042e39e0 /numpy/testing/utils.py
parent19b5dea4e52c96c08ee364295137f69af15c724e (diff)
downloadnumpy-b89aaa473462dae66fc3eaaa9712702d30e86dcf.tar.gz
ENH: add a function to compute the signed-magnitude interpretation of the binary repr of a float.
Diffstat (limited to 'numpy/testing/utils.py')
-rw-r--r--numpy/testing/utils.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py
index e5e86250f..dd9974bef 100644
--- a/numpy/testing/utils.py
+++ b/numpy/testing/utils.py
@@ -1082,3 +1082,27 @@ def _assert_valid_refcount(op):
assert(sys.getrefcount(i) >= rc)
+def _integer_repr(x, vdt, comp):
+ # Reinterpret binary representation of the float as sign-magnitude:
+ # take into account two-complement representation
+ # See also
+ # http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm
+ rx = x.view(vdt)
+ if not (rx.size == 1):
+ rx[rx < 0] = comp - rx[rx<0]
+ else:
+ if rx < 0:
+ rx = comp - rx
+
+ return rx
+
+def integer_repr(x):
+ """Return the signed-magnitude interpretation of the binary representation of
+ x."""
+ import numpy as np
+ if x.dtype == np.float32:
+ return _integer_repr(x, np.int32, np.int32(-2**31))
+ elif x.dtype == np.float64:
+ return _integer_repr(x, np.int64, np.int64(-2**63))
+ else:
+ raise ValueError("Unsupported dtype %s" % x.dtype)