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

mateusz.rukowicz python-checkins at python.org
Mon Jun 12 23:28:02 CEST 2006


Author: mateusz.rukowicz
Date: Mon Jun 12 23:28:00 2006
New Revision: 46900

Modified:
   sandbox/trunk/decimal-c/_decimal.c
Log:
There is no tabs anymore in code, nor white spaces at end of lines.


Modified: sandbox/trunk/decimal-c/_decimal.c
==============================================================================
--- sandbox/trunk/decimal-c/_decimal.c	(original)
+++ sandbox/trunk/decimal-c/_decimal.c	Mon Jun 12 23:28:00 2006
@@ -69,6 +69,13 @@
 #include "decimal.h"
 #include <stdio.h>
 
+/* Important note:
+ * The code is meant to work well and without errors, not yet to be efficent.
+ * I will start optimizing code, when I will be quite sure, everything
+ * works fine. Real slow code, that should be replaced have comment
+ * "SLOW". */
+
+
 /* integer arithmetic */
 /* Little explanation:
  * Digits are grouped into limbs, that is, every limb keeps LOG digits.
@@ -81,287 +88,287 @@
 
 /* takes new_digits from self[], starting from start_at digit, *digits* not limbs */
 /* returns digit after that we returned (useful for rounding) */
-static long			
-_limb_first_n_digits(long *self, long ndigits, long start_at, long *new, long new_digits)	
+static long    
+_limb_first_n_digits(long *self, long ndigits, long start_at, long *new, long new_digits)    
 {
-		long start_pos = ndigits - (start_at + new_digits);	/* where we start counting from left */
-		long pos = 0;					/* we are here */
-		long self_limb = 0;				/* we start at first digit of first limb */
-		long self_mult = 1;
-		long new_limb = 0;
-		long new_mult = 1;
-		long last_digit = 0;				/* digit from start_pos -1 */
-		long i;
-		
-		for(i = 0;i<new_digits;i+= LOG)
-				new[i/LOG] = 0;
-
-		while(pos < start_pos)			/* we need to set pos to 0 */
-		{
-			if(pos == start_pos - 1)		/* that's our last_digit */
-			{
-				long tmp = self[self_limb];
-				tmp %= self_mult * 10;
-				tmp /= self_mult;
-				last_digit = tmp;
-			}
-					
-			self_mult *= 10;
-			if(self_mult == BASE)
-			{
-				self_limb ++;
-				self_mult = 1;
-			}	
-			pos ++;
-		}
-
-		long diff = start_pos;
-		while (diff < 0)		/* put 0s to new */
-		{
-			new[new_limb] = 0;
-			new_mult *= 10;
-			if(new_mult == BASE)
-			{
-					new_limb ++;
-					new_mult = 1;
-			}
-			diff ++;
-		}
-
-		/* now we will just copy from self to digit */
-		long actual_limb = self[self_limb];
-		actual_limb /= self_mult;
-		
-		/* while don't go out of self and new */
-		while(pos < ndigits && pos - start_pos < new_digits)
-		{
-			if(self_mult == 1)
-					actual_limb = self[self_limb];
-				
-			long x = actual_limb % 10;
-			new[new_limb] += x * new_mult;
-			new_mult *= 10;
-			if(new_mult == BASE)
-			{
-					new_limb ++;
-					new_mult = 1;
-			}
-			actual_limb /= 10;
-			self_mult *= 10;
-
-			if(self_mult == BASE)
-			{
-					self_mult = 1;
-					self_limb ++;
-			}
-			pos ++;
-		}
+    long start_pos = ndigits - (start_at + new_digits);    /* where we start counting from left */
+    long pos = 0;                    /* we are here */
+    long self_limb = 0;                /* we start at first digit of first limb */
+    long self_mult = 1;
+    long new_limb = 0;
+    long new_mult = 1;
+    long last_digit = 0;                /* digit from start_pos -1 */
+    long i;
+    
+    for (i = 0; i < new_digits; i += LOG)
+        new[i/LOG] = 0;
+
+    while (pos < start_pos)            /* we need to set pos to 0 */
+    {
+        if (pos == start_pos - 1)        /* that's our last_digit */
+        {
+            long tmp = self[self_limb];
+            tmp %= self_mult * 10;
+            tmp /= self_mult;
+            last_digit = tmp;
+        }
+
+    self_mult *= 10;
+    if (self_mult == BASE)
+        {
+            self_limb ++;
+            self_mult = 1;
+        }
+        pos ++;
+    }
+
+    long diff = start_pos;
+    while (diff < 0)        /* put 0s to new */
+    {
+        new[new_limb] = 0;
+        new_mult *= 10;
+        if (new_mult == BASE)
+        {
+            new_limb ++;
+            new_mult = 1;
+        }
+        diff ++;
+    }
+
+    /* now we will just copy from self to digit */
+    long actual_limb = self[self_limb];
+    actual_limb /= self_mult;
+
+    /* while don't go out of self and new */
+    while (pos < ndigits && pos - start_pos < new_digits)
+    {
+        if (self_mult == 1)
+            actual_limb = self[self_limb];
 
-		/* rest is 0 */
+        long x = actual_limb % 10;
+        new[new_limb] += x * new_mult;
+        new_mult *= 10;
+        if (new_mult == BASE)
+        {
+            new_limb ++;
+            new_mult = 1;
+        }
+        actual_limb /= 10;
+        self_mult *= 10;
 
-		return last_digit;
+        if(self_mult == BASE)
+        {
+            self_mult = 1;
+            self_limb ++;
+        }
+        pos ++;
+    }
+
+    /* rest is 0 */
+
+    return last_digit;
 }
 
 /* cuts least significant digit */
 static void
 _limb_cut_one_digit(long *self, long ndigits)
 {
-	long limb_count = ndigits/LOG + ((ndigits%LOG) != 0);
-	long i;
+    long limb_count = ndigits/LOG + ((ndigits%LOG) != 0);
+    long i;
 
-	self[0] /= 10;
-	for(i = 1;i<limb_count;i++)
-	{
-			self[i-1] += (self[i]%10) * (BASE/10);
-			self[i] /= 10;
-	}
+    self[0] /= 10;
+    for(i = 1;i<limb_count;i++)
+    {
+        self[i-1] += (self[i]%10) * (BASE/10);
+        self[i] /= 10;
+    }
 }
 
 static void
 _limb_fill(long *self, long ndigits, long x)
 {
-	long full_limb = 0;
-	long mult = 1;
-	long i;
-	while(mult != BASE)
-	{
-		full_limb += x * mult;
-		mult *= 10;
-	}
-
-	long limbs = ndigits / LOG;
-
-	for(i = 0;i<limbs;i++)
-		self[i] = full_limb;
-
-	mult = 1;
-	ndigits -= limbs * LOG;
-	if(ndigits)
-		self[limbs] = 0;
-	
-	while(ndigits--)
-	{
-		self[limbs] += mult * x;
-		mult *= 10;
-	}
+    long full_limb = 0;
+    long mult = 1;
+    long i;
+    while(mult != BASE)
+    {
+        full_limb += x * mult;
+        mult *= 10;
+    }
+
+    long limbs = ndigits / LOG;
+
+    for(i = 0;i<limbs;i++)
+        self[i] = full_limb;
+
+    mult = 1;
+    ndigits -= limbs * LOG;
+    if(ndigits)
+        self[limbs] = 0;
+    
+    while(ndigits--)
+    {
+        self[limbs] += mult * x;
+        mult *= 10;
+    }
 }
 
 static long
 _limb_get_digit(long *self, long ndigits, long i)
 {
-	long pos = ndigits - i - 1;
-	long limb = pos / LOG;
-	pos %= LOG;
-	long tmp = self[limb];
-	while(pos)
-	{
-			tmp/=10;
-			pos --;
-	}
-	return tmp%10;
+    long pos = ndigits - i - 1;
+    long limb = pos / LOG;
+    pos %= LOG;
+    long tmp = self[limb];
+    while(pos)
+    {
+        tmp/=10;
+        pos --;
+    }
+    return tmp%10;
 }
 
 static int
 _limb_compare(long *self, long limbs, long *other, long olimbs)
 {
-	long i;
-	if(limbs != olimbs)
-		return limbs > olimbs ? 1 : -1;
-
-	for(i = limbs-1; i>=0 ;i--)
-		if(self[i] != other[i])
-			return self[i] > other[i] ? 1 : -1;
-	return 0;
+    long i;
+    if(limbs != olimbs)
+        return limbs > olimbs ? 1 : -1;
+
+    for(i = limbs-1; i>=0 ;i--)
+        if(self[i] != other[i])
+            return self[i] > other[i] ? 1 : -1;
+    return 0;
 }
 
 static long
 _limb_add_core(long *big, long bsize, long *small, long ssize, long *out)
 {
-	long max = bsize + 1;
-	long limbs_total = (max + LOG -1)/LOG;
-	long limbs_copy = (bsize + LOG -1)/LOG;
-	long limbs_add = (ssize + LOG -1)/LOG;
-	long i;
-
-	for(i =0 ;i<limbs_add; i++)
-		out[i] = big[i] + small[i];
-
-	for(i = limbs_add;i<limbs_copy;i++)
-		out[i] = big[i];
-
-	for(i = limbs_copy;i<limbs_total;i++)
-		out[i] = 0;
-
-	for(i=0; i< limbs_total;i++)
-	{
-		if(out[i] >= BASE)
-		{
-			out[i] -= BASE;
-			out[i+1] ++;
-		}
-	}
-
-	/* there may be some extra digit at max, check this out */
-	if(_limb_get_digit(out, max, 0) > 0)
-		return max;
-	else
-		return max-1;
-		
+    long max = bsize + 1;
+    long limbs_total = (max + LOG -1)/LOG;
+    long limbs_copy = (bsize + LOG -1)/LOG;
+    long limbs_add = (ssize + LOG -1)/LOG;
+    long i;
+
+    for(i =0 ;i<limbs_add; i++)
+        out[i] = big[i] + small[i];
+
+    for(i = limbs_add;i<limbs_copy;i++)
+        out[i] = big[i];
+
+    for(i = limbs_copy;i<limbs_total;i++)
+        out[i] = 0;
+
+    for(i=0; i< limbs_total;i++)
+    {
+        if(out[i] >= BASE)
+        {
+            out[i] -= BASE;
+            out[i+1] ++;
+        }
+    }
+
+    /* there may be some extra digit at max, check this out */
+    if(_limb_get_digit(out, max, 0) > 0)
+        return max;
+    else
+        return max-1;
+
 }
 
 
 static long
 _limb_add(long *self, long ssize, long *other, long osize, long *out)
 {
-	if(ssize > osize)
-		return _limb_add_core(self, ssize, other, osize, out);
-	else
-		return _limb_add_core(other, osize, self, ssize, out);
+    if(ssize > osize)
+        return _limb_add_core(self, ssize, other, osize, out);
+    else
+        return _limb_add_core(other, osize, self, ssize, out);
 }
 
 static long
 _limb_sub(long *big, long bsize, long *small, long ssize, long *out)
 {
-	long blimbs = (bsize + LOG -1)/LOG;
-	long slimbs = (ssize + LOG -1)/LOG;
-	long i;
-
-	for(i=0;i<blimbs;i++)
-		out[i] = big[i];
-
-	for(i=0;i<slimbs;i++)
-		out[i] -= small[i];
-
-	for(i=0;i<blimbs;i++)
-	{
-		if(out[i] < 0)
-		{
-				out[i] += BASE;
-				out[i+1] --;
-		}
-	}
-
-	long left_limbs = blimbs-1;
-
-	while(!out[left_limbs]) left_limbs --;
-	long left_digits = (left_limbs+1) * LOG;
-	
-	while(_limb_get_digit(out, left_digits, 0) == 0) left_digits --;
-	
-	return left_digits;
+    long blimbs = (bsize + LOG -1)/LOG;
+    long slimbs = (ssize + LOG -1)/LOG;
+    long i;
+
+    for(i=0;i<blimbs;i++)
+        out[i] = big[i];
+
+    for(i=0;i<slimbs;i++)
+        out[i] -= small[i];
+
+    for(i=0;i<blimbs;i++)
+    {
+        if(out[i] < 0)
+        {
+            out[i] += BASE;
+            out[i+1] --;
+        }
+    }
+
+    long left_limbs = blimbs-1;
+
+    while(!out[left_limbs]) left_limbs --;
+    long left_digits = (left_limbs+1) * LOG;
+    
+    while(_limb_get_digit(out, left_digits, 0) == 0) left_digits --;
+    
+    return left_digits;
 }
 /* temporary solution, will be speeded up */
 static long
 _limb_multiply_core(long *first, long flimbs, long *second, long slimbs, long *out)
 {
-	long max_limbs = flimbs + slimbs;
-	long i,j;
-	
-	for(i = 0;i<max_limbs;i++)
-		out[i] = 0;
-
-	for(i = 0;i<flimbs;i++)
-		for(j = 0;j<slimbs;j++)
-		{
-			out[i+j] += first[i] * second[j];
-			if(out[i+j] >= BASE)
-			{
-				out[i+j+1] += out[i+j]/BASE;
-				out[i+j] %= BASE;
-			}
-		}
-
-	for(i = 0;i<max_limbs;i++)
-	{
-		if(out[i] >= BASE)
-		{
-			assert(i+1 < max_limbs);
-			out[i+1] += out[i] / BASE;
-			out[i] %= BASE;
-		}
-	}
+    long max_limbs = flimbs + slimbs;
+    long i,j;
+    
+    for(i = 0;i<max_limbs;i++)
+        out[i] = 0;
 
-	while(!out[max_limbs -1] && max_limbs > 1) max_limbs --;
-	return max_limbs;
+    for(i = 0;i<flimbs;i++)
+        for(j = 0;j<slimbs;j++)
+        {
+            out[i+j] += first[i] * second[j];
+            if(out[i+j] >= BASE)
+            {
+                out[i+j+1] += out[i+j]/BASE;
+                out[i+j] %= BASE;
+            }
+        }
+
+    for(i = 0;i<max_limbs;i++)
+    {
+        if(out[i] >= BASE)
+        {
+            assert(i+1 < max_limbs);
+            out[i+1] += out[i] / BASE;
+            out[i] %= BASE;
+        }
+    }
+
+    while(!out[max_limbs -1] && max_limbs > 1) max_limbs --;
+    return max_limbs;
 }
 
 static long
 _limb_multiply(long *first, long fsize, long *second, long ssize, long *out)
 {
-	long used_limbs;
-	long flimbs, slimbs;
-	long digits_at_most;
-	flimbs = (fsize + LOG - 1)/LOG;
-	slimbs = (ssize + LOG - 1)/LOG;
-	used_limbs = _limb_multiply_core(first, flimbs, second, slimbs, out);
-	
-	digits_at_most = used_limbs * LOG;
-	if(!digits_at_most)
-		return 1;
+    long used_limbs;
+    long flimbs, slimbs;
+    long digits_at_most;
+    flimbs = (fsize + LOG - 1)/LOG;
+    slimbs = (ssize + LOG - 1)/LOG;
+    used_limbs = _limb_multiply_core(first, flimbs, second, slimbs, out);
+    
+    digits_at_most = used_limbs * LOG;
+    if(!digits_at_most)
+        return 1;
 
-	while(_limb_get_digit(out, digits_at_most, 0) == 0 && digits_at_most >1) digits_at_most --;
+    while(_limb_get_digit(out, digits_at_most, 0) == 0 && digits_at_most >1) digits_at_most --;
 
-	return digits_at_most;
+    return digits_at_most;
 }
 
 
