[Python-checkins] r46802 - sandbox/trunk/decimal-c/_decimal.c

mateusz.rukowicz python-checkins at python.org
Fri Jun 9 21:56:10 CEST 2006


Author: mateusz.rukowicz
Date: Fri Jun 9 21:56:10 2006
New Revision: 46802
Modified:
 sandbox/trunk/decimal-c/_decimal.c
Log:
Added _fix and _rescale, more bugs fixed.
Modified: sandbox/trunk/decimal-c/_decimal.c
==============================================================================
--- sandbox/trunk/decimal-c/_decimal.c	(original)
+++ sandbox/trunk/decimal-c/_decimal.c	Fri Jun 9 21:56:10 2006
@@ -777,7 +777,7 @@
 long i, last;
 decimalobject *tmp;
 assert(expdiff > 0);
- tmp = _NEW_decimalobj(prec, self->sign, self->exp - expdiff);
+ tmp = _NEW_decimalobj(prec, self->sign, self->exp + expdiff);
 if (!tmp) return NULL;
 for (i = 0; i < prec; i++)
 tmp->digits[i] = self->digits[i];
@@ -803,7 +803,7 @@
 decimalobject *tmp;
 long i, last;
 assert(expdiff > 0);
- tmp = _NEW_decimalobj(prec, self->sign, self->exp - expdiff);
+ tmp = _NEW_decimalobj(prec, self->sign, self->exp + expdiff);
 if (!tmp) return NULL;
 for (i = 0; i < prec; i++)
 tmp->digits[i] = self->digits[i];
@@ -829,7 +829,7 @@
 {
 decimalobject *tmp;
 long i;
- tmp = _NEW_decimalobj(prec, self->sign, self->exp - expdiff);
+ tmp = _NEW_decimalobj(prec, self->sign, self->exp + expdiff);
 if (!tmp) return NULL;
 for (i = 0; i < prec; i++)
 tmp->digits[i] = self->digits[i];
@@ -939,7 +939,7 @@
 return new;
 else if (expdiff < 0) {
 /* we need to extend precision */
- new2 = _NEW_decimalobj(prec, new->sign, new->exp - expdiff);
+ new2 = _NEW_decimalobj(prec, new->sign, new->exp + expdiff);
 if (!new2) {
 Py_DECREF(new);
 return NULL;
@@ -964,7 +964,7 @@
 }
 /* All lost digits are 0, so just clobber new */
 new->ob_size = prec;
- new->exp -= expdiff;
+ new->exp += expdiff;
 if (handle_Rounded(ctx, NULL) != 0) {
 Py_DECREF(new);
 return NULL;
@@ -1058,7 +1058,7 @@
 for (i = 0; i < self->ob_size; i++)
 ans->digits[i+1] = self->digits[i];
 ans->digits[0] = 0;
-		_limb_first_n_digits(self->limbs, self->ob_size, -1, ans->limbs, ans->ob_size);
+		_limb_first_n_digits(self->limbs, self->ob_size, 0, ans->limbs, ans->ob_size);
 }
 
 tmp = _decimal_round(ans, digits, ctx, rounding);
@@ -1070,7 +1070,8 @@
 /* We need one digit less, just clobber tmp. */
 for (i = 0; i < tmp->ob_size-1; i++)
 tmp->digits[i] = tmp->digits[i+1];
- tmp->ob_size--;
+		_limb_cut_one_digit(tmp->limbs, tmp->ob_size);
+		tmp->ob_size--;
 }
 tmp->exp = exp;
 
@@ -1089,6 +1090,30 @@
 return tmp;
 }
 
+static PyObject *
+decimal_rescale(decimalobject *self, PyObject *args, PyObject *kwds)
+{
+	static char *kwlist[] = {"exp", "rounding", "context", "watchexp", 0};
+	contextobject *ctx = NULL;
+	long exp;
+	int rounding = -1, watchexp = 1;
+
+	if(!PyArg_ParseTupleAndKeywords(args,kwds,"l|iOi:_rescale",kwlist,
+							&exp, &rounding, &ctx, &watchexp))
+		return NULL;
+
+	if(ctx == NULL)
+		if(!(ctx = getcontext()))
+			return NULL;
+
+	if(!PyDecimalContext_Check(ctx)){
+		PyErr_SetString(PyExc_TypeError, "context must be Context object");
+		return NULL;
+	}
+
+	return _decimal_rescale(self,exp,ctx,rounding,watchexp);
+}
+
 /* Fix the exponents and return a copy with the exponents in bounds.
 * Only call if known not to be a special value. */
 static decimalobject *
@@ -1184,7 +1209,7 @@
 Py_DECREF(ans);
 if (!rounded)
 return NULL;
- ans = _fixexponents(self, ctx);
+ ans = _fixexponents(rounded, ctx);
 Py_DECREF(rounded);
 if (!ans)
 return NULL;
@@ -1192,6 +1217,22 @@
 return ans;
 }
 
+static PyObject *
+decimal_fix(decimalobject *self, PyObject *args, PyObject *kwds)
+{
+	static char *kwlist[] = {"context",0};
+	contextobject *ctx;
+
+	if(!PyArg_ParseTupleAndKeywords(args, kwds, "O:_fix", kwlist,
+							&ctx))
+			return NULL;
+
+	if(ctx == Py_None)
+		if(!(ctx = getcontext()))
+			return NULL;
+
+	return _decimal_fix(self,ctx);
+}
 /* convert something to a Decimal. On failure, either
 returns NotImplemented or raises an exception, depending
 on the raise argument.
@@ -1845,7 +1886,7 @@
 }
 
 
-static PyObject *	/* TODO review that */
+static PyObject *	
 _do_decimal_str(decimalobject *d, contextobject *context, int engineering)
 {
 char *outbuf;
@@ -2059,6 +2100,12 @@
 }
 
 static PyMethodDef decimal_methods[] = {
+	{"_rescale",		(PyCFunction)decimal_rescale,
+	METH_VARARGS | METH_KEYWORDS,
+	PyDoc_STR("asdf")},
+	{"_fix",			(PyCFunction)decimal_fix,
+	METH_VARARGS | METH_KEYWORDS,
+	PyDoc_STR("asdf")},
 {"adjusted", (PyCFunction)decimal_adjusted,
 METH_NOARGS,
 PyDoc_STR("Return the adjusted exponent of self.")},


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /