[Jython-checkins] jython: Rationalise certain constants in PyFloat.
jeff.allen
jython-checkins at python.org
Wed Dec 31 02:41:07 CET 2014
https://hg.python.org/jython/rev/fce295e64d78
changeset: 7481:fce295e64d78
user: Jeff Allen <ja.py at farowl.co.uk>
date: Thu Dec 18 23:12:45 2014 +0000
summary:
Rationalise certain constants in PyFloat.
files:
src/org/python/core/PyFloat.java | 22 ++++++++++++-------
src/org/python/core/PyLong.java | 2 +-
2 files changed, 15 insertions(+), 9 deletions(-)
diff --git a/src/org/python/core/PyFloat.java b/src/org/python/core/PyFloat.java
--- a/src/org/python/core/PyFloat.java
+++ b/src/org/python/core/PyFloat.java
@@ -29,6 +29,12 @@
static final Spec SPEC_REPR = InternalFormat.fromText(" >r");
/** Format specification used by str(). */
static final Spec SPEC_STR = Spec.NUMERIC;
+ /** Constant float(0). */
+ static final PyFloat ZERO = new PyFloat(0.0);
+ /** Constant float(1). */
+ static final PyFloat ONE = new PyFloat(1.0);
+ /** Constant float("nan"). */
+ static final PyFloat NAN = new PyFloat(Double.NaN);
private final double value;
@@ -56,7 +62,7 @@
PyObject x = ap.getPyObject(0, null);
if (x == null) {
if (new_.for_type == subtype) {
- return new PyFloat(0.0);
+ return ZERO;
} else {
return new PyFloatDerived(subtype, 0.0);
}
@@ -90,7 +96,7 @@
@ExposedGet(name = "imag", doc = BuiltinDocs.float_imag_doc)
public PyObject getImag() {
- return Py.newFloat(0.0);
+ return ZERO;
}
@ExposedClassMethod(doc = BuiltinDocs.float_fromhex_doc)
@@ -108,7 +114,7 @@
if (value.length() == 0) {
throw Py.ValueError(message);
} else if (value.equals("nan") || value.equals("-nan") || value.equals("+nan")) {
- return new PyFloat(Double.NaN);
+ return NAN;
} else if (value.equals("inf") || value.equals("infinity") || value.equals("+inf")
|| value.equals("+infinity")) {
return new PyFloat(Double.POSITIVE_INFINITY);
@@ -760,18 +766,18 @@
*/
if (w == 0) {
// v**0 is 1, even 0**0
- return new PyFloat(1.0);
+ return ONE;
} else if (Double.isNaN(v)) {
// nan**w = nan, unless w == 0
- return new PyFloat(Double.NaN);
+ return NAN;
} else if (Double.isNaN(w)) {
// v**nan = nan, unless v == 1; 1**nan = 1
if (v == 1.0) {
- return new PyFloat(1.0);
+ return ONE;
} else {
- return new PyFloat(Double.NaN);
+ return NAN;
}
} else if (Double.isInfinite(w)) {
@@ -780,7 +786,7 @@
* Python they are all 1.
*/
if (v == 1.0 || v == -1.0) {
- return new PyFloat(1.0);
+ return ONE;
}
} else if (v == 0.0) {
diff --git a/src/org/python/core/PyLong.java b/src/org/python/core/PyLong.java
--- a/src/org/python/core/PyLong.java
+++ b/src/org/python/core/PyLong.java
@@ -545,7 +545,7 @@
if (aexp > Integer.MAX_VALUE / 8) {
throw Py.OverflowError("long/long too large for a float");
} else if (aexp < -(Integer.MAX_VALUE / 8)) {
- return new PyFloat(0.0);
+ return PyFloat.ZERO;
}
ad = ad * Math.pow(2.0, aexp * 8);
--
Repository URL: https://hg.python.org/jython
More information about the Jython-checkins
mailing list