@@ -383,9 +390,9 @@
 #define GETNAN(op)    ( ( (op)->sign == SIGN_POSNAN  || (op)->sign == SIGN_NEGNAN ) ? \
                         1 : ( ( (op)->sign == SIGN_POSSNAN || (op)->sign == SIGN_NEGSNAN ) ? 2 : 0 ) )
 
-#define ISINF(op)	( ( (op)->sign == SIGN_POSINF || (op)->sign == SIGN_NEGINF) ?  \
-						((op)->sign == SIGN_POSINF ? 1 : -1) : 0 )
-		
+#define ISINF(op)    ( ( (op)->sign == SIGN_POSINF || (op)->sign == SIGN_NEGINF) ?  \
+                        ((op)->sign == SIGN_POSINF ? 1 : -1) : 0 )
+        
 /* Exponent calculations */
 
 /* XXX: overflow checking? */
@@ -569,18 +576,18 @@
     }
     assert(PyDecimal_Check(thing));
     /* we want to return a NaN, but keep diagnostics */
-	if(thing->sign&1)		/* neg */
-		sign = SIGN_NEGNAN;
-	else
-		sign = SIGN_POSNAN;
+    if(thing->sign&1)        /* neg */
+        sign = SIGN_NEGNAN;
+    else
+        sign = SIGN_POSNAN;
 
-	res = _new_decimalobj(type, thing->ob_size, sign, 0);
+    res = _new_decimalobj(type, thing->ob_size, sign, 0);
     if (!res) return NULL;
     for (i = 0; i < thing->ob_size; i++)
-        res->digits[i] = thing->digits[i];	/* DELETE */
-	
-	for (i = 0; i< res->limb_count;i++)
-		res->limbs[i] = thing->limbs[i];
+        res->digits[i] = thing->digits[i];    /* DELETE */
+    
+    for (i = 0; i< res->limb_count;i++)
+        res->limbs[i] = thing->limbs[i];
     return res;
 }
 
@@ -684,7 +691,7 @@
             if (res) {
                 for (i = 0; i < ctx->prec; i++)
                     res->digits[i] = 9;
-				_limb_fill(res->limbs, ctx->prec, 9);
+                _limb_fill(res->limbs, ctx->prec, 9);
                 return res;
             }
         }
@@ -697,7 +704,7 @@
             if (res) {
                 for (i = 0; i < ctx->prec; i++)
                     res->digits[i] = 9;
-				_limb_fill(res->limbs, ctx->prec, 9);
+                _limb_fill(res->limbs, ctx->prec, 9);
                 return res;
             }
         }
@@ -729,9 +736,9 @@
 {
     decimalobject *new;
     char *arr = NULL;
-	long *arr2 = NULL;
-	long limb_c = ndigits / LOG;
-	limb_c += (ndigits % LOG) > 0;	
+    long *arr2 = NULL;
+    long limb_c = ndigits / LOG;
+    limb_c += (ndigits % LOG) > 0;    
     
     if (ndigits > LONG_MAX) {
         PyErr_NoMemory();
@@ -744,12 +751,12 @@
         goto err;
     }
 
-	arr2 = PyObject_MALLOC(limb_c * sizeof(long));
+    arr2 = PyObject_MALLOC(limb_c * sizeof(long));
 
-	if(!arr2){
-		PyErr_NoMemory();
-		goto err;
-	}
+    if(!arr2){
+        PyErr_NoMemory();
+        goto err;
+    }
     new = (decimalobject *)type->tp_alloc(type, 0);
     if (new == NULL)
         goto err;
@@ -757,13 +764,13 @@
     new->exp = exp;
     new->ob_size = ndigits;
     new->digits = arr;
-	new->limb_count = limb_c;
-	new->limbs = arr2;
+    new->limb_count = limb_c;
+    new->limbs = arr2;
     return new;
         
   err:
     if (arr) PyObject_FREE(arr);
-	if (arr2) PyObject_FREE(arr2);
+    if (arr2) PyObject_FREE(arr2);
     return NULL;
 }
 
@@ -839,23 +846,23 @@
     if (!new) return NULL;
     
 
-	for(i=0;i<self->limb_count;i++)
-			new->limbs[i] = self->limbs[i];
-	
-	if(self->limb_count != new->limb_count)
-		new->limbs[new->limb_count - 1] = 0;	/* we have new limb */
-
-	new->limbs[0] ++;
-	i = 0;
-	while(new->limbs[i] >= BASE)
-	{
-			assert(i+1 < new->limb_count);
-			new->limbs[i] -= BASE;
-			new->limbs[i+1] ++;
-			i++;
-	}
-	if(_limb_get_digit(new->limbs, new->ob_size, 0) == 0)
-		new->ob_size --;
+    for(i=0;i<self->limb_count;i++)
+        new->limbs[i] = self->limbs[i];
+    
+    if(self->limb_count != new->limb_count)
+        new->limbs[new->limb_count - 1] = 0;    /* we have new limb */
+
+    new->limbs[0] ++;
+    i = 0;
+    while(new->limbs[i] >= BASE)
+    {
+        assert(i+1 < new->limb_count);
+        new->limbs[i] -= BASE;
+        new->limbs[i+1] ++;
+        i++;
+    }
+    if(_limb_get_digit(new->limbs, new->ob_size, 0) == 0)
+        new->ob_size --;
     return new;
 }
 
@@ -869,36 +876,36 @@
     for (i = 0; i < prec; i++)
         new->digits[i] = self->digits[i];
 
-	_limb_first_n_digits(self->limbs, self->ob_size, 0, new->limbs, prec);
+    _limb_first_n_digits(self->limbs, self->ob_size, 0, new->limbs, prec);
     return new;
 }
 
 /* Round away from 0. */
 static decimalobject *
