Revision: 5525
http://jython.svn.sourceforge.net/jython/?rev=5525&view=rev
Author: pjenvey
Date: 2008-10-29 02:38:55 +0000 (Wed, 29 Oct 2008)
Log Message:
-----------
o fix assert not nan == nan and nan != nan, and cmp on nans
o fix math.frexp(nan) causing an endless loop
Modified Paths:
--------------
trunk/jython/Lib/test/test_float_jy.py
trunk/jython/src/org/python/core/PyFloat.java
trunk/jython/src/org/python/modules/math.java
Added Paths:
-----------
trunk/jython/Lib/test/test_math_jy.py
Modified: trunk/jython/Lib/test/test_float_jy.py
===================================================================
--- trunk/jython/Lib/test/test_float_jy.py 2008-10-28 21:47:35 UTC (rev 5524)
+++ trunk/jython/Lib/test/test_float_jy.py 2008-10-29 02:38:55 UTC (rev 5525)
@@ -66,10 +66,23 @@
self.assertNotEqual(0.1, shuge_int)
def test_nan(self):
- self.assert_(type(float('NaN')), float)
- self.assert_(type(float('nan')), float)
- self.assertEqual(long(float('NaN')), 0)
+ nan = float('nan')
+ self.assert_(type(nan), float)
+ if jython:
+ # support Java syntax
+ self.assert_(type(float('NaN')), float)
+ # CPython 2.4/2.5 allow this
+ self.assertEqual(long(nan), 0)
+
+ self.assertNotEqual(nan, float('nan'))
+ self.assertNotEqual(nan, nan)
+ self.assertEqual(cmp(nan, float('nan')), 1)
+ self.assertEqual(cmp(nan, nan), 0)
+ for i in (-1, 1, -1.0, 1.0):
+ self.assertEqual(cmp(nan, i), -1)
+ self.assertEqual(cmp(i, nan), 1)
+
def test_infinity(self):
self.assert_(type(float('Infinity')), float)
self.assert_(type(float('inf')), float)
Added: trunk/jython/Lib/test/test_math_jy.py
===================================================================
--- trunk/jython/Lib/test/test_math_jy.py (rev 0)
+++ trunk/jython/Lib/test/test_math_jy.py 2008-10-29 02:38:55 UTC (rev 5525)
@@ -0,0 +1,26 @@
+"""Misc math module tests
+
+Made for Jython.
+"""
+import math
+import unittest
+from test import test_support
+
+inf = float('inf')
+nan = float('nan')
+
+class MathTestCase(unittest.TestCase):
+
+ def test_frexp(self):
+ self.assertEqual(math.frexp(inf), (inf, 0))
+ mantissa, exponent = math.frexp(nan)
+ self.assertNotEqual(mantissa, mantissa)
+ self.assertEqual(exponent, 0)
+
+
+def test_main():
+ test_support.run_unittest(MathTestCase)
+
+
+if __name__ == '__main__':
+ test_main()
Modified: trunk/jython/src/org/python/core/PyFloat.java
===================================================================
--- trunk/jython/src/org/python/core/PyFloat.java 2008-10-28 21:47:35 UTC (rev 5524)
+++ trunk/jython/src/org/python/core/PyFloat.java 2008-10-29 02:38:55 UTC (rev 5525)
@@ -168,6 +168,22 @@
return super.__tojava__(c);
}
+ public PyObject __eq__(PyObject other) {
+ // preclude _cmp_unsafe's this == other shortcut because NaN != anything, even
+ // itself
+ if (Double.isNaN(value)) {
+ return Py.False;
+ }
+ return null;
+ }
+
+ public PyObject __ne__(PyObject other) {
+ if (Double.isNaN(value)) {
+ return Py.True;
+ }
+ return null;
+ }
+
public int __cmp__(PyObject other) {
return float___cmp__(other);
}
@@ -197,7 +213,17 @@
} else {
return -2;
}
- return i < j ? -1 : i > j ? 1 : 0;
+
+ if (i < j) {
+ return -1;
+ } else if (i > j) {
+ return 1;
+ } else if (i == j) {
+ return 0;
+ } else {
+ // at least one side is NaN
+ return Double.isNaN(i) ? (Double.isNaN(j) ? 1 : -1) : 1;
+ }
}
public Object __coerce_ex__(PyObject other) {
Modified: trunk/jython/src/org/python/modules/math.java
===================================================================
--- trunk/jython/src/org/python/modules/math.java 2008-10-28 21:47:35 UTC (rev 5524)
+++ trunk/jython/src/org/python/modules/math.java 2008-10-29 02:38:55 UTC (rev 5525)
@@ -157,26 +157,26 @@
return new PyTuple(new PyFloat(w), new PyFloat(v));
}
- public static PyTuple frexp(double v) {
- int i = 0;
- if (v != 0.0) {
- int sign = 1;
- if (v < 0) {
+ public static PyTuple frexp(double x) {
+ int exponent = 0;
+
+ if (Double.isNaN(x) || Double.isInfinite(x) || x == 0.0) {
+ exponent = 0;
+ } else {
+ short sign = 1;
+
+ if (x < 0.0) {
+ x = -x;
sign = -1;
- v = -v;
}
- // slow...
- while (v < 0.5) {
- v = v*2.0;
- i = i-1;
- }
- while (v >= 1.0) {
- v = v*0.5;
- i = i+1;
- }
- v = v*sign;
+
+ for (; x < 0.5; x *= 2.0, exponent--);
+
+ for (; x >= 1.0; x *= 0.5, exponent++);
+
+ x *= sign;
}
- return new PyTuple(new PyFloat(v), new PyInteger(i));
+ return new PyTuple(new PyFloat(x), new PyInteger(exponent));
}
public static double ldexp(double v, int w) {
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|