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

mateusz.rukowicz python-checkins at python.org
Thu Aug 10 20:29:22 CEST 2006


Author: mateusz.rukowicz
Date: Thu Aug 10 20:29:21 2006
New Revision: 51193

Modified:
   sandbox/trunk/decimal-c/_decimal.c
Log:
Reviewed and fixed some warnings.


Modified: sandbox/trunk/decimal-c/_decimal.c
==============================================================================
--- sandbox/trunk/decimal-c/_decimal.c	(original)
+++ sandbox/trunk/decimal-c/_decimal.c	Thu Aug 10 20:29:21 2006
@@ -503,7 +503,6 @@
             long tmp_limbs;
             long diff;
             long mid;
-            int bla;
             diff = up - down;
             mid = down + diff/2;
 
@@ -574,7 +573,7 @@
     
     return new_pos;
 }
-
+/*
 static long
 _limb_normalize(long *first, long size) {
     long i;
@@ -591,6 +590,7 @@
 
     return new_size;
 }
+*/
 #ifdef BIG_EXP
 
 /* TODO I have to *limit* passing arguments by value,
@@ -685,11 +685,11 @@
         written ++;
     }
 
-    tmp = sprintf(buf, "%i", exp.limbs[exp.size-1]);
+    tmp = sprintf(buf, "%ld", exp.limbs[exp.size-1]);
     buf += tmp;
     written += tmp;
     for (i = exp.size - 2; i>=0; i--) {
-        tmp = sprintf(buf, "%."LOG_STR"i", exp.limbs[i]);
+        tmp = sprintf(buf, "%."LOG_STR"ld", exp.limbs[i]);
         buf += tmp;
         written += tmp;
     }
@@ -831,12 +831,12 @@
 exp_sub_i(exp_t a, long b) {
     return exp_sub(a, exp_from_i(b));
 }
-
+/*
 static exp_t
 exp_inp_add_i(exp_t *a, long b) {
     return exp_inp_add(a, exp_from_i(b));
 }
-
+*/
 static exp_t
 exp_inp_sub_i(exp_t *a, long b) {
     return exp_inp_sub(a, exp_from_i(b));
@@ -894,16 +894,6 @@
     return ret;
 }
 
-static exp_t
-exp_floordiv_i(exp_t a, long b) {
-    long remainder;
-    exp_t ret;
-    ret = exp_div_i(a, b, &remainder);
-    if (remainder && exp_is_neg(a))
-        exp_dec(&ret);
-
-    return ret;
-}
 
 static int
 exp_mod_i(exp_t a, long b) {
@@ -928,12 +918,12 @@
     else
         return cmp;
 }
-
+/*
 static int
 exp_cmp_i(exp_t a, long b) {
     return exp_cmp(a, exp_from_i(b));
 }
-
+*/
 static int
 exp_g(exp_t a, exp_t b) {
     int cmp = exp_cmp(a, b);
@@ -972,12 +962,12 @@
     int cmp = exp_cmp(a, b);
     return cmp != 0;
 }
-
+/*
 static int
 exp_ne_i(exp_t a, long b) {
     return exp_ne(a, exp_from_i(b));
 }
-
+*/
 static int
 exp_ge(exp_t a, exp_t b) {
     int cmp = exp_cmp(a, b);
@@ -1041,6 +1031,17 @@
         return b;
 }
 
+static exp_t
+exp_floordiv_i(exp_t a, long b) {
+    long remainder;
+    exp_t ret;
+    ret = exp_div_i(a, b, &remainder);
+    if (remainder && exp_is_neg(a))
+        exp_dec(&ret);
+    return ret;
+}
+
+
 #else
 /* TODO a little mess here ;P */
 #define exp_add_i(exp, a)   ((exp) + (a))   
@@ -1071,8 +1072,8 @@
 #define exp_floordiv_i(a,b) ((a) / (b) - ((a) % (b) && (a) < 0))
 #define exp_inp_sub(a, b)   ((*(a)) -= (b)) 
 #define exp_inp_sub_i(a, b) ((*(a)) -= (b)) 