-_round_up(decimalobject *self, long prec, long expdiff, contextobject *ctx)			
-{			
+_round_up(decimalobject *self, long prec, long expdiff, contextobject *ctx)
+{
     long i;
     decimalobject *new = _NEW_decimalobj(prec, self->sign, self->exp + expdiff);
     decimalobject *new2 = NULL;
     if (!new) return NULL;
     for (i = 0; i < prec; i++)
         new->digits[i] = self->digits[i];
-	_limb_first_n_digits(self->limbs, self->ob_size, 0, new->limbs, prec);
-	
+    _limb_first_n_digits(self->limbs, self->ob_size, 0, new->limbs, prec);
+    
     for (i = prec; i < self->ob_size; i++)
-		if(_limb_get_digit(self->limbs,self->ob_size, i) > 0){		/* SLOW */
+        if(_limb_get_digit(self->limbs,self->ob_size, i) > 0){        /* SLOW */
             new2 = _decimal_increment(new, 1, ctx);
             Py_DECREF(new);
             if (!new2)
                 return NULL;
             if (new2->ob_size > prec) {
-				_limb_cut_one_digit(new2->limbs,new2->ob_size);				
+                _limb_cut_one_digit(new2->limbs,new2->ob_size);
                 new2->ob_size--;
                 new2->exp++;
             }
             return new2;
         }
-    return new;	
+    return new;
 }
 
 /* Actually round half up. Returns a new reference, either on tmp
@@ -912,12 +919,12 @@
 {
     decimalobject *new;
     assert(expdiff > 0);
-	if(self->ob_size > prec && _limb_get_digit(self->limbs, self->ob_size, prec) >= 5){	/* SLOW */
-		new = _decimal_increment(tmp, 1, ctx);
+    if(self->ob_size > prec && _limb_get_digit(self->limbs, self->ob_size, prec) >= 5){    /* SLOW */
+        new = _decimal_increment(tmp, 1, ctx);
         Py_DECREF(tmp);
         if (!new) return NULL;
         if (new->ob_size > prec) {
-			_limb_cut_one_digit(new->limbs,new->ob_size);
+            _limb_cut_one_digit(new->limbs,new->ob_size);
             new->ob_size--;
             new->exp++;
         }
@@ -939,10 +946,10 @@
     for (i = 0; i < prec; i++)
         tmp->digits[i] = self->digits[i];
 
-	last = _limb_first_n_digits(self->limbs, self->ob_size, 0, tmp->limbs, prec);
+    last = _limb_first_n_digits(self->limbs, self->ob_size, 0, tmp->limbs, prec);
     if (last == 5) {
         for (i = prec+1; i < self->ob_size; i++) {
-			if(_limb_get_digit(self->limbs, self->ob_size, i) != 0)	/* SLOW */
+            if(_limb_get_digit(self->limbs, self->ob_size, i) != 0)    /* SLOW */
                 return _do_round_half_up(self, prec, expdiff, ctx, tmp);
         }
         /* self ends in 5000...., so tmp is okay */
@@ -962,14 +969,14 @@
     if (!tmp) return NULL;
     for (i = 0; i < prec; i++)
         tmp->digits[i] = self->digits[i];
-	last = _limb_first_n_digits(self->limbs, self->ob_size, 0, tmp->limbs, prec);
+    last = _limb_first_n_digits(self->limbs, self->ob_size, 0, tmp->limbs, prec);
     if (last == 5) {
         for (i = prec+1; i < self->ob_size; i++) {
-			if(_limb_get_digit(self->limbs, self->ob_size, i) != 0)	/* SLOW */
+            if(_limb_get_digit(self->limbs, self->ob_size, i) != 0)    /* SLOW */
                 return _do_round_half_up(self, prec, expdiff, ctx, tmp);
         }
-		if((_limb_get_digit(self->limbs, self->ob_size, prec-1)&1) == 0)
-			return tmp;
+        if((_limb_get_digit(self->limbs, self->ob_size, prec-1)&1) == 0)
+            return tmp;
     }
     return _do_round_half_up(self, prec, expdiff, ctx, tmp);
 }
@@ -984,7 +991,7 @@
     if (!tmp) return NULL;
     for (i = 0; i < prec; i++)
         tmp->digits[i] = self->digits[i];
-	_limb_first_n_digits(self->limbs, self->ob_size, 0, tmp->limbs, prec);
+    _limb_first_n_digits(self->limbs, self->ob_size, 0, tmp->limbs, prec);
     return _do_round_half_up(self, prec, expdiff, ctx, tmp);
 }
 
@@ -1055,7 +1062,7 @@
         if (!new) return NULL;
         while (i--)
             new->digits[i] = 0;
-		_limb_fill(new->limbs, new->ob_size,0);
+        _limb_fill(new->limbs, new->ob_size,0);
         
         if (handle_Rounded(ctx, NULL) != 0) {
             Py_DECREF(new);
@@ -1070,7 +1077,7 @@
         new->digits[0] = 0;
         for (i = 1; i < new->ob_size; i++)
             new->digits[i] = self->digits[i-1];
-		_limb_first_n_digits(self->limbs, self->ob_size, -1, new->limbs, new->ob_size);
+        _limb_first_n_digits(self->limbs, self->ob_size, -1, new->limbs, new->ob_size);
         prec = 1;
     } else if (prec < 0) {
         new = _NEW_decimalobj(2, self->sign,
@@ -1078,7 +1085,7 @@
         if (!new) return NULL;
         new->digits[0] = 0;
         new->digits[1] = 1;
-		new->limbs[0] = 1;
+        new->limbs[0] = 1;
         prec = 1;
     } else {
         new = _decimal_get_copy(self);
@@ -1102,24 +1109,24 @@
             new2->digits[i] = 0;
         }
 
-		_limb_first_n_digits(new->limbs, new->ob_size, 0, new2->limbs, new2->ob_size);
+        _limb_first_n_digits(new->limbs, new->ob_size, 0, new2->limbs, new2->ob_size);
         Py_DECREF(new);
         return new2;
     }
 
     /* Maybe all the lost digits are 0. */
     for (i = self->ob_size - expdiff; i < self->ob_size; i++) {
-		if(_limb_get_digit(self->limbs, self->ob_size, i) > 0)
-			goto no_way;
+        if(_limb_get_digit(self->limbs, self->ob_size, i) > 0)
+            goto no_way;
     }
     /* All lost digits are 0, so just clobber new */
 
-	while(new->ob_size > prec)
-	{
-		_limb_cut_one_digit(new->limbs, new->ob_size);	/* VERY SLOW */
-		new->ob_size --;
-	}
-	new->exp += expdiff;
+    while(new->ob_size > prec)
+    {
+        _limb_cut_one_digit(new->limbs, new->ob_size);    /* VERY SLOW */
+        new->ob_size --;
+    }
+    new->exp += expdiff;
     if (handle_Rounded(ctx, NULL) != 0) {
         Py_DECREF(new);
         return NULL;
@@ -1129,7 +1136,7 @@
 no_way:
     /* Now rounding starts. We still own "new". */
     rnd_func = round_funcs[rounding];
-	/*
+    /*
     if (prec != ctx->prec) {
         ctx2 = (contextobject *)context_copy(ctx);
         if (!ctx2) {
@@ -1138,10 +1145,10 @@
         }
         ctx2->prec = prec;
         ctx = ctx2;
-    }*/	/* XXX here is quite subtle bug - we copy context, and set flags in copied context
-		   after that, they are lost, not sure if we really need function above,
-		   I'll comment it */
-	
+    }*/    /* XXX here is quite subtle bug - we copy context, and set flags in copied context
+           after that, they are lost, not sure if we really need function above,
+           I'll comment it */
+    
     /* ctx2 is NULL if the original context is used, because that one
      * doesn't have to be DECREF'd. */
     new2 = rnd_func(new, prec, expdiff, ctx);
@@ -1161,7 +1168,7 @@
     return NULL;
 }
 
-/* Default values: rounding=-1 (use context), watchexp=1 */			/* TODO TODO TODO */
+/* Default values: rounding=-1 (use context), watchexp=1 */
 static decimalobject *
 _decimal_rescale(decimalobject *self, long exp, contextobject *ctx,
                  int rounding, int watchexp)
@@ -1190,7 +1197,7 @@
         if (!ans)
             return NULL;
         ans->digits[0] = 0;
-		ans->limbs[0] = 0;
+        ans->limbs[0] = 0;
         return ans;
     }
 
@@ -1208,7 +1215,7 @@
             return NULL;
         ans->digits[0] = 0;
         ans->digits[1] = 1;
-		ans->limbs[0] = 1;
+        ans->limbs[0] = 1;
         digits = 1;
     } else {
         ans = _NEW_decimalobj(self->ob_size+1, self->sign, self->exp);
@@ -1217,9 +1224,9 @@
         for (i = 0; i < self->ob_size; i++)
             ans->digits[i+1] = self->digits[i];
         ans->digits[0] = 0;
-		for(i=0;i<ans->limb_count;i++)
-			ans->limbs[i] = 0;
-		_limb_first_n_digits(self->limbs, self->ob_size, 0, ans->limbs, ans->ob_size-1);
+        for(i=0;i<ans->limb_count;i++)
+            ans->limbs[i] = 0;
+        _limb_first_n_digits(self->limbs, self->ob_size, 0, ans->limbs, ans->ob_size-1);
     }
 
     tmp = _decimal_round(ans, digits, ctx, rounding);
@@ -1227,11 +1234,11 @@
     if (!tmp)
         return NULL;
 
-	if(_limb_get_digit(tmp -> limbs, tmp->ob_size, 0) == 0 && tmp->ob_size > 1){
-	/* We need one digit less, just clobber tmp. */
+    if(_limb_get_digit(tmp -> limbs, tmp->ob_size, 0) == 0 && tmp->ob_size > 1){
+    /* We need one digit less, just clobber tmp. */
         for (i = 0; i < tmp->ob_size-1; i++)
-            tmp->digits[i] = tmp->digits[i+1];	/* TODO make last digit 0 */
-		tmp->ob_size--;
+            tmp->digits[i] = tmp->digits[i+1];    /* TODO make last digit 0 */
+        tmp->ob_size--;
     }
     tmp->exp = exp;
 
@@ -1253,25 +1260,25 @@
 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;
-	}
+    static char *kwlist[] = {"exp", "rounding", "context", "watchexp", 0};
+    contextobject *ctx = NULL;
+    long exp;
+    int rounding = -1, watchexp = 1;
 
-	return (PyObject*)_decimal_rescale(self,exp,ctx,rounding,watchexp);
+    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 (PyObject*)_decimal_rescale(self,exp,ctx,rounding,watchexp);
 }
 
 /* Fix the exponents and return a copy with the exponents in bounds.
@@ -1288,8 +1295,8 @@
         long Etiny = ETINY(ctx);
         if (self->exp < Etiny) {
             if (!decimal_nonzero(self)) {
-    	    	ans = _decimal_get_copy(self);
-	        	if (!ans)
+                ans = _decimal_get_copy(self);
+                if (!ans)
                     return NULL;
                 ans->exp = Etiny;
                 if (handle_Clamped(ctx, NULL) != 0)
@@ -1380,18 +1387,18 @@
 static PyObject *
 decimal_fix(decimalobject *self, PyObject *args, PyObject *kwds)
 {
-	static char *kwlist[] = {"context",0};
-	contextobject *ctx;
+    static char *kwlist[] = {"context",0};
+    contextobject *ctx;
 
-	if(!PyArg_ParseTupleAndKeywords(args, kwds, "O:_fix", kwlist,
-							&ctx))
-			return NULL;
-
-	if((PyObject*)ctx == Py_None)
-		if(!(ctx = getcontext()))
-			return NULL;
+    if(!PyArg_ParseTupleAndKeywords(args, kwds, "O:_fix", kwlist,
+                            &ctx))
+            return NULL;
 
-	return (PyObject*)_decimal_fix(self,ctx);
+    if((PyObject*)ctx == Py_None)
+        if(!(ctx = getcontext()))
+            return NULL;
+
+    return (PyObject*)_decimal_fix(self,ctx);
 }
 /* convert something to a Decimal. On failure, either
    returns NotImplemented or raises an exception, depending
@@ -1507,13 +1514,13 @@
             return 1;
         }
 
-		if(ISINF(self) || ISINF(other))
-		{
-			if(ISINF(self) == ISINF(other))
-				return 0;
-			else
-				return ISINF(self) > ISINF(other) ? 1 : -1 ;
-		}
+        if(ISINF(self) || ISINF(other))
+        {
+            if(ISINF(self) == ISINF(other))
+                return 0;
+            else
+                return ISINF(self) > ISINF(other) ? 1 : -1 ;
+        }
     }
 
     /* If both are 0, sign comparison isn't correct. */
@@ -1553,10 +1560,10 @@
              
   next:
     /* Adjusted sizes are not equal. */
-	if(adj1 > adj2 && _limb_get_digit(self->limbs, self->ob_size, 0) != 0)
-		return 1 - 2*(self->sign & 1); /* 0 -> 1, 1 -> -1 */
-	else if(adj1 < adj2 && _limb_get_digit(other->limbs, other->ob_size, 0) != 0)
-		return -1 + 2*(other->sign & 1);
+    if(adj1 > adj2 && _limb_get_digit(self->limbs, self->ob_size, 0) != 0)
+        return 1 - 2*(self->sign & 1); /* 0 -> 1, 1 -> -1 */
+    else if(adj1 < adj2 && _limb_get_digit(other->limbs, other->ob_size, 0) != 0)
+        return -1 + 2*(other->sign & 1);
 
     ctx2 = context_copy(ctx);
     if (!ctx2) return 0; /* error */
@@ -1565,7 +1572,7 @@
     if (context_ignore_all_flags(ctx2) == NULL)
         return 0; /* error */
 
-	Py_XDECREF(ctx2);
+    Py_XDECREF(ctx2);
 
     ans = _do_decimal_subtract(self, other, ctx2);
     if (!ans) return 0;
