@Override public void write(Kryo kryo, Output output, PyFloat object) { output.writeDouble(object.getValue()); }
/** * Returns either {@link PyObject#__tojava__(java.lang.Class)} with * {@link java.lang.Number}.class as argument. If that fails, returns * {@link PyObject#__float__()}. */ public Number getAsNumber() throws TemplateModelException { try { Object value = object.__tojava__(java.lang.Number.class); if (value == null || value == Py.NoConversion) { return Double.valueOf(object.__float__().getValue()); } return (Number) value; } catch (PyException e) { throw new TemplateModelException(e); } } }
@Override public PyObject __ne__(PyObject other) { if (Double.isNaN(getValue())) { return Py.True; } return null; }
@Override public PyObject __le__(PyObject other) { // NaN >= anything is always false. if (Double.isNaN(getValue())) { return Py.False; } return null; }
@Override public PyObject __gt__(PyObject other) { // NaN > anything is always false. if (Double.isNaN(getValue())) { return Py.False; } return null; }
@Override public PyObject __ge__(PyObject other) { // NaN >= anything is always false. if (Double.isNaN(getValue())) { return Py.False; } return null; }
@Override public PyObject __eq__(PyObject other) { // preclude _cmp_unsafe's this == other shortcut because NaN != anything, even // itself if (Double.isNaN(getValue())) { return Py.False; } return null; }
@Override public Object __tojava__(Class<?> c) { if (c == Double.TYPE || c == Number.class || c == Double.class || c == Object.class || c == Serializable.class) { return new Double(getValue()); } else if (c == Float.TYPE || c == Float.class) { return new Float(getValue()); } return super.__tojava__(c); }
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___rfloordiv___doc) final PyObject float___rfloordiv__(PyObject left) { if (!canCoerce(left)) { return null; } double leftv = coerce(left); if (getValue() == 0) { throw Py.ZeroDivisionError("float division"); } return new PyFloat(Math.floor(leftv / getValue())); }
@Override public PyObject __rdivmod__(PyObject left) { if (!canCoerce(left)) { return null; } double leftv = coerce(left); if (getValue() == 0) { throw Py.ZeroDivisionError("float division"); } double z = Math.floor(leftv / getValue()); return new PyTuple(new PyFloat(z), new PyFloat(leftv - z * getValue())); }
private static double coerce(PyObject other) { if (other instanceof PyFloat) { return ((PyFloat)other).getValue(); } else if (other instanceof PyInteger) { return ((PyInteger)other).getValue(); } else if (other instanceof PyLong) { return ((PyLong)other).doubleValue(); } else { throw Py.TypeError("xxx"); } }
@Override public PyObject __rpow__(PyObject left) { if (!canCoerce(left)) { return null; } else { return _pow(coerce(left), getValue()); } }
@ExposedMethod(doc = BuiltinDocs.float___int___doc) final PyObject float___int__() { double v = getValue(); if (v < INT_LONG_BOUNDARY && v > -(INT_LONG_BOUNDARY + 1.0)) { // v will fit into an int (when rounded towards zero). return new PyInteger((int)v); } else { return __long__(); } }
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___sub___doc) final PyObject float___sub__(PyObject right) { if (!canCoerce(right)) { return null; } double rightv = coerce(right); return new PyFloat(getValue() - rightv); }
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___rsub___doc) final PyObject float___rsub__(PyObject left) { if (!canCoerce(left)) { return null; } double leftv = coerce(left); return new PyFloat(leftv - getValue()); }
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___mul___doc) final PyObject float___mul__(PyObject right) { if (!canCoerce(right)) { return null; } double rightv = coerce(right); return new PyFloat(getValue() * rightv); }
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___add___doc) final PyObject float___add__(PyObject right) { if (!canCoerce(right)) { return null; } double rightv = coerce(right); return new PyFloat(getValue() + rightv); }
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.float___truediv___doc) final PyObject float___truediv__(PyObject right) { if (!canCoerce(right)) { return null; } double rightv = coerce(right); if (rightv == 0) { throw Py.ZeroDivisionError("float division"); } return new PyFloat(getValue() / rightv); }
public void emitNum(Num node, Code code) throws Exception { if (node.getInternalN() instanceof PyInteger) { integerConstant(((PyInteger)node.getInternalN()).getValue()).get(code); } else if (node.getInternalN() instanceof PyLong) { longConstant(((PyObject)node.getInternalN()).__str__().toString()).get(code); } else if (node.getInternalN() instanceof PyFloat) { floatConstant(((PyFloat)node.getInternalN()).getValue()).get(code); } else if (node.getInternalN() instanceof PyComplex) { complexConstant(((PyComplex)node.getInternalN()).imag).get(code); } }