We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent bb0195f commit 2113bacCopy full SHA for 2113bac
4 files changed
Lib/test/test_float_jy.py
@@ -66,9 +66,22 @@ def test_overflow(self):
66
self.assertNotEqual(0.1, shuge_int)
67
68
def test_nan(self):
69
- self.assert_(type(float('NaN')), float)
70
- self.assert_(type(float('nan')), float)
71
- self.assertEqual(long(float('NaN')), 0)
+ nan = float('nan')
+ self.assert_(type(nan), float)
+ if jython:
72
+ # support Java syntax
73
+ self.assert_(type(float('NaN')), float)
74
+
75
+ # CPython 2.4/2.5 allow this
76
+ self.assertEqual(long(nan), 0)
77
78
+ self.assertNotEqual(nan, float('nan'))
79
+ self.assertNotEqual(nan, nan)
80
+ self.assertEqual(cmp(nan, float('nan')), 1)
81
+ self.assertEqual(cmp(nan, nan), 0)
82
+ for i in (-1, 1, -1.0, 1.0):
83
+ self.assertEqual(cmp(nan, i), -1)
84
+ self.assertEqual(cmp(i, nan), 1)
85
86
def test_infinity(self):
87
self.assert_(type(float('Infinity')), float)
Lib/test/test_math_jy.py
@@ -0,0 +1,26 @@
1
+"""Misc math module tests
2
3
+Made for Jython.
4
+"""
5
+import math
6
+import unittest
7
+from test import test_support
8
9
+inf = float('inf')
10
+nan = float('nan')
11
12
+class MathTestCase(unittest.TestCase):
13
14
+ def test_frexp(self):
15
+ self.assertEqual(math.frexp(inf), (inf, 0))
16
+ mantissa, exponent = math.frexp(nan)
17
+ self.assertNotEqual(mantissa, mantissa)
18
+ self.assertEqual(exponent, 0)
19
20
21
+def test_main():
22
+ test_support.run_unittest(MathTestCase)
23
24
25
+if __name__ == '__main__':
26
+ test_main()
src/org/python/core/PyFloat.java
@@ -168,6 +168,22 @@ public Object __tojava__(Class c) {
168
return super.__tojava__(c);
169
}
170
171
+ public PyObject __eq__(PyObject other) {
172
+ // preclude _cmp_unsafe's this == other shortcut because NaN != anything, even
173
+ // itself
174
+ if (Double.isNaN(value)) {
175
+ return Py.False;
176
+ }
177
+ return null;
178
179
180
+ public PyObject __ne__(PyObject other) {
181
182
+ return Py.True;
183
184
185
186
187
public int __cmp__(PyObject other) {
188
return float___cmp__(other);
189
@@ -197,7 +213,17 @@ final int float___cmp__(PyObject other) {
197
213
} else {
198
214
return -2;
199
215
200
- return i < j ? -1 : i > j ? 1 : 0;
216
217
+ if (i < j) {
218
+ return -1;
219
+ } else if (i > j) {
220
+ return 1;
221
+ } else if (i == j) {
222
+ return 0;
223
+ } else {
224
+ // at least one side is NaN
225
+ return Double.isNaN(i) ? (Double.isNaN(j) ? 1 : -1) : 1;
226
201
227
202
228
203
229
public Object __coerce_ex__(PyObject other) {
src/org/python/modules/math.java
@@ -157,26 +157,26 @@ public static PyTuple modf(double v) {
157
return new PyTuple(new PyFloat(w), new PyFloat(v));
158
159
160
- public static PyTuple frexp(double v) {
161
- int i = 0;
162
- if (v != 0.0) {
163
- int sign = 1;
164
- if (v < 0) {
+ public static PyTuple frexp(double x) {
+ int exponent = 0;
+ if (Double.isNaN(x) || Double.isInfinite(x) || x == 0.0) {
+ exponent = 0;
165
166
+ short sign = 1;
167
+ 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) {
0 commit comments