@@ -1631,8 +1638,8 @@
                 contextobject *ctx)
 {
     decimalobject *ans;
-	int cmp;
-	contextobject *ctx2;
+    int cmp;
+    contextobject *ctx2;
     
     if (ISSPECIAL(self) || ISSPECIAL(other)) {
         decimalobject *nan;
@@ -1651,44 +1658,44 @@
             return nan;
     }
 
-	ans = self;		/* let's assume */
-	if(!ctx)
-	{
-		ctx = getcontext();
-	}
-	
-	/* we have to give getcontext() to _do_decimal_compare */
-	ctx2 = getcontext();
-	if(!ctx2)
-		return NULL;
-	
-	cmp = _do_real_decimal_compare(self, other,ctx2);
-	if(PyErr_Occurred())
-		return NULL;
-
-	if(!cmp)
-	{
-		if(self->sign != other->sign)
-		{
-			if(self->sign)
-				ans = other;
-		}
-		else
-		{
-			if(self->exp < other->exp && !self->sign)
-				ans = other;
-			else if(self->exp > other->exp && self->sign)
-				ans = other;
-		}
-	}
-	else if(cmp == -1)
-		ans = other;
+    ans = self;        /* let's assume */
+    if(!ctx)
+    {
+        ctx = getcontext();
+    }
+    
+    /* we have to give getcontext() to _do_decimal_compare */
+    ctx2 = getcontext();
+    if(!ctx2)
+        return NULL;
+    
+    cmp = _do_real_decimal_compare(self, other,ctx2);
+    if(PyErr_Occurred())
+        return NULL;
 
-	if(ctx ->rounding_dec == ALWAYS_ROUND)
-		return _decimal_fix(ans, ctx);
+    if(!cmp)
+    {
+        if(self->sign != other->sign)
+        {
+            if(self->sign)
+                ans = other;
+        }
+        else
+        {
+            if(self->exp < other->exp && !self->sign)
+                ans = other;
+            else if(self->exp > other->exp && self->sign)
+                ans = other;
+        }
+    }
+    else if(cmp == -1)
+        ans = other;
 
-	Py_INCREF(ans);
-	return ans;
+    if(ctx ->rounding_dec == ALWAYS_ROUND)
+        return _decimal_fix(ans, ctx);
+
+    Py_INCREF(ans);
+    return ans;
 }
 DECIMAL_BINARY_FUNC(max)
 
@@ -1697,70 +1704,70 @@
 _do_decimal_min(decimalobject *self, decimalobject *other,
                 contextobject *ctx)
 {
-	decimalobject *ans;
-	int cmp;
-	contextobject *ctx2;
-
-	if(ISSPECIAL(self) || ISSPECIAL(other))
-	{
-		decimalobject *nan;
-		int sn = GETNAN(self);
-		int so = GETNAN(other);
-
-		if(sn || so)
-		{
-			if(so == 1 && sn != 2)
-			{
-				Py_INCREF(self);
-				return self;
-			}
-			else if (sn == 1 && so != 2)
-			{
-				Py_INCREF(other);
-				return other;
-			}
-		}
-
-		if(_check_nans(self, other, ctx, &nan) != 0)
-				return nan;
-	}
-
-	ans = self;
-	if(!ctx)
-		ctx = getcontext();
-	if(!ctx)
-		return NULL;
-
-	ctx2 = getcontext();
-	if(!ctx2)
-		return NULL;
-	cmp = _do_real_decimal_compare(self,other,ctx2);
-
-	if(PyErr_Occurred())
-		return NULL;
-
-	if(!cmp)
-	{
-		if(self->sign != other->sign)
-		{
-			if(!self->sign)
-				ans = other;
-		}
-		else{	
-		if(self->exp > other->exp && !self->sign)
-			ans = other;
-		else if (self->exp < other->exp && self->sign)
-			ans = other;
-		}
-	}
-	else if(cmp == 1)
-		ans = other;
-
-	if(ctx->rounding_dec == ALWAYS_ROUND)
-		return _decimal_fix(ans, ctx);
-	
-	Py_INCREF(ans);
-	return ans;
+    decimalobject *ans;
+    int cmp;
+    contextobject *ctx2;
+
+    if(ISSPECIAL(self) || ISSPECIAL(other))
+    {
+        decimalobject *nan;
+        int sn = GETNAN(self);
+        int so = GETNAN(other);
+
+        if(sn || so)
+        {
+            if(so == 1 && sn != 2)
+            {
+                Py_INCREF(self);
+                return self;
+            }
+            else if (sn == 1 && so != 2)
+            {
+                Py_INCREF(other);
+                return other;
+            }
+        }
+
+        if(_check_nans(self, other, ctx, &nan) != 0)
+                return nan;
+    }
+
+    ans = self;
+    if(!ctx)
+        ctx = getcontext();
+    if(!ctx)
+        return NULL;
+
+    ctx2 = getcontext();
+    if(!ctx2)
+        return NULL;
+    cmp = _do_real_decimal_compare(self,other,ctx2);
+
+    if(PyErr_Occurred())
+        return NULL;
+
+    if(!cmp)
+    {
+        if(self->sign != other->sign)
+        {
+            if(!self->sign)
+                ans = other;
+        }
+        else{    
+        if(self->exp > other->exp && !self->sign)
+            ans = other;
+        else if (self->exp < other->exp && self->sign)
+            ans = other;
+        }
+    }
+    else if(cmp == 1)
+        ans = other;
+
+    if(ctx->rounding_dec == ALWAYS_ROUND)
+        return _decimal_fix(ans, ctx);
+    
+    Py_INCREF(ans);
+    return ans;
 
 }
 DECIMAL_BINARY_FUNC(min)
@@ -1786,15 +1793,15 @@
         new = _NEW_decimalobj(1, dup->sign, 0);
         Py_DECREF(dup);
         if (!new) return NULL;
-		new->limbs[0] = 0;
+        new->limbs[0] = 0;
         return new;
     }
     
     /* strip trailing 0s from dup */
-	while(_limb_get_digit(dup->limbs, dup->ob_size, dup->ob_size -1) == 0){
-		_limb_cut_one_digit(dup->limbs,dup->ob_size);
-		dup->ob_size --;
-		dup->exp ++;
+    while(_limb_get_digit(dup->limbs, dup->ob_size, dup->ob_size -1) == 0){
+        _limb_cut_one_digit(dup->limbs,dup->ob_size);
+        dup->ob_size --;
+        dup->exp ++;
     }
     return dup;
 }
@@ -1960,8 +1967,8 @@
                                      &rounding, &ctx))
         return NULL;
     ENSURE_CONTEXT("to_integral", ctx);
-	if(rounding == -1)
-		rounding = ctx->rounding;
+    if(rounding == -1)
+        rounding = ctx->rounding;
     if (!VALID_ROUND(rounding)) {
         PyErr_SetString(PyExc_ValueError, "invalid rounding value");
         return NULL;
@@ -2014,8 +2021,8 @@
         return NULL;
     for (i = 0; i < self->ob_size; i++)
         new->digits[i] = self->digits[i];
-	for (i = 0;i < new->limb_count;i++)
-		new->limbs[i] = self->limbs[i];
+    for (i = 0;i < new->limb_count;i++)
+        new->limbs[i] = self->limbs[i];
     return new;
 }
 
@@ -2042,15 +2049,15 @@
         return NULL;
     
     for (i = 0; i < self->ob_size; i++) {
-        d = PyInt_FromLong(_limb_get_digit(self->limbs, self->ob_size, i));	/* SLOW */
+        d = PyInt_FromLong(_limb_get_digit(self->limbs, self->ob_size, i));    /* SLOW */
         if (!d) return NULL;
         PyTuple_SET_ITEM(digits, i, d);
     }
 
-	if(!ISINF(self))
-	    res = Py_BuildValue("(bOn)", self->sign % 2, digits, self->exp);
-	else
-		res = Py_BuildValue("(bOO)", self->sign % 2, digits, PyString_FromString("F"));
+    if(!ISINF(self))
+        res = Py_BuildValue("(bOn)", self->sign % 2, digits, self->exp);
+    else
+        res = Py_BuildValue("(bOO)", self->sign % 2, digits, PyString_FromString("F"));
     Py_DECREF(digits);
     return res;
 }
@@ -2086,14 +2093,14 @@
         SANITY_CHECK(p);
 
         /* check for digits != {0, } */
-		if(d->ob_size != 1 || _limb_get_digit(d->limbs, d->ob_size, 0) != 0)
-		{
-			for(i=0;i< d->ob_size;i++)
-			{
-				p+= sprintf(p, "%d", _limb_get_digit(d->limbs, d->ob_size, i));	/* SLOW */
-				SANITY_CHECK(p);
-			}
-		}
+        if(d->ob_size != 1 || _limb_get_digit(d->limbs, d->ob_size, 0) != 0)
+        {
+            for(i=0;i< d->ob_size;i++)
+            {
+                p+= sprintf(p, "%d", _limb_get_digit(d->limbs, d->ob_size, i));    /* SLOW */
+                SANITY_CHECK(p);
+            }
+        }
     } else { /* infinity */
         p += sprintf(p, "%s", "Infinity");
     }
@@ -2110,27 +2117,27 @@
     char *p, *end;
     int roundexp, i;
 
-	end = &outbuf[FORMAT_SIZE-1];
+    end = &outbuf[FORMAT_SIZE-1];
     p = outbuf;
     if (d->sign & 1) {
         *p++ = '-';
         SANITY_CHECK(p);
     }
     if (d->exp < 0 && d->exp >= -6) {
-		
-		i = 0;
-		*p++ = '0';
-		*p++ = '.';
-		for (;i<abs(d->exp);i++)
-			*p++ = '0';
+        
+        i = 0;
+        *p++ = '0';
+        *p++ = '.';
+        for (;i<abs(d->exp);i++)
+            *p++ = '0';
         SANITY_CHECK(p);
     } else {
-		roundexp = d->exp;
-		while(roundexp%3)
-			roundexp ++;
-		if (roundexp != d->exp) {
-			*p++ = '0';
-			*p++ = '.';
+        roundexp = d->exp;
+        while(roundexp%3)
+            roundexp ++;
+        if (roundexp != d->exp) {
+            *p++ = '0';
+            *p++ = '.';
             for (i = 0; i < (roundexp - d->exp); i++)
                 *p++ = '0';
             SANITY_CHECK(p);
@@ -2152,8 +2159,8 @@
             SANITY_CHECK(p);
         }
     }
-	*p++ = 0;
-	p = &outbuf[0];
+    *p++ = 0;
+    p = &outbuf[0];
     return PyString_FromString(p);
 }
 
@@ -2172,14 +2179,14 @@
 }
 
 
-static PyObject *	
+static PyObject *    
 _do_decimal_str(decimalobject *d, contextobject *context, int engineering)
 {
     char *outbuf;
     int buflen, i;
     int dotplace, adjexp;
     int append_E = 0, append_adjexp = 0;
-	long extra_zeros=0;
+    long extra_zeros=0;
     char *p, *end;
     PyObject *ret;
 
@@ -2206,66 +2213,66 @@
         *p++ = '-';
     }
 
