cinap_lenrekadd hg and python4bb9e2215 years agoHistory
1748 lines · 40.3 KB · C
RawDownload
				if (ww == NULL)					goto Error;			}			else				Py_INCREF(ww); 			fracpart = modf(i, &intpart);			vv = PyLong_FromDouble(intpart);			if (vv == NULL)				goto Error; 			if (fracpart != 0.0) {				/* Shift left, and or a 1 bit into vv				 * to represent the lost fraction.				 */				PyObject *temp; 				one = PyInt_FromLong(1);				if (one == NULL)					goto Error; 				temp = PyNumber_Lshift(ww, one);				if (temp == NULL)					goto Error;				Py_DECREF(ww);				ww = temp; 				temp = PyNumber_Lshift(vv, one);				if (temp == NULL)					goto Error;				Py_DECREF(vv);				vv = temp; 				temp = PyNumber_Or(vv, one);				if (temp == NULL)					goto Error;				Py_DECREF(vv);				vv = temp;			} 			r = PyObject_RichCompareBool(vv, ww, op);			if (r < 0)				goto Error;			result = PyBool_FromLong(r); 		 Error: 		 	Py_XDECREF(vv); 		 	Py_XDECREF(ww); 		 	Py_XDECREF(one); 		 	return result;		}	} /* else if (PyLong_Check(w)) */ 	else	/* w isn't float, int, or long */		goto Unimplemented;  Compare:	PyFPE_START_PROTECT("richcompare", return NULL)	switch (op) {	case Py_EQ:		r = i == j;		break;	case Py_NE:		r = i != j;		break;	case Py_LE:		r = i <= j;		break;	case Py_GE:		r = i >= j;		break;	case Py_LT:		r = i < j;		break;	case Py_GT:		r = i > j;		break;	}	PyFPE_END_PROTECT(r)	return PyBool_FromLong(r);  Unimplemented:	Py_INCREF(Py_NotImplemented);	return Py_NotImplemented;} static longfloat_hash(PyFloatObject *v){	return _Py_HashDouble(v->ob_fval);} static PyObject *float_add(PyObject *v, PyObject *w){	double a,b;	CONVERT_TO_DOUBLE(v, a);	CONVERT_TO_DOUBLE(w, b);	PyFPE_START_PROTECT("add", return 0)	a = a + b;	PyFPE_END_PROTECT(a)	return PyFloat_FromDouble(a);} static PyObject *float_sub(PyObject *v, PyObject *w){	double a,b;	CONVERT_TO_DOUBLE(v, a);	CONVERT_TO_DOUBLE(w, b);	PyFPE_START_PROTECT("subtract", return 0)	a = a - b;	PyFPE_END_PROTECT(a)	return PyFloat_FromDouble(a);} static PyObject *float_mul(PyObject *v, PyObject *w){	double a,b;	CONVERT_TO_DOUBLE(v, a);	CONVERT_TO_DOUBLE(w, b);	PyFPE_START_PROTECT("multiply", return 0)	a = a * b;	PyFPE_END_PROTECT(a)	return PyFloat_FromDouble(a);} static PyObject *float_div(PyObject *v, PyObject *w){	double a,b;	CONVERT_TO_DOUBLE(v, a);	CONVERT_TO_DOUBLE(w, b);	if (b == 0.0) {		PyErr_SetString(PyExc_ZeroDivisionError, "float division");		return NULL;	}	PyFPE_START_PROTECT("divide", return 0)	a = a / b;	PyFPE_END_PROTECT(a)	return PyFloat_FromDouble(a);} static PyObject *float_classic_div(PyObject *v, PyObject *w){	double a,b;	CONVERT_TO_DOUBLE(v, a);	CONVERT_TO_DOUBLE(w, b);	if (Py_DivisionWarningFlag >= 2 &&	    PyErr_Warn(PyExc_DeprecationWarning, "classic float division") < 0)		return NULL;	if (b == 0.0) {		PyErr_SetString(PyExc_ZeroDivisionError, "float division");		return NULL;	}	PyFPE_START_PROTECT("divide", return 0)	a = a / b;	PyFPE_END_PROTECT(a)	return PyFloat_FromDouble(a);} static PyObject *float_rem(PyObject *v, PyObject *w){	double vx, wx;	double mod; 	CONVERT_TO_DOUBLE(v, vx); 	CONVERT_TO_DOUBLE(w, wx);	if (wx == 0.0) {		PyErr_SetString(PyExc_ZeroDivisionError, "float modulo");		return NULL;	}	PyFPE_START_PROTECT("modulo", return 0)	mod = fmod(vx, wx);	/* note: checking mod*wx < 0 is incorrect -- underflows to	   0 if wx < sqrt(smallest nonzero double) */	if (mod && ((wx < 0) != (mod < 0))) {		mod += wx;	}	PyFPE_END_PROTECT(mod)	return PyFloat_FromDouble(mod);} static PyObject *float_divmod(PyObject *v, PyObject *w){	double vx, wx;	double div, mod, floordiv; 	CONVERT_TO_DOUBLE(v, vx); 	CONVERT_TO_DOUBLE(w, wx);	if (wx == 0.0) {		PyErr_SetString(PyExc_ZeroDivisionError, "float divmod()");		return NULL;	}	PyFPE_START_PROTECT("divmod", return 0)	mod = fmod(vx, wx);	/* fmod is typically exact, so vx-mod is *mathematically* an	   exact multiple of wx.  But this is fp arithmetic, and fp	   vx - mod is an approximation; the result is that div may	   not be an exact integral value after the division, although	   it will always be very close to one.	*/	div = (vx - mod) / wx;	if (mod) {		/* ensure the remainder has the same sign as the denominator */		if ((wx < 0) != (mod < 0)) {			mod += wx;			div -= 1.0;		}	}	else {		/* the remainder is zero, and in the presence of signed zeroes		   fmod returns different results across platforms; ensure		   it has the same sign as the denominator; we'd like to do		   "mod = wx * 0.0", but that may get optimized away */		mod *= mod;  /* hide "mod = +0" from optimizer */		if (wx < 0.0)			mod = -mod;	}	/* snap quotient to nearest integral value */	if (div) {		floordiv = floor(div);		if (div - floordiv > 0.5)			floordiv += 1.0;	}	else {		/* div is zero - get the same sign as the true quotient */		div *= div;	/* hide "div = +0" from optimizers */		floordiv = div * vx / wx; /* zero w/ sign of vx/wx */	}	PyFPE_END_PROTECT(floordiv)	return Py_BuildValue("(dd)", floordiv, mod);} static PyObject *float_floor_div(PyObject *v, PyObject *w){	PyObject *t, *r; 	t = float_divmod(v, w);	if (t == NULL || t == Py_NotImplemented)		return t;	assert(PyTuple_CheckExact(t));	r = PyTuple_GET_ITEM(t, 0);	Py_INCREF(r);	Py_DECREF(t);	return r;} static PyObject *float_pow(PyObject *v, PyObject *w, PyObject *z){	double iv, iw, ix; 	if ((PyObject *)z != Py_None) {		PyErr_SetString(PyExc_TypeError, "pow() 3rd argument not "			"allowed unless all arguments are integers");		return NULL;	} 	CONVERT_TO_DOUBLE(v, iv);	CONVERT_TO_DOUBLE(w, iw); 	/* Sort out special cases here instead of relying on pow() */	if (iw == 0) { 		/* v**0 is 1, even 0**0 */		PyFPE_START_PROTECT("pow", return NULL)		if ((PyObject *)z != Py_None) {			double iz;			CONVERT_TO_DOUBLE(z, iz);			ix = fmod(1.0, iz);			if (ix != 0 && iz < 0)				ix += iz;		}		else			ix = 1.0;		PyFPE_END_PROTECT(ix)		return PyFloat_FromDouble(ix);	}	if (iv == 0.0) {  /* 0**w is error if w<0, else 1 */		if (iw < 0.0) {			PyErr_SetString(PyExc_ZeroDivisionError,					"0.0 cannot be raised to a negative power");			return NULL;		}		return PyFloat_FromDouble(0.0);	}	if (iv < 0.0) {		/* Whether this is an error is a mess, and bumps into libm		 * bugs so we have to figure it out ourselves.		 */		if (iw != floor(iw)) {			PyErr_SetString(PyExc_ValueError, "negative number "				"cannot be raised to a fractional power");			return NULL;		}		/* iw is an exact integer, albeit perhaps a very large one.		 * -1 raised to an exact integer should never be exceptional.		 * Alas, some libms (chiefly glibc as of early 2003) return		 * NaN and set EDOM on pow(-1, large_int) if the int doesn't		 * happen to be representable in a *C* integer.  That's a		 * bug; we let that slide in math.pow() (which currently		 * reflects all platform accidents), but not for Python's **.		 */		 if (iv == -1.0 && Py_IS_FINITE(iw)) {		 	/* Return 1 if iw is even, -1 if iw is odd; there's		 	 * no guarantee that any C integral type is big		 	 * enough to hold iw, so we have to check this		 	 * indirectly.		 	 */		 	ix = floor(iw * 0.5) * 2.0;			return PyFloat_FromDouble(ix == iw ? 1.0 : -1.0);		}		/* Else iv != -1.0, and overflow or underflow are possible.		 * Unless we're to write pow() ourselves, we have to trust		 * the platform to do this correctly.		 */	}	errno = 0;	PyFPE_START_PROTECT("pow", return NULL)	ix = pow(iv, iw);	PyFPE_END_PROTECT(ix)	Py_ADJUST_ERANGE1(ix);	if (errno != 0) {		/* We don't expect any errno value other than ERANGE, but		 * the range of libm bugs appears unbounded.		 */		PyErr_SetFromErrno(errno == ERANGE ? PyExc_OverflowError :						     PyExc_ValueError);		return NULL;	}	return PyFloat_FromDouble(ix);} static PyObject *float_neg(PyFloatObject *v){	return PyFloat_FromDouble(-v->ob_fval);} static PyObject *float_pos(PyFloatObject *v){	if (PyFloat_CheckExact(v)) {		Py_INCREF(v);		return (PyObject *)v;	}	else		return PyFloat_FromDouble(v->ob_fval);} static PyObject *float_abs(PyFloatObject *v){	return PyFloat_FromDouble(fabs(v->ob_fval));} static intfloat_nonzero(PyFloatObject *v){	return v->ob_fval != 0.0;} static intfloat_coerce(PyObject **pv, PyObject **pw){	if (PyInt_Check(*pw)) {		long x = PyInt_AsLong(*pw);		*pw = PyFloat_FromDouble((double)x);		Py_INCREF(*pv);		return 0;	}	else if (PyLong_Check(*pw)) {		double x = PyLong_AsDouble(*pw);		if (x == -1.0 && PyErr_Occurred())			return -1;		*pw = PyFloat_FromDouble(x);		Py_INCREF(*pv);		return 0;	}	else if (PyFloat_Check(*pw)) {		Py_INCREF(*pv);		Py_INCREF(*pw);		return 0;	}	return 1; /* Can't do it */} static PyObject *float_long(PyObject *v){	double x = PyFloat_AsDouble(v);	return PyLong_FromDouble(x);} static PyObject *float_int(PyObject *v){	double x = PyFloat_AsDouble(v);	double wholepart;	/* integral portion of x, rounded toward 0 */ 	(void)modf(x, &wholepart);	/* Try to get out cheap if this fits in a Python int.  The attempt	 * to cast to long must be protected, as C doesn't define what	 * happens if the double is too big to fit in a long.  Some rare	 * systems raise an exception then (RISCOS was mentioned as one,	 * and someone using a non-default option on Sun also bumped into	 * that).  Note that checking for >= and <= LONG_{MIN,MAX} would	 * still be vulnerable:  if a long has more bits of precision than	 * a double, casting MIN/MAX to double may yield an approximation,	 * and if that's rounded up, then, e.g., wholepart=LONG_MAX+1 would	 * yield true from the C expression wholepart<=LONG_MAX, despite	 * that wholepart is actually greater than LONG_MAX.	 */	if (LONG_MIN < wholepart && wholepart < LONG_MAX) {		const long aslong = (long)wholepart;		return PyInt_FromLong(aslong);	}	return PyLong_FromDouble(wholepart);} static PyObject *float_float(PyObject *v){	if (PyFloat_CheckExact(v))		Py_INCREF(v);	else		v = PyFloat_FromDouble(((PyFloatObject *)v)->ob_fval);	return v;}  static PyObject *float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds); static PyObject *float_new(PyTypeObject *type, PyObject *args, PyObject *kwds){	PyObject *x = Py_False; /* Integer zero */	static char *kwlist[] = {"x", 0}; 	if (type != &PyFloat_Type)		return float_subtype_new(type, args, kwds); /* Wimp out */	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))		return NULL;	if (PyString_Check(x))		return PyFloat_FromString(x, NULL);	return PyNumber_Float(x);} /* Wimpy, slow approach to tp_new calls for subtypes of float:   first create a regular float from whatever arguments we got,   then allocate a subtype instance and initialize its ob_fval   from the regular float.  The regular float is then thrown away.*/static PyObject *float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds){	PyObject *tmp, *newobj; 	assert(PyType_IsSubtype(type, &PyFloat_Type));	tmp = float_new(&PyFloat_Type, args, kwds);	if (tmp == NULL)		return NULL;	assert(PyFloat_CheckExact(tmp));	newobj = type->tp_alloc(type, 0);	if (newobj == NULL) {		Py_DECREF(tmp);		return NULL;	}	((PyFloatObject *)newobj)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;	Py_DECREF(tmp);	return newobj;} static PyObject *float_getnewargs(PyFloatObject *v){	return Py_BuildValue("(d)", v->ob_fval);} /* this is for the benefit of the pack/unpack routines below */ typedef enum {	unknown_format, ieee_big_endian_format, ieee_little_endian_format} float_format_type; static float_format_type double_format, float_format;static float_format_type detected_double_format, detected_float_format; static PyObject *float_getformat(PyTypeObject *v, PyObject* arg){	char* s;	float_format_type r; 	if (!PyString_Check(arg)) {		PyErr_Format(PyExc_TypeError,	     "__getformat__() argument must be string, not %.500s",			     arg->ob_type->tp_name);

Lines 501–1000 of 1748. Previous lines