-#define exp_sprintf(a, e)   (sprintf(a, "%d", e))   
-#define exp_sscanf(a, e)    (sscanf(a, "%d", e))    
+#define exp_sprintf(a, e)   (sprintf(a, "%ld", e))   
+#define exp_sscanf(a, e)    (sscanf(a, "%ld", e))    
 #define exp_min(a, b)       ((a) < (b) ? (a) : (b)) 
 #define exp_max(a, b)       ((a) > (b) ? (a) : (b)) 
 #define exp_neg(a)          (-(a))          
@@ -1260,6 +1261,7 @@
 static decimalobject *_do_decimal_subtract(decimalobject *, decimalobject *, contextobject *);
 static PyObject *context_ignore_flags(contextobject *self, PyObject *args);
 static decimalobject *_do_decimal_power(decimalobject *, decimalobject *, decimalobject *, contextobject *);
+static int _decimal_isint(decimalobject*);
 
 /* Exception handlers *********************************************************/
 
@@ -1336,7 +1338,7 @@
     return res;
 }
 
-static decimalobject *
+static PyObject *
 handle_DivisionImpossible(PyTypeObject *type, contextobject *ctx, char *expl)
 {
     HANDLE_ERROR(ctx, C_DIV_IMPOSSIBLE, expl, NULL);
@@ -1346,7 +1348,7 @@
     return Py_BuildValue("(OO)", PyDecimal_NaN, PyDecimal_NaN);
 }
 