-	/* The rule is as follows, imagine you write integer:
-	 * 123456789 we want to print it, without adding post-zeros
-	 * or adding at most 6 pre-zeros that is
-	 * 0.00000123456789, if none of this can be done, then, we put dot
-	 * after first digit, and compute E *or* if engineering, then, we put
-	 * dot after 1st 2nd or 3rd digit, and assure that E is divisible by 3
-	 * huh */
-
-	/* (1) dotplace = -adjexp + d->exp + d->ob_size */
-	/* why is that like? well, d->exp moves dot right, d->ob_size moves dot right
-	 * and adjexp moves dot left */
-	adjexp = 0;
-	dotplace = d->exp + d->ob_size;
-	/* dotplace must be at most d->ob_size (no dot at all) and at last -5 (6 pre-zeros)*/
-	if(dotplace >d->ob_size || dotplace <-5)
-	{
-			/* ok, we have to put dot after 1 digit, that is dotplace = 1 
-			 * we compute adjexp from equation (1) */
-			dotplace = 1;
-			adjexp = -dotplace + d->exp + d->ob_size;
-	}
-
-	if(engineering)		/* we have to be sure, adjexp%3 == 0 */
-		while(adjexp%3)
-		{
-			adjexp --;
-			dotplace ++;
-		}
-
-	/* now all we have to do, is to put it to the string =] */
-
-	if(dotplace > d->ob_size)
-		extra_zeros = dotplace - d->ob_size;
-
-	if(dotplace <= 0)
-	{
-		*p++ = '0';
-		*p++ = '.';
-		while(dotplace++ < 0)
-			*p++ = '0';
-		dotplace = -1;
-	}
-
-	for(i = 0;i<d->ob_size;i++)
-	{
-		if(dotplace == i)
-			*p++ = '.';
-		p += sprintf(p, "%d", _limb_get_digit(d->limbs,d->ob_size,i));
-	}
-
-	while(extra_zeros --)*p++ = '0';
-	
-	/* that was a way easier way =] */
-	
-
-	if(adjexp)
-	{
-		append_E = 1;
-		append_adjexp = 1;
-	}
+    /* The rule is as follows, imagine you write integer:
+     * 123456789 we want to print it, without adding post-zeros
+     * or adding at most 6 pre-zeros that is
+     * 0.00000123456789, if none of this can be done, then, we put dot
+     * after first digit, and compute E *or* if engineering, then, we put
+     * dot after 1st 2nd or 3rd digit, and assure that E is divisible by 3
+     * huh */
+
+    /* (1) dotplace = -adjexp + d->exp + d->ob_size */
+    /* why is that like? well, d->exp moves dot right, d->ob_size moves dot right
+     * and adjexp moves dot left */
+    adjexp = 0;
+    dotplace = d->exp + d->ob_size;
+    /* dotplace must be at most d->ob_size (no dot at all) and at last -5 (6 pre-zeros)*/
+    if(dotplace >d->ob_size || dotplace <-5)
+    {
+        /* ok, we have to put dot after 1 digit, that is dotplace = 1 
+         * we compute adjexp from equation (1) */
+        dotplace = 1;
+        adjexp = -dotplace + d->exp + d->ob_size;
+    }
+
+    if(engineering)        /* we have to be sure, adjexp%3 == 0 */
+        while(adjexp%3)
+        {
+            adjexp --;
+            dotplace ++;
+        }
+
+    /* now all we have to do, is to put it to the string =] */
+
+    if(dotplace > d->ob_size)
+        extra_zeros = dotplace - d->ob_size;
+
+    if(dotplace <= 0)
+    {
+        *p++ = '0';
+        *p++ = '.';
+        while(dotplace++ < 0)
+            *p++ = '0';
+        dotplace = -1;
+    }
+
+    for(i = 0;i<d->ob_size;i++)
+    {
+        if(dotplace == i)
+            *p++ = '.';
+        p += sprintf(p, "%d", _limb_get_digit(d->limbs,d->ob_size,i));
+    }
+
+    while(extra_zeros --)*p++ = '0';
+    
+    /* that was a way easier way =] */
+    
+
+    if(adjexp)
+    {
+        append_E = 1;
+        append_adjexp = 1;
+    }
     if (append_E) {
         if (context->capitals) {
             *p++ = 'E';
@@ -2287,7 +2294,7 @@
     *p++ = 0;
     SANITY_CHECK(p);
     
-	p = &outbuf[1];
+    p = &outbuf[1];
     SANITY_CHECK(p);
     ret = PyString_FromString(p);
     PyMem_FREE(outbuf);
@@ -2325,12 +2332,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")},
+    {"_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.")},
@@ -2413,27 +2420,27 @@
     static PyObject *                                                   \
     func (PyObject *self, PyObject *other) {                            \
         decimalobject *res;                                             \
-		PyObject *to_decref;										\
+        PyObject *to_decref;                                        \
         contextobject *ctx = getcontext();                              \
         if (!ctx) return NULL;                                          \
-		if(PyDecimal_Check(self))										\
-		{																\
+        if(PyDecimal_Check(self))                                        \
+        {                                                                \
         other = (PyObject*)_convert_to_decimal(self->ob_type, other, ctx, 0);      \
-  	    if (other == NULL || other == Py_NotImplemented) return other;  \
-		to_decref = other;												\
-		}																\
-		else															\
-		{																\
-		assert(PyDecimal_Check(other));									\
-		self = (PyObject*)_convert_to_decimal(other->ob_type, self, ctx, 0);	\
-		to_decref = self;												\
-		if(self == NULL || self == Py_NotImplemented) return self;	\
-		}																\
+          if (other == NULL || other == Py_NotImplemented) return other;  \
+        to_decref = other;                                                \
+        }                                                                \
+        else                                                            \
+        {                                                                \
+        assert(PyDecimal_Check(other));                                    \
+        self = (PyObject*)_convert_to_decimal(other->ob_type, self, ctx, 0);    \
+        to_decref = self;                                                \
+        if(self == NULL || self == Py_NotImplemented) return self;    \
+        }                                                                \
         res = _do_##func((decimalobject *)self,                         \
                          (decimalobject *)other, ctx);                  \
         Py_DECREF(to_decref);                                              \
         return (PyObject *)res;                                         \
-    }															
+    }                                                            
 
 /* Same for one-argument methods. */
 #define DECIMAL_SPECIAL_1FUNC(func)                                 \
@@ -2450,13 +2457,13 @@
 _do_decimal_add(decimalobject *self, decimalobject *other,
                 contextobject *ctx)
 {
-	assert(PyDecimal_Check(other));
-	assert(PyDecimal_Check(self));
+    assert(PyDecimal_Check(other));
+    assert(PyDecimal_Check(self));
     int shouldround, sign, negativezero = 0;
     long exp, oexp;
-	long prec,cmp;
+    long prec,cmp;
     decimalobject *res, *res2, *ret, *o1, *o2;
-	prec = ctx->prec;
+    prec = ctx->prec;
     
     if (ISSPECIAL(self) || ISSPECIAL(other)) {
         decimalobject *nan;
@@ -2492,7 +2499,7 @@
         res = _new_decimalobj(self->ob_type, 1, sign, exp);
         if (!res) return NULL;
         res->digits[0] = 0;
-		res->limbs[0] = 0;
+        res->limbs[0] = 0;
         return res;
     }
     if (!decimal_nonzero(self)) {
@@ -2522,161 +2529,161 @@
         }            
     }
 
-	decimalobject *tmp;	/* we borrow refference */
-	decimalobject *oother;
+    decimalobject *tmp;    /* we borrow refference */
+    decimalobject *oother;
 
-	long numdigits = self->exp - other->exp;
+    long numdigits = self->exp - other->exp;
 
-	if(numdigits < 0)
-	{
-		numdigits *= -1;
-		tmp = other;
-		oother = self;
-	}
-	else
-	{
-		tmp = self;
-		oother = other;
-	}
-	
-	/* we borrow refference */
-	if(shouldround && numdigits > prec + 1)
-	{
-		if(numdigits > (oother->ob_size + prec + 1 - tmp->ob_size))
-		{
-			long extend = prec + 2 - tmp->ob_size;
-			if(extend <= 0)
-				extend = 1;
-			o1 = _NEW_decimalobj(tmp->ob_size + extend, tmp->sign, tmp->exp - extend);
-			if(!o1)
-				return NULL;
-
-			_limb_first_n_digits(tmp->limbs, tmp->ob_size, 0, o1->limbs, o1->ob_size);
-			o2 = _NEW_decimalobj(1, oother->sign, o1->exp);
-			if(!o2)
-			{
-				Py_XDECREF(o1);
-				return NULL;
-			}
-			o2->limbs[0] =1;
-			goto calc;
-		}	
-	}
-
-	o1 = _NEW_decimalobj(tmp->ob_size + numdigits, tmp->sign, tmp->exp - numdigits);
-	_limb_first_n_digits(tmp->limbs, tmp->ob_size, 0, o1->limbs, o1->ob_size);
-
-	if(!o1)
-	{
-		return NULL;
-	}
-	o2 = oother;
-	Py_INCREF(o2);
+    if(numdigits < 0)
+    {
+        numdigits *= -1;
+        tmp = other;
+        oother = self;
+    }
+    else
+    {
+        tmp = self;
+        oother = other;
+    }
+    
+    /* we borrow refference */
+    if(shouldround && numdigits > prec + 1)
+    {
+        if(numdigits > (oother->ob_size + prec + 1 - tmp->ob_size))
+        {
+            long extend = prec + 2 - tmp->ob_size;
+            if(extend <= 0)
+                extend = 1;
+            o1 = _NEW_decimalobj(tmp->ob_size + extend, tmp->sign, tmp->exp - extend);
+            if(!o1)
+                return NULL;
+
+            _limb_first_n_digits(tmp->limbs, tmp->ob_size, 0, o1->limbs, o1->ob_size);
+            o2 = _NEW_decimalobj(1, oother->sign, o1->exp);
+            if(!o2)
+            {
+                Py_XDECREF(o1);
+                return NULL;
+            }
+            o2->limbs[0] =1;
+            goto calc;
+        }    
+    }
+
+    o1 = _NEW_decimalobj(tmp->ob_size + numdigits, tmp->sign, tmp->exp - numdigits);
+    _limb_first_n_digits(tmp->limbs, tmp->ob_size, 0, o1->limbs, o1->ob_size);
+
+    if(!o1)
+    {
+        return NULL;
+    }
+    o2 = oother;
+    Py_INCREF(o2);
 
 
 calc:
 
-	assert(o1->exp == o2->exp);
-	exp = o1->exp;
-	cmp = _limb_compare(o1->limbs, o1->limb_count, o2->limbs, o2->limb_count);
-
-	if(o1->sign != o2->sign)
-	{
-		if(!cmp)
-		{
-			Py_DECREF(o1);
-			Py_DECREF(o2);
-			ret = _NEW_decimalobj(1, negativezero, exp);
-			if(!ret)
-				return NULL;
-			ret->limbs[0] = 0;
-			if(ret->exp < ETINY(ctx))
-			{
-				ret->exp = ETINY(ctx);
-				if(handle_Clamped(ctx, NULL) != 0)
-				{
-					Py_DECREF(ret);
-					return NULL;	
-				}
-			}
-			
-			return ret;
-		}
-		else
-		{
-			long max_size;
-			if(cmp == -1)
-			{
-				ret = o1;
-				o1 = o2;
-				o2 = ret;
-				ret = 0;
-			}
-			/* o1 > o2 */
-			max_size = o1->ob_size > o2->ob_size ? o1->ob_size : o2->ob_size;
-			ret = _NEW_decimalobj(max_size, o1->sign, o1->exp);
-			if(!ret)
-			{
-				Py_DECREF(o1);
-				Py_DECREF(o2);
-				return NULL;
-			}
-
-			max_size = _limb_sub(o1->limbs, o1->ob_size, o2->limbs, o2->ob_size, ret->limbs);
-			ret->ob_size = max_size;
-			ret->limb_count = (max_size + LOG -1)/LOG;
-			
-		}
-	}
-	else
-	{
-		long max_size;
-		max_size = o1->ob_size > o2->ob_size ? o1->ob_size : o2->ob_size;
-		ret = _NEW_decimalobj(max_size + 1, o1->sign, o1->exp);	/* we may have extra digit */
-		if(!ret)
-		{
-			Py_DECREF(o1);
-			Py_DECREF(o2);
-			return NULL;
-		}
-		
-		max_size = _limb_add(o1->limbs, o1->ob_size, o2->limbs, o2->ob_size, ret->limbs);
-		ret->ob_size = max_size;
-		ret->limb_count = (max_size + LOG -1)/LOG;
+    assert(o1->exp == o2->exp);
+    exp = o1->exp;
+    cmp = _limb_compare(o1->limbs, o1->limb_count, o2->limbs, o2->limb_count);
+
+    if(o1->sign != o2->sign)
+    {
+        if(!cmp)
+        {
+            Py_DECREF(o1);
+            Py_DECREF(o2);
+            ret = _NEW_decimalobj(1, negativezero, exp);
+            if(!ret)
+                return NULL;
+            ret->limbs[0] = 0;
+            if(ret->exp < ETINY(ctx))
+            {
+                ret->exp = ETINY(ctx);
+                if(handle_Clamped(ctx, NULL) != 0)
+                {
+                    Py_DECREF(ret);
+                    return NULL;    
+                }
+            }
+            
+            return ret;
+        }
+        else
+        {
+            long max_size;
+            if(cmp == -1)
+            {
+                ret = o1;
+                o1 = o2;
+                o2 = ret;
+                ret = 0;
+            }
+            /* o1 > o2 */
+            max_size = o1->ob_size > o2->ob_size ? o1->ob_size : o2->ob_size;
+            ret = _NEW_decimalobj(max_size, o1->sign, o1->exp);
+            if(!ret)
+            {
+                Py_DECREF(o1);
+                Py_DECREF(o2);
+                return NULL;
+            }
+
+            max_size = _limb_sub(o1->limbs, o1->ob_size, o2->limbs, o2->ob_size, ret->limbs);
+            ret->ob_size = max_size;
+            ret->limb_count = (max_size + LOG -1)/LOG;
+            
+        }
+    }
+    else
+    {
+        long max_size;
+        max_size = o1->ob_size > o2->ob_size ? o1->ob_size : o2->ob_size;
+        ret = _NEW_decimalobj(max_size + 1, o1->sign, o1->exp);    /* we may have extra digit */
+        if(!ret)
+        {
+            Py_DECREF(o1);
+            Py_DECREF(o2);
+            return NULL;
+        }
+        
+        max_size = _limb_add(o1->limbs, o1->ob_size, o2->limbs, o2->ob_size, ret->limbs);
+        ret->ob_size = max_size;
+        ret->limb_count = (max_size + LOG -1)/LOG;
 /*
-		Py_DECREF(o1);
-		Py_DECREF(o2);
-		if(shouldround)
-		{
-			fixed = _decimal_fix(ret, ctx);
-			Py_DECREF(ret);
-			if(!fixed)
-			{
-				return NULL;
-			}
-			return fixed;
-		}
-		return ret; */
-	}
-
-	Py_DECREF(o1);
-	Py_DECREF(o2);
-	
-	if(shouldround)
-	{
-		decimalobject *fixed;
-		fixed = _decimal_fix(ret,ctx);
-		Py_DECREF(ret);
-		if(!fixed)
-			return NULL;
-		return fixed; 
-	}
-	
-	return ret;
-	
-	Py_XDECREF(o1);
-	Py_XDECREF(o2);	
-	return NULL;
+        Py_DECREF(o1);
+        Py_DECREF(o2);
+        if(shouldround)
+        {
+            fixed = _decimal_fix(ret, ctx);
+            Py_DECREF(ret);
+            if(!fixed)
+            {
+                return NULL;
+            }
+            return fixed;
+        }
+        return ret; */
+    }
+
+    Py_DECREF(o1);
+    Py_DECREF(o2);
+    
+    if(shouldround)
+    {
+        decimalobject *fixed;
+        fixed = _decimal_fix(ret,ctx);
+        Py_DECREF(ret);
+        if(!fixed)
+            return NULL;
+        return fixed; 
+    }
+    
+    return ret;
+    
+    Py_XDECREF(o1);
+    Py_XDECREF(o2);    
+    return NULL;
 }
 DECIMAL_SPECIAL_2FUNC(decimal_add)
 
@@ -2714,98 +2721,98 @@
 _do_decimal_multiply(decimalobject *self, decimalobject *other,
                      contextobject *ctx)
 {
-	long resultsign;
-	long resultexp;
-	long shouldround;
-	long i;
-	long max_limbs;
-	long size;
-	decimalobject *ans;
-
-	resultsign = (self->sign&1) ^ (other->sign&1);
-	
-	if(ISSPECIAL(self) || ISSPECIAL(other))
-	{
-		decimalobject *nan;
-		if(_check_nans(self, other, ctx, &nan))
-			return nan;
-
-		if(ISINF(self))
-		{
-			if(decimal_nonzero(other))
-			{
-				ans = _NEW_decimalobj(1, resultsign ? SIGN_NEGINF : SIGN_POSINF, 0);
-				return ans;
-			}
-			return handle_InvalidOperation(self->ob_type, ctx, "(+-)INF * 0", NULL);
-		}
-
-		if(ISINF(other))
-		{
-			if(decimal_nonzero(self))
-			{
-				ans = _NEW_decimalobj(1, resultsign ? SIGN_NEGINF : SIGN_POSINF, 0);
-				return ans;
-			}
-			return handle_InvalidOperation(self->ob_type, ctx, "0 * (+-)INF", NULL);
-		}
-	}
-
-	resultexp = self->exp + other->exp;
-	shouldround = ctx->rounding_dec == ALWAYS_ROUND;
-
-	if(!decimal_nonzero(self) || !decimal_nonzero(other))
-	{
-		decimalobject *ret;
-		ans = _NEW_decimalobj(1, resultsign, resultexp);
-		if(!ans)
-			return NULL;
-
-		ans->limbs[0] = 0;
-		goto done;
-	}
-
-	if(self->ob_size == 1 && self->limbs[0] == 1)
-	{
-		ans = _NEW_decimalobj(other->ob_size, resultsign, resultexp);
-		if(!ans)
-			return NULL;
-		
-		for(i = 0; i<ans->limb_count;i++)
-			ans->limbs[i] = other->limbs[i];
-		goto done;
-	}
-
-	if(other->ob_size == 1 && other->limbs[0] == 1)
-	{
-		ans = _NEW_decimalobj(self->ob_size, resultsign, resultexp);
-		if(!ans)
-			return NULL;
-
-		for(i = 0; i<ans->limb_count; i++)
-			ans->limbs[i] = self->limbs[i];
-
-		goto done;
-	}
-
-	max_limbs = (self->ob_size + LOG -1)/ LOG + (other->ob_size + LOG -1)/LOG;
-	ans = _NEW_decimalobj(max_limbs * LOG, resultsign, resultexp);
-	if(!ans)
-		return NULL;
+    long resultsign;
+    long resultexp;
+    long shouldround;
+    long i;
+    long max_limbs;
+    long size;
+    decimalobject *ans;
 
-	size = _limb_multiply(self->limbs, self->ob_size, other->limbs, other->ob_size, ans->limbs);
+    resultsign = (self->sign&1) ^ (other->sign&1);
+    
+    if(ISSPECIAL(self) || ISSPECIAL(other))
+    {
+        decimalobject *nan;
+        if(_check_nans(self, other, ctx, &nan))
+            return nan;
 
-	ans->ob_size = size;
-	
+        if(ISINF(self))
+        {
+            if(decimal_nonzero(other))
+            {
+                ans = _NEW_decimalobj(1, resultsign ? SIGN_NEGINF : SIGN_POSINF, 0);
+                return ans;
+            }
+            return handle_InvalidOperation(self->ob_type, ctx, "(+-)INF * 0", NULL);
+        }
+
+        if(ISINF(other))
+        {
+            if(decimal_nonzero(self))
+            {
+                ans = _NEW_decimalobj(1, resultsign ? SIGN_NEGINF : SIGN_POSINF, 0);
+                return ans;
+            }
+            return handle_InvalidOperation(self->ob_type, ctx, "0 * (+-)INF", NULL);
+        }
+    }
+
+    resultexp = self->exp + other->exp;
+    shouldround = ctx->rounding_dec == ALWAYS_ROUND;
+
+    if(!decimal_nonzero(self) || !decimal_nonzero(other))
+    {
+        decimalobject *ret;
+        ans = _NEW_decimalobj(1, resultsign, resultexp);
+        if(!ans)
+            return NULL;
+
+        ans->limbs[0] = 0;
+        goto done;
+    }
+
+    if(self->ob_size == 1 && self->limbs[0] == 1)
+    {
+        ans = _NEW_decimalobj(other->ob_size, resultsign, resultexp);
+        if(!ans)
+            return NULL;
+        
+        for(i = 0; i<ans->limb_count;i++)
+            ans->limbs[i] = other->limbs[i];
+        goto done;
+    }
+
+    if(other->ob_size == 1 && other->limbs[0] == 1)
+    {
+        ans = _NEW_decimalobj(self->ob_size, resultsign, resultexp);
+        if(!ans)
+            return NULL;
+
+        for(i = 0; i<ans->limb_count; i++)
+            ans->limbs[i] = self->limbs[i];
+
+        goto done;
+    }
+
+    max_limbs = (self->ob_size + LOG -1)/ LOG + (other->ob_size + LOG -1)/LOG;
+    ans = _NEW_decimalobj(max_limbs * LOG, resultsign, resultexp);
+    if(!ans)
+        return NULL;
+
+    size = _limb_multiply(self->limbs, self->ob_size, other->limbs, other->ob_size, ans->limbs);
+
+    ans->ob_size = size;
+    
 
 done:
-	if(shouldround)
-	{
-		decimalobject *fixed = _decimal_fix(ans, ctx);
-		Py_DECREF(ans);
-		return fixed;
-	}
-	return ans;
+    if(shouldround)
+    {
+        decimalobject *fixed = _decimal_fix(ans, ctx);
+        Py_DECREF(ans);
+        return fixed;
+    }
+    return ans;
 }
 DECIMAL_SPECIAL_2FUNC(decimal_multiply)
 
@@ -2869,13 +2876,13 @@
 }
 
 
-static PyObject *                                                       
-decimal_power(PyObject *self, PyObject *other, PyObject *modulo) {                               
-    decimalobject *res;                                                
+static PyObject *
+decimal_power(PyObject *self, PyObject *other, PyObject *modulo) {
+    decimalobject *res;
     contextobject *ctx = getcontext();
-    if (!ctx) return NULL;                                              
+    if (!ctx) return NULL;
 
-    other = _convert_to_decimal(self->ob_type, other, ctx, 0);          
+    other = _convert_to_decimal(self->ob_type, other, ctx, 0);
     if (other == NULL || other == Py_NotImplemented) return other;
     
     if (modulo == Py_None) {
@@ -2887,10 +2894,10 @@
             return modulo;
         }
     }
-    res = _do_decimal_power((decimalobject *)self,                             
+    res = _do_decimal_power((decimalobject *)self,
                             (decimalobject *)other,
                             (decimalobject *)modulo,
-                            ctx);                      
+                            ctx);
     Py_DECREF(other);
     Py_DECREF(modulo);
     return (PyObject *)res;
@@ -3015,8 +3022,8 @@
     buf[0] = (self->sign & 1 ? '-' : '+');
     for (i = 0; i < max; i++) {
         if (i < self->ob_size)
-			buf[i+1] = _limb_get_digit(self->limbs, self->ob_size, i) + '0';	/* SLOW */
-		else
+            buf[i+1] = _limb_get_digit(self->limbs, self->ob_size, i) + '0';    /* SLOW */
+        else
             buf[i+1] = '0';
     }
     buf[max+1] = 0;
@@ -3058,8 +3065,8 @@
 
     for (i = 0; i < max; i++) {
         if (i < self->ob_size)
-			res = res * 10 + _limb_get_digit(self->limbs, self->ob_size, i);	/* SLOW */
-		else
+            res = res * 10 + _limb_get_digit(self->limbs, self->ob_size, i);    /* SLOW */
+        else
             res *= 10;
     }
     if (self->sign & 1) res = -res;
@@ -3087,9 +3094,9 @@
     if (ISSPECIAL(self))
         return 1;
 
-	for(i=0;i<self->limb_count;i++)
-		if(self->limbs[i] != 0) return 1;
-	return 0;
+    for(i=0;i<self->limb_count;i++)
+        if(self->limbs[i] != 0) return 1;
+    return 0;
 }
 
 
@@ -3162,7 +3169,7 @@
 decimal_dealloc(decimalobject *d)
 {
     PyObject_FREE(d->digits);
-	PyObject_FREE(d->limbs);
+    PyObject_FREE(d->limbs);
     d->ob_type->tp_free(d);
 }
 
@@ -3179,14 +3186,14 @@
         return NULL;
     /* remove sign */
     if (str[0] == '+')
-	{
+    {
         str++;
-		len --;
-	}
+        len --;
+    }
     else if (str[0] == '-') {
         str++;
         literalsign = 1;
-		len --;
+        len --;
     }
     if (tolower(str[0]) == 'n' &&
         tolower(str[1]) == 'a' &&
@@ -3224,24 +3231,24 @@
         return NULL;
     for (i = 0; i < size; i++)
         new->digits[i] = *(start+i) -48;
-	for(i=0;i<new->limb_count;i++)
-		new->limbs[i] = 0;
+    for(i=0;i<new->limb_count;i++)
+        new->limbs[i] = 0;
 
-	long mult = 1;
-	long limb = 0;
-	for(i = size -1; i>=0; i--)
-	{
-		assert(limb < new->limb_count);
-		new->limbs[limb] += mult * (start[i] - '0');
-		mult *= 10;
-		if(mult == BASE)
-		{
-			mult = 1;
-			limb ++;
-		}
-	}
+    long mult = 1;
+    long limb = 0;
+    for(i = size -1; i>=0; i--)
+    {
+        assert(limb < new->limb_count);
+        new->limbs[limb] += mult * (start[i] - '0');
+        mult *= 10;
+        if(mult == BASE)
+        {
+            mult = 1;
+            limb ++;
+        }
+    }
 
-	return new;
+    return new;
 }
 
 
@@ -3273,7 +3280,7 @@
         if (!new)
             return NULL;
         new->digits[0] = 0;
-		new->limbs[0] = 0;
+        new->limbs[0] = 0;
         return new;
     }
     return NULL;
@@ -3283,151 +3290,151 @@
 static decimalobject *
 _decimal_fromliteral(PyTypeObject *type, char *str, long len, contextobject *ctx)
 {
-	char sign = 0;	
-	long exp = 0;
-	long ndigits;
-	long zero = 0;				/* if zero */
-	long digits_after_dot = 0;
-	char *p = str;
-	char *first_digit = 0;
-	char *last_digit = 0;
-	char *dot = 0;
-	char *e = 0;
-	int any_digit = 0;
-	char *c;
-	long i;
-	decimalobject *new;
-
-	if(!len)
-		goto err;	/* empty string */
-	
-	if(*p == '+')
-		p++;
-	else if(*p == '-')
-	{
-		p++;
-		sign = 1;
-	}
-
-	if(*p == 'e' || *p == 'E')	/* no digits before E */
-		goto err;
-
-	/* now at *p is our integer with (possibly) dot */
-
-	for(c = p; c - str < len; c++)
-	{
-			if(!isdigit(*c))
-			{
-				if(*c == 'e' || *c == 'E')
-				{
-						if(e)
-								goto err;	/* we should have only one e */
-						e = c;
-				}
-
-				else if(*c == '.')
-				{
-					if(dot || e)
-						goto err;
-					dot = c;
-				}
-
-				else if(*c == '-' || *c == '+')	/* we should have sign only after e */
-				{
-					if(c - e != 1)
-						goto err;
-				}
-				else				/* we shouldn't have anything else */
-					goto err;		
-			}
-			else
-			{
-				if(!e)
-					any_digit = 1;
-				if(dot && !e)
-					digits_after_dot ++;
-				
-				if(!first_digit && *c != '0')
-				{
-					if(!e)				/* it's digit of exponent */
-						first_digit = c;
-				}
-			}
-	}		/* we now are pretty sure, that string is properly formated */
-	if(!any_digit)
-		goto err;
-	if(!first_digit)
-	{
-		zero = 1;
-		ndigits = 1;
-	}
-	else		/* let's count digits and find by the way last digit*/
-	{
-		ndigits = 0;
-		for(c = first_digit; c - str < len; c++)
-		{
-			if(isdigit(*c))
-			{
-				ndigits ++;
-				last_digit = c;
-			}
-			else
-				if(*c == 'e' || *c == 'E')
-					break;
-			
-			assert(isdigit(*c) || *c == '.');
-		}
-	}
-
-	if(!ndigits)
-	{
-		goto err;
-	}
-	
-	if(e)			/* pretty obvious */
-	{
-		int ok;
-		ok = sscanf(e+1,"%d", &exp);
-		if(ok!=1)
-		{
-			goto err;
-		}
-	}
-	exp -= digits_after_dot;
-
-	new = _new_decimalobj(type, ndigits, sign, exp);
-	if(!new)
-		return NULL;
-
-	for(i = 0; i<new->limb_count ;i++)
-		new->limbs[i] = 0;
-
-	if(!first_digit)	/* we are 0 */
-	{
-		new->limbs[0] = 0;
-		return new;
-	}
-	
-	long mult = 1;	/* now we just read integer till '\0' or 'e'/'E', dont care about '.' */
-	long limb = 0;
-
-	for(c = last_digit; c >= first_digit; c--)
-	{
-		if(*c == 'e' || *c == 'E') break;
-		if(!isdigit(*c)) continue;
-
-		assert(limb < new->limb_count);
-		new->limbs[limb] += mult * (*c - '0');
-
-		mult *= 10;
-		if(mult == BASE)
-		{
-				limb ++;
-				mult = 1;
-		}
-	}
-	
-	return new;
+    char sign = 0;    
+    long exp = 0;
+    long ndigits;
+    long zero = 0;                /* if zero */
+    long digits_after_dot = 0;
+    char *p = str;
+    char *first_digit = 0;
+    char *last_digit = 0;
+    char *dot = 0;
+    char *e = 0;
+    int any_digit = 0;
+    char *c;
+    long i;
+    decimalobject *new;
+
+    if(!len)
+        goto err;    /* empty string */
+    
+    if(*p == '+')
+        p++;
+    else if(*p == '-')
+    {
+        p++;
+        sign = 1;
+    }
+
+    if(*p == 'e' || *p == 'E')    /* no digits before E */
+        goto err;
+
+    /* now at *p is our integer with (possibly) dot */
+
+    for(c = p; c - str < len; c++)
+    {
+        if(!isdigit(*c))
+        {
+            if(*c == 'e' || *c == 'E')
+            {
+                if(e)
+                    goto err;    /* we should have only one e */
+                e = c;
+            }
+
+            else if(*c == '.')
+            {
+                if(dot || e)
+                    goto err;
+                dot = c;
+            }
+
+            else if(*c == '-' || *c == '+')    /* we should have sign only after e */
+            {
+                if(c - e != 1)
+                    goto err;
+            }
+            else                /* we shouldn't have anything else */
+                goto err;        
+        }
+        else
+        {
+            if(!e)
+                any_digit = 1;
+            if(dot && !e)
+                digits_after_dot ++;
+            
+            if(!first_digit && *c != '0')
+            {
+                if(!e)                /* it's digit of exponent */
+                    first_digit = c;
+            }
+        }
+    }        /* we now are pretty sure, that string is properly formated */
+    if(!any_digit)
+        goto err;
+    if(!first_digit)
+    {
+        zero = 1;
+        ndigits = 1;
+    }
+    else        /* let's count digits and find by the way last digit*/
+    {
+        ndigits = 0;
+        for(c = first_digit; c - str < len; c++)
+        {
+            if(isdigit(*c))
+            {
+                ndigits ++;
+                last_digit = c;
+            }
+            else
+                if(*c == 'e' || *c == 'E')
+                    break;
+            
+            assert(isdigit(*c) || *c == '.');
+        }
+    }
+
+    if(!ndigits)
+    {
+        goto err;
+    }
+    
+    if(e)            /* pretty obvious */
+    {
+        int ok;
+        ok = sscanf(e+1,"%d", &exp);
+        if(ok!=1)
+        {
+            goto err;
+        }
+    }
+    exp -= digits_after_dot;
+
+    new = _new_decimalobj(type, ndigits, sign, exp);
+    if(!new)
+        return NULL;
+
+    for(i = 0; i<new->limb_count ;i++)
+        new->limbs[i] = 0;
+
+    if(!first_digit)    /* we are 0 */
+    {
+        new->limbs[0] = 0;
+        return new;
+    }
+    
+    long mult = 1;    /* now we just read integer till '\0' or 'e'/'E', dont care about '.' */
+    long limb = 0;
+
+    for(c = last_digit; c >= first_digit; c--)
+    {
+        if(*c == 'e' || *c == 'E') break;
+        if(!isdigit(*c)) continue;
+
+        assert(limb < new->limb_count);
+        new->limbs[limb] += mult * (*c - '0');
+
+        mult *= 10;
+        if(mult == BASE)
+        {
+            limb ++;
+            mult = 1;
+        }
+    }
+    
+    return new;
 
   err:
     return handle_ConversionSyntax(type, ctx, "invalid literal for Decimal");
@@ -3469,8 +3476,8 @@
     if (value == 0) {
         new = _new_decimalobj(type, 1, 0, 0);
         if (!new) return NULL;
-        new->digits[0] = 0;	
-		new->limbs[0] = 0;
+        new->digits[0] = 0;    
+        new->limbs[0] = 0;
         return (PyObject *)new;
     } else if (value < 0) {
         value = -value;
@@ -3483,8 +3490,8 @@
         v /= 10;
     }
     
-	v = value;
-	
+    v = value;
+    
     new = _new_decimalobj(type, ndigits, (neg ? SIGN_NEG : SIGN_POS), 0);
     if (!new) return NULL;
     while (value) {
@@ -3493,12 +3500,12 @@
         i++;
     }
 
-	for(i=0; i < new->limb_count; i++)
-	{
-			new->limbs[i] = v % BASE;
-			v /= BASE;
-	}
-	
+    for(i=0; i < new->limb_count; i++)
+    {
+        new->limbs[i] = v % BASE;
+        v /= BASE;
+    }
+    
     return (PyObject *)new;
 }
 
@@ -3546,7 +3553,7 @@
     new = _new_decimalobj(type, PyTuple_GET_SIZE(digtup), sign, exp);
     for (i = 0; i < new->ob_size; i++) {
         item = PyTuple_GET_ITEM(digtup, i);
-		long x;
+        long x;
         if (PyInt_Check(item)) {
             x = PyInt_AsLong(item);
         } else if (PyLong_Check(item)) {
@@ -3558,55 +3565,55 @@
                             "must be composed of non  negative integer elements.");
             goto err;
         }