-static decimalobject *
+static PyObject *
 handle_DivisionUndefined(PyTypeObject *type, contextobject *ctx,
                          char *expl, int two)
 {
@@ -1354,7 +1356,7 @@
 
     if (!two) {
         Py_INCREF(PyDecimal_NaN);
-        return PyDecimal_NaN;
+        return (PyObject*)PyDecimal_NaN;
     }
     
     else {
@@ -1398,7 +1400,6 @@
 handle_Overflow(PyTypeObject *type, contextobject *ctx, char *expl, long lsign)
 {
     decimalobject *res = 0;
-    long i;
 
     HANDLE_ERROR(ctx, S_OVERFLOW, expl, NULL);
 
@@ -1589,7 +1590,6 @@
 static decimalobject *
 _round_down(decimalobject *self, long prec, exp_t expdiff, contextobject *ctx)
 {
-    long i;
     decimalobject *new = _NEW_decimalobj(prec, self->sign, exp_add(self->exp, expdiff));
     if (!new) return NULL;
 
@@ -1699,7 +1699,6 @@
 _round_half_up(decimalobject *self, long prec, exp_t expdiff, contextobject *ctx)
 {
     decimalobject *tmp;
-    long i;
     tmp = _NEW_decimalobj(prec, self->sign, exp_add(self->exp, expdiff));
     if (!tmp) return NULL;
     _limb_first_n_digits(self->limbs, self->ob_size, 0, tmp->limbs, prec);
@@ -2359,13 +2358,14 @@
     contextobject *ctx;
     int res;
     ctx = getcontext();
-    if (!ctx) return NULL;
+    if (!ctx) return 0;
 
     other = (decimalobject *)_convert_to_decimal(self->ob_type,
-                                                other, ctx, 0);
+                                                (PyObject*)other, ctx, 0);
 
-    if (!other) return NULL;
-    if (other == Py_NotImplemented) return other;
+    if (!other) return 0;
+    /* XXX ??*/
+    if ((PyObject*) other == Py_NotImplemented) return 0;
 
     res = _do_real_decimal_compare(self, other, ctx);
     Py_DECREF(other);
@@ -2732,8 +2732,8 @@
             goto err;
         }
 
-        side = PySequence_GetItem(divmod, 0);
-        r = PySequence_GetItem(divmod, 1);
+        side = (decimalobject*) PySequence_GetItem(divmod, 0);
+        r = (decimalobject*) PySequence_GetItem(divmod, 1);
 
         Py_DECREF(divmod);
     }
@@ -2768,7 +2768,7 @@
         two->limbs[0] = 2;
         two->sign = other->sign&1;
 
-        comparison = _do_decimal__divide(other, two, 0, ctx2);
+        comparison = (decimalobject*) _do_decimal__divide(other, two, 0, ctx2);
         Py_DECREF(two);
 
         if (!comparison)
@@ -2831,8 +2831,8 @@
         Py_DECREF(side);
         Py_DECREF(r);
         
-        side = PySequence_GetItem(divmod, 0);
-        r = PySequence_GetItem(divmod, 1);
+        side = (decimalobject *)PySequence_GetItem(divmod, 0);
+        r = (decimalobject *)PySequence_GetItem(divmod, 1);
         Py_DECREF(divmod);
     }
 
@@ -2860,7 +2860,7 @@
         if (PyErr_Occurred())
             goto err;
 
-        if (cmp == 1 || decrease && cmp == 0) {
+        if ((cmp == 1) || (decrease && cmp == 0)) {
             r->sign = s1;
             comparison->sign = s2;
             ctx2->prec += 1;
@@ -2899,7 +2899,7 @@
                     Py_DECREF(r);
                     Py_DECREF(comparison);
 
-                    return ret;
+                    return (decimalobject *)ret;
                 }
                 Py_DECREF(tmp);
                 
@@ -3154,7 +3154,7 @@
     while (1) {
         ctx2->prec = 2 * ctx2->prec - 2 < maxp ? 2 * ctx2->prec - 2: maxp;
         /* ans = half * (ans + tmp/ans) */
-        tmp2 = _do_decimal__divide(tmp, ans, 0, ctx2);
+        tmp2 = (decimalobject*) _do_decimal__divide(tmp, ans, 0, ctx2);
         if (!tmp2)
             goto err;
         /* ans = half * (ans + tmp2) */
@@ -3539,7 +3539,7 @@
         {
             for(i=0;i< d->ob_size;i++)
             {
-                p+= sprintf(p, "%d", _limb_get_digit(d->limbs, d->ob_size, i));    /* SLOW */
+                p+= sprintf(p, "%ld", _limb_get_digit(d->limbs, d->ob_size, i));    /* SLOW */
                 SANITY_CHECK(p);
             }
         }
@@ -3707,7 +3707,7 @@
     {
         if(exp_eq_i(dotplace, i))
             *p++ = '.';
-        p += sprintf(p, "%d", _limb_get_digit(d->limbs,d->ob_size,i));
+        p += sprintf(p, "%ld", _limb_get_digit(d->limbs,d->ob_size,i));
     }
 
     while(extra_zeros --)*p++ = '0';
@@ -3918,9 +3918,9 @@
 
     if (self_is_zero && other_is_zero) {
        if (divmod) {
-           return handle_DivisionUndefined(self->ob_type, ctx, "0 / 0", 1);
+           return (PyObject*)handle_DivisionUndefined(self->ob_type, ctx, "0 / 0", 1);
        } 
-       return handle_DivisionUndefined(self->ob_type, ctx, "0 / 0", 0);
+       return (PyObject*)handle_DivisionUndefined(self->ob_type, ctx, "0 / 0", 0);
     }
 
     if (self_is_zero) {
@@ -3974,9 +3974,9 @@
 
     if (other_is_zero) {
         if (divmod) {
-            return handle_DivisionByZero(self->ob_type, ctx, "divmod(x,0)", sign, 1);
+            return (PyObject*)handle_DivisionByZero(self->ob_type, ctx, "divmod(x,0)", sign, 1);
         }
-        return handle_DivisionByZero(self->ob_type, ctx, "x / 0", sign, 0);
+        return (PyObject*)handle_DivisionByZero(self->ob_type, ctx, "x / 0", sign, 0);
     }
 
     /* now fun starts, answer isnt one of 0 NaN or INF */
@@ -4083,7 +4083,7 @@
         exp_t expdiff;
         long rlimbs;
         long old_size;
-        exp_t adj1, adj2, adjusted = exp_from_i(0);
+        exp_t adjusted = exp_from_i(0);
         long i;
         long max_size;
         exp_t min_expdiff;   /* used when divmod */
@@ -4313,7 +4313,9 @@
     static char *kwlist[] = {"other", "divmod", "context", 0};
     contextobject *ctx = NULL;
     int divmod = 0;
-    PyObject *other;
+    PyObject *other, *ret;
+    /* what should we DECREF */
+    decimalobject *dec = 0;
 
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|iO:_divide", kwlist,
                 &other, &divmod, &ctx))
@@ -4323,15 +4325,32 @@
         if (!(ctx = getcontext()))
             return NULL;
 
+    if (PyDecimal_Check(self)) {
+        other = _convert_to_decimal(self->ob_type, other, ctx, 0);
+        if (!other || other == Py_NotImplemented)
+            return other;
+        dec = (decimalobject*)other;
+    }
+
+    else {
+        assert(PyDecimal_Check(other));
+        self = (decimalobject*)_convert_to_decimal(other->ob_type, (PyObject*)self, ctx, 0);
+        if (!self || (PyObject*)self == Py_NotImplemented)
+            return (PyObject*)self;
+        dec = (decimalobject*)self;
+    }
     if (!PyDecimalContext_Check(ctx)) {
         PyErr_SetString(PyExc_TypeError, "context must be Context object");
+        Py_DECREF(dec);
         return NULL;
     }
 
-    return (PyObject*) _do_decimal__divide(self, other, divmod, ctx);
+    ret = _do_decimal__divide(self, (decimalobject*)other, divmod, ctx);
+    Py_DECREF(dec);
+    return ret;
 }
 
-static int *
+static int
 check_ctx(decimalobject *self, contextobject *ctx) {
     
     if (ctx->prec > MAX_MATH ||
@@ -4347,7 +4366,7 @@
  * But first, we divide x, by integer, so that x < 1 - this results
  * in much faster calculation. So we have e^(x/a), where a = 10^h,
  * when we got e^(x/a) we raise it to power a. H is at most 8. */
-static PyObject *
+static decimalobject *
 _do_decimal_exponent(decimalobject *self, contextobject *ctx) {
     decimalobject *ans;
     long h;
@@ -4377,7 +4396,7 @@
                     return NULL;
                 
                 ans->limbs[0] = 0;
-                return ans;
+                return  ans;
             }
             else {
                 ans = _decimal_get_copy(self);
@@ -4392,7 +4411,7 @@
             return NULL;
 
         ans->limbs[0] = 1;
-        return ans;
+        return  ans;
     }
 
     h = exp_to_i(ADJUSTED(self)) + 1;
@@ -4512,7 +4531,7 @@
 
             t = tmp;
             
-            tmp = _do_decimal__divide(t, d, 0, ctx2);
+            tmp = (decimalobject*)_do_decimal__divide(t, d, 0, ctx2);
             Py_DECREF(t);
 
             if (!tmp) {
@@ -4570,7 +4589,7 @@
         for (i = 0; i < h; i++)
             pow *= 10;
         
-        y = decimal_from_long(self->ob_type, pow);
+        y = (decimalobject*)decimal_from_long(self->ob_type, pow);
 
         if (!y) {
             Py_DECREF(ans);
@@ -4580,7 +4599,7 @@
         }
 
         Py_INCREF(Py_None);
-        tmp = _do_decimal_power(ans, y, Py_None, ctx);
+        tmp = _do_decimal_power(ans, y, (decimalobject*)Py_None, ctx);
         Py_DECREF(Py_None);
         Py_DECREF(y);
         Py_DECREF(ans);
@@ -4619,7 +4638,7 @@
         Py_DECREF(ans);
         ans = fixed;
     }
-    return ans;
+    return  ans;
 }
 
 DECIMAL_UNARY_FUNC(exponent);
@@ -4632,8 +4651,8 @@
         return NULL;
 
     if (!check_ctx(self, ctx))
-        return handle_InvalidContext(self->ob_type, ctx, NULL);
-    return _do_decimal_exponent(self, ctx);
+        return (PyObject*)handle_InvalidContext(self->ob_type, ctx, NULL);
+    return (PyObject*)_do_decimal_exponent(self, ctx);
 }
 
 DECIMAL_UNARY_FUNC(exp);
@@ -4649,15 +4668,13 @@
     5341,  4889,  4437, 39930, 35534, 31186, 26886, 22630, 18418, 14254,
     10130,  6046, 20055};
 
-static PyObject *
+static decimalobject *
 _do_decimal__ln(decimalobject *self, contextobject *ctx) {
     
     decimalobject *ans = 0, *b = 0, *tmp, *one = 0;
     contextobject *ctx1 = 0, *ctx2 = 0;
     PyObject *flags;
-    long precision;
     int rounding;
-    int clamped;
     long prec, cp;
     int t;
     if (!ctx)
@@ -4710,10 +4727,10 @@
     prec = (self->ob_size > ctx->prec ? self->ob_size : ctx->prec) + 2;
     prec = prec > 9 ? prec : 9;
     
-    ans = decimal_from_long(self->ob_type, exp_to_i(ADJUSTED(self)) + 1);
+    ans = (decimalobject*)decimal_from_long(self->ob_type, exp_to_i(ADJUSTED(self)) + 1);
     if (!ans)
         goto err;
-    b = decimal_from_long(self->ob_type, 2302585);
+    b = (decimalobject*)decimal_from_long(self->ob_type, 2302585);
     if (!b)
         goto err;
     b->exp = exp_from_i(-6);
@@ -4736,7 +4753,7 @@
     t = ln_lookup[t-10];
 
     Py_DECREF(b);
-    b = decimal_from_long(self->ob_type, t >> 2);
+    b = (decimalobject*)decimal_from_long(self->ob_type, t >> 2);
     if (!b)
         goto err;
     b->exp = exp_from_i(-(t&3) - 3);
@@ -4875,13 +4892,13 @@
 static PyObject *
 _do_decimal_ln(decimalobject *self, contextobject *ctx) {
     if (!check_ctx(self, ctx))
-        return handle_InvalidContext(self->ob_type, ctx, NULL);
-    return _do_decimal__ln(self, ctx);
+        return (PyObject*)handle_InvalidContext(self->ob_type, ctx, NULL);
+    return (PyObject*)_do_decimal__ln(self, ctx);
 }
 
 DECIMAL_UNARY_FUNC(ln);
 
-static PyObject *
+static decimalobject *
 _do_decimal_log10(decimalobject *self, contextobject *ctx) {
     decimalobject *ret;
     long prec;
@@ -4925,7 +4942,7 @@
         if (i == self->ob_size) {
             decimalobject *fixed;
             /* TODO what about big exp? */
-            ret = decimal_from_long(self->ob_type, exp_to_i(ADJUSTED(self)));
+            ret = (decimalobject*)decimal_from_long(self->ob_type, exp_to_i(ADJUSTED(self)));
             if (!ret)
                 return NULL;
 
@@ -4997,7 +5014,7 @@
         Py_DECREF(ctx2);
 
         /* log10 = ln(self)/ln(10) */
-        ret = _do_decimal__divide(a, ln10, 0, ctx);
+        ret = (decimalobject*)_do_decimal__divide(a, ln10, 0, ctx);
         Py_DECREF(a);
         Py_DECREF(ln10);
 
@@ -5076,7 +5093,6 @@
     if (exp_eq(adj_self, adj_other)) {
         long digits = self->ob_size > other->ob_size ? self->ob_size : other->ob_size;
         long i;
-        long ret;
         /* SLOW */
 
         for (i=0;i<digits;i++) {
@@ -5119,6 +5135,9 @@
         case -1: return when_l;
                  break;
     }
+    /* we should not reach here */
+    assert(0);
+    return -1;
 }
 
 static decimalobject *
@@ -5127,7 +5146,7 @@
     int res;
 
     res = _do_real_decimal_comparetotal(self, other, ctx);
-    return decimal_from_long(self->ob_type, res);
+    return (decimalobject*)decimal_from_long(self->ob_type, res);
 }
 
 DECIMAL_BINARY_FUNC(comparetotal);
@@ -5591,7 +5610,6 @@
 
     if(!decimal_nonzero(self) || !decimal_nonzero(other))
     {
-        decimalobject *ret;
         ans = _NEW_decimalobj(1, resultsign, resultexp);
         if(!ans)
             return NULL;
@@ -5650,7 +5668,7 @@
 _do_decimal_divide(decimalobject *self, decimalobject *other,
                    contextobject *ctx)
 {
-    return _do_decimal__divide(self, other, 0, ctx);
+    return (decimalobject*)_do_decimal__divide(self, other, 0, ctx);
 }
 DECIMAL_SPECIAL_2FUNC(decimal_divide)
 
@@ -5665,11 +5683,11 @@
         return NULL;
     
     if (PySequence_Check(seq)) {
-        ret = PySequence_GetItem(seq, 0);
+        ret = (decimalobject*)PySequence_GetItem(seq, 0);
         Py_DECREF(seq);
     }
     else
-        ret = seq;
+        ret = (decimalobject*)seq;
     return ret;
 }
 DECIMAL_SPECIAL_2FUNC(decimal_floor_div)
@@ -5705,17 +5723,17 @@
         return NULL;
 
     if (PySequence_Check(seq)) {
-        ret = PySequence_GetItem(seq, 1);
+        ret = (decimalobject*)PySequence_GetItem(seq, 1);
         Py_DECREF(seq);
     }
     else
-        ret = seq;
+        ret = (decimalobject*)seq;
     return ret;
 }
 DECIMAL_SPECIAL_2FUNC(decimal_remainder)
 
 
-static decimalobject *
+static PyObject *
 _do_decimal_divmod(decimalobject *self, decimalobject *other,
                    contextobject *ctx)
 {
@@ -5732,7 +5750,7 @@
     long n;
     int sign;
     long spot;
-    int mod = modulo != Py_None;
+    int mod = modulo != (decimalobject *)Py_None;
     long firstprec = ctx->prec;
     int cmp;
     int use_exp = 0;        /* when we should use exp/ln method */
@@ -5742,10 +5760,6 @@
     if (!ctx)
         return NULL;
     
-//    if ((ISINF(other) || exp_g_i(ADJUSTED(other), 8)) && decimal_nonzero(other) ) {
-//        return handle_InvalidOperation(self->ob_type, ctx, "x ** INF", NULL);
-//    }
-
     if ((exp_g_i(ADJUSTED(other), 8)) && decimal_nonzero(other)) {
         use_exp = 1;
     }
@@ -6082,7 +6096,7 @@
     if (n < 0) {
         decimalobject *tmp;
         n *= -1;
-        tmp = _do_decimal__divide(val, mul, 0, ctx);
+        tmp = (decimalobject*)_do_decimal__divide(val, mul, 0, ctx);
         if (!tmp) {
             Py_DECREF(mul);
             Py_DECREF(val);
@@ -6170,7 +6184,7 @@
         other = _convert_to_decimal(self->ob_type, other, ctx, 0);
         if (!other || other == Py_NotImplemented) 
             return other;
-        dec = other;
+        dec = (decimalobject*)other;
         
     }
 
@@ -6179,7 +6193,7 @@
         self = _convert_to_decimal(other->ob_type, self, ctx, 0);
         if (!self || self == Py_NotImplemented)
             return self;
-        dec = self;
+        dec = (decimalobject*)self;
     }
 
     
@@ -7196,7 +7210,7 @@
     0,                           /* tp_print */
     0,                           /* tp_getattr */
     0,                           /* tp_setattr */
-    decimal_cmp,                           /* tp_compare */
+    (cmpfunc)decimal_cmp,                           /* tp_compare */
     (reprfunc)decimal_repr,      /* tp_repr */
     &decimal_as_number,          /* tp_as_number */
     0,                           /* tp_as_sequence */
@@ -7598,14 +7612,14 @@
         dec_c = (decimalobject *)_convert_to_decimal(
             &PyDecimal_DecimalType, c, self, 1);
         if (dec_c == NULL) {
-            dec_c = Py_None;
+            dec_c = (decimalobject *)Py_None;
             Py_INCREF(Py_None);
             /* XXX is it ok ? */
             PyErr_Clear();
         }
     }
     else {
-        dec_c = Py_None;
+        dec_c = (decimalobject*)Py_None;
         Py_INCREF(Py_None);
     }
     res = _do_decimal_power(dec_a, dec_b, dec_c, self);


More information about the Python-checkins mailing list