-		
-		if(x < 0 || x > 9)
-		{
-				PyErr_Format(PyExc_ValueError, "Invalid digit: %ld", x);
-				goto err;
-		}
-
-		new->digits[i] = x;
-    }			/* this loop will go out soon XXX */
-	
-	for(i = 0;i < new->limb_count;i++)
-			new->limbs[i] = 0;
-	
-	long mult = 1;
-	long limb = 0;
-	new->limbs[0] = 0;
-	for(i = new->ob_size-1; i>=0; i--)	/* limb[0] keeps least significant limb */
-	{
-		item = PyTuple_GET_ITEM(digtup, i);
-		long x;
-		if(PyInt_Check(item))
-			x = PyInt_AsLong(item);
-		else if (PyLong_Check(item)) {
-			x = PyLong_AsLong(item);
-			if(x == -1 && PyErr_Occurred())
-					goto err;
-		}
-		else {
-			PyErr_SetString(PyExc_ValueError, "The second value in the tuple "
-							"must be composed of non negative integer elements.");
-			goto err;
-		}
-
-		if(x < 0 || x > 9)
-		{
-				PyErr_Format(PyExc_ValueError, "Invalid digit: %ld", x);
-				goto err;
-		}
-		
-		assert(limb < new->limb_count);
-		new->limbs[limb] += mult * x;
-		mult *= 10;
-		
-		if(mult == BASE)	/* we already used LOG digits of one limb */
-		{
-				mult = 1;
-				limb ++;
-		}
-	}
+        
+        if(x < 0 || x > 9)
+        {
+            PyErr_Format(PyExc_ValueError, "Invalid digit: %ld", x);
+            goto err;
+        }
+
+        new->digits[i] = x;
+    }            /* this loop will go out soon XXX */
+    
+    for(i = 0;i < new->limb_count;i++)
+        new->limbs[i] = 0;
+    
+    long mult = 1;
+    long limb = 0;
+    new->limbs[0] = 0;
+    for(i = new->ob_size-1; i>=0; i--)    /* limb[0] keeps least significant limb */
+    {
+        item = PyTuple_GET_ITEM(digtup, i);
+        long x;
+        if(PyInt_Check(item))
+            x = PyInt_AsLong(item);
+        else if (PyLong_Check(item)) {
+            x = PyLong_AsLong(item);
+            if(x == -1 && PyErr_Occurred())
+                goto err;
+        }
+        else {
+            PyErr_SetString(PyExc_ValueError, "The second value in the tuple "
+                            "must be composed of non negative integer elements.");
+            goto err;
+        }
+
+        if(x < 0 || x > 9)
+        {
+            PyErr_Format(PyExc_ValueError, "Invalid digit: %ld", x);
+            goto err;
+        }
+        
+        assert(limb < new->limb_count);
+        new->limbs[limb] += mult * x;
+        mult *= 10;
+        
+        if(mult == BASE)    /* we already used LOG digits of one limb */
+        {
+            mult = 1;
+            limb ++;
+        }
+    }
 
     Py_DECREF(digtup);
     Py_DECREF(tup);
@@ -3671,16 +3678,16 @@
         decimalobject *new = _new_decimalobj(type, 1, 0, 0);
         if (!new) return NULL;
         new->digits[0] = 0;
-		new->limbs[0] = 0;
+        new->limbs[0] = 0;
         return (PyObject *)new;
     }
 
-	if(context)
-		if(!PyDecimalContext_Check(context))
-		{
-			PyErr_SetString(PyExc_TypeError, "context must be Context type");
-			return NULL;
-		}
+    if(context)
+        if(!PyDecimalContext_Check(context))
+        {
+            PyErr_SetString(PyExc_TypeError, "context must be Context type");
+            return NULL;
+        }
     if (PyDecimal_Check(value))
         return (PyObject *)_decimal_get_copy((decimalobject *)value);
 
@@ -3700,10 +3707,10 @@
         return decimal_from_long(type, x);
     }
 
-	if(context)
-		ctx = (contextobject*) context;
-	else
-	    ctx = getcontext();
+    if(context)
+        ctx = (contextobject*) context;
+    else
+        ctx = getcontext();
 
     if (!ctx) return NULL;
 
@@ -3805,7 +3812,7 @@
 static PyMemberDef _decimal_members[] = {
     {"_sign", T_INT, offsetof(decimalobject, sign), 0},
     {"_exp", T_LONG, offsetof(decimalobject, exp), 0},
-	{"_size", T_LONG, offsetof(decimalobject, ob_size), 0},
+    {"_size", T_LONG, offsetof(decimalobject, ob_size), 0},
     {NULL}
 };
 
@@ -3818,7 +3825,7 @@
     tup = PyTuple_New(self->ob_size);
     if (!tup) return NULL;
     for (i = 0; i < self->ob_size; i++)
-        PyTuple_SET_ITEM(tup, i, PyInt_FromLong(_limb_get_digit(self->limbs, self->ob_size, i)));	/* SLOW */
+        PyTuple_SET_ITEM(tup, i, PyInt_FromLong(_limb_get_digit(self->limbs, self->ob_size, i)));    /* SLOW */
     return tup;
 }
 
@@ -3864,13 +3871,13 @@
 static PyObject *
 _decimal_get_limbs(decimalobject *self)
 {
-	PyObject *tup;
-	long i;
-	tup = PyTuple_New(self->limb_count);
-	if(!tup) return NULL;
-	for(i = 0; i< self->limb_count; i++)
-		PyTuple_SET_ITEM(tup, i, PyInt_FromLong(self->limbs[i]));
-	return tup;
+    PyObject *tup;
+    long i;
+    tup = PyTuple_New(self->limb_count);
+    if(!tup) return NULL;
+    for(i = 0; i< self->limb_count; i++)
+        PyTuple_SET_ITEM(tup, i, PyInt_FromLong(self->limbs[i]));
+    return tup;
 }
 
 static PyObject *
@@ -3884,7 +3891,7 @@
 
 
 static PyGetSetDef _decimal_getset[] = {
-	{"_limbs", (getter)_decimal_get_limbs, 0},
+    {"_limbs", (getter)_decimal_get_limbs, 0},
     {"_int", (getter)_decimal_get_int, (setter)_decimal_set_int},
     {"_is_special", (getter)_decimal_is_special, 0},
     {NULL}
@@ -4068,7 +4075,7 @@
         t = traps;
         Py_INCREF(t);
     }
-    if (!t) goto err;    
+    if (!t) goto err;
     
     if (!ignored) {
         i = PyDict_New();
@@ -4104,11 +4111,11 @@
 static PyObject *
 context_clear_flags(contextobject *self)
 {
-	int i;
-	for(i = 0;i<NUMSIGNALS;i++)
-		_set_flag(self->flags,i,0);
-	
-	Py_RETURN_NONE;
+    int i;
+    for(i = 0;i<NUMSIGNALS;i++)
+        _set_flag(self->flags,i,0);
+    
+    Py_RETURN_NONE;
 }
 
 
@@ -4251,43 +4258,43 @@
 static PyObject *
 context_apply(contextobject *self, PyObject *args, PyObject *kwds)
 {
-	static char *kwlist[] = {"a", 0};
-	decimalobject *a;
+    static char *kwlist[] = {"a", 0};
+    decimalobject *a;
 
-	if(!PyArg_ParseTupleAndKeywords(args, kwds, "O:_apply", kwlist, &a))
-		return NULL;
+    if(!PyArg_ParseTupleAndKeywords(args, kwds, "O:_apply", kwlist, &a))
+        return NULL;
 
-	if(!PyDecimal_Check(a))
-	{
-		PyErr_SetString(PyExc_ValueError, "a must be Decimal object");
-		return NULL;
-	}
-	
-	decimalobject *tmp = _decimal_fix(a, self);
-	if(!tmp)
-		return NULL;
+    if(!PyDecimal_Check(a))
+    {
+        PyErr_SetString(PyExc_ValueError, "a must be Decimal object");
+        return NULL;
+    }
+    
+    decimalobject *tmp = _decimal_fix(a, self);
+    if(!tmp)
+        return NULL;
 
-	PyObject *ret = decimal_str(tmp);
-	Py_DECREF(tmp);
+    PyObject *ret = decimal_str(tmp);
+    Py_DECREF(tmp);
 
-	return ret;
-	
+    return ret;
+    
 }
-	
+    
 static PyObject *
 context_power(contextobject *self, PyObject *args)
 {               
-    PyObject *a, *b, *c;                                                
-    decimalobject *dec_a = NULL, *dec_b = NULL, *dec_c = NULL, *res;               
+    PyObject *a, *b, *c;
+    decimalobject *dec_a = NULL, *dec_b = NULL, *dec_c = NULL, *res;
     if (!PyArg_ParseTuple(args, "OO|O:power", &a, &b, &c))
         return NULL;          
-    dec_a = (decimalobject *)_convert_to_decimal(                   
-        &PyDecimal_DecimalType, a, self, 1);                        
+    dec_a = (decimalobject *)_convert_to_decimal(
+        &PyDecimal_DecimalType, a, self, 1);
     if (dec_a == NULL)
-        return NULL;                                 
+        return NULL;
 
-    dec_b = (decimalobject *)_convert_to_decimal(                   
-        &PyDecimal_DecimalType, b, self, 1);                        
+    dec_b = (decimalobject *)_convert_to_decimal(
+        &PyDecimal_DecimalType, b, self, 1);
     if (dec_b == NULL) {
         Py_DECREF(dec_a);
         return NULL;
@@ -4300,11 +4307,11 @@
         Py_DECREF(dec_b);
         return NULL;
     }
-    res = _do_decimal_power(dec_a, dec_b, dec_c, self);                
-    Py_DECREF(dec_a);                                               
+    res = _do_decimal_power(dec_a, dec_b, dec_c, self);
+    Py_DECREF(dec_a);
     Py_DECREF(dec_b);
     Py_DECREF(dec_c);
-    return (PyObject *)res;                                                     
+    return (PyObject *)res;
 }
 
 
@@ -4365,7 +4372,7 @@
 static PyObject *
 context_to_integral(contextobject *self, PyObject *args, PyObject *kwds)
 {
-	static char *kwlist[] = {"a", 0};
+    static char *kwlist[] = {"a", 0};
     PyObject *a, *res;
     decimalobject *dec_a = NULL;
 
@@ -4388,7 +4395,7 @@
 {
     PyObject *a, *res;
     decimalobject *dec_a = NULL;
-	static char *kwlist[] = {"a", 0};
+    static char *kwlist[] = {"a", 0};
 
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O:to_sci_string", kwlist, &a))
         return NULL;
@@ -4693,8 +4700,8 @@
 /* XXX: all that PyCFunction casting might not be necessary */
 
 static PyMethodDef context_methods[] = {
-	{"_apply",			(PyCFunction)context_apply,
-	 METH_VARARGS | METH_KEYWORDS},
+    {"_apply",            (PyCFunction)context_apply,
+     METH_VARARGS | METH_KEYWORDS},
     {"clear_flags",     (PyCFunction)context_clear_flags,
      METH_NOARGS},
     {"copy",            (PyCFunction)context_copy,
@@ -4926,10 +4933,10 @@
     _flags = PyDict_New();
     if (!_flags) goto err;
 
-	for(i=0; i < NUMSIGNALS; i++)	/* XXX don't know if it's ok! */
-	{
-		_set_flag(_flags,i,0);
-	}
+    for(i=0; i < NUMSIGNALS; i++)    /* XXX don't know if it's ok! */
+    {
+        _set_flag(_flags,i,0);
+    }
 
     if (pyflags == NULL) {
         /* don't copy flags from default context */
@@ -5001,8 +5008,8 @@
         capitals = capitals & 1;
 
 /*    if (rounding_dec == -1) */
-	if(rounding_dec != NEVER_ROUND && rounding_dec != ALWAYS_ROUND)	/* XXX????*/
-		rounding_dec = PyDecimal_DefaultContext->rounding_dec;
+    if(rounding_dec != NEVER_ROUND && rounding_dec != ALWAYS_ROUND)    /* XXX????*/
+        rounding_dec = PyDecimal_DefaultContext->rounding_dec;
 
     if (rounding_dec != NEVER_ROUND && rounding_dec != ALWAYS_ROUND) {
         PyErr_SetString(PyExc_ValueError, "_rounding_decision must be one of "
@@ -5137,7 +5144,7 @@
 static PyMethodDef module_methods[] = {
     {"getcontext", (PyCFunction)module_getcontext, METH_NOARGS},
     {"setcontext", (PyCFunction)module_setcontext, METH_VARARGS | METH_KEYWORDS},
-	{NULL, NULL}
+    {NULL, NULL}
 };
 
 
@@ -5166,7 +5173,7 @@
 PyMODINIT_FUNC
 INITFUNC_NAME(void)
 {
-    PyObject *m;	/* a module object */
+    PyObject *m;    /* a module object */
     PyObject *tup;
     contextobject *ctx;
     PyObject *traps;


More information about the Python-checkins mailing list