diff options
Diffstat (limited to 'src/backend/utils/adt/numeric.c')
-rw-r--r-- | src/backend/utils/adt/numeric.c | 92 |
1 files changed, 48 insertions, 44 deletions
diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index 0fbca231756..ba3721b12b3 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -14,7 +14,7 @@ * Copyright (c) 1998-2009, PostgreSQL Global Development Group * * IDENTIFICATION - * $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.117 2009/04/08 22:08:40 tgl Exp $ + * $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.118 2009/06/11 14:49:03 momjian Exp $ * *------------------------------------------------------------------------- */ @@ -90,7 +90,7 @@ typedef int16 NumericDigit; /* ---------- - * NumericVar is the format we use for arithmetic. The digit-array part + * NumericVar is the format we use for arithmetic. The digit-array part * is the same as the NumericData storage format, but the header is more * complex. * @@ -243,7 +243,7 @@ static void free_var(NumericVar *var); static void zero_var(NumericVar *var); static const char *set_var_from_str(const char *str, const char *cp, - NumericVar *dest); + NumericVar *dest); static void set_var_from_num(Numeric value, NumericVar *dest); static void set_var_from_var(NumericVar *value, NumericVar *dest); static char *get_str_from_var(NumericVar *var, int dscale); @@ -271,7 +271,7 @@ static void mul_var(NumericVar *var1, NumericVar *var2, NumericVar *result, static void div_var(NumericVar *var1, NumericVar *var2, NumericVar *result, int rscale, bool round); static void div_var_fast(NumericVar *var1, NumericVar *var2, NumericVar *result, - int rscale, bool round); + int rscale, bool round); static int select_div_scale(NumericVar *var1, NumericVar *var2); static void mod_var(NumericVar *var1, NumericVar *var2, NumericVar *result); static void ceil_var(NumericVar *var, NumericVar *result); @@ -348,8 +348,8 @@ numeric_in(PG_FUNCTION_ARGS) if (!isspace((unsigned char) *cp)) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), - errmsg("invalid input syntax for type numeric: \"%s\"", - str))); + errmsg("invalid input syntax for type numeric: \"%s\"", + str))); cp++; } } @@ -367,16 +367,16 @@ numeric_in(PG_FUNCTION_ARGS) /* * We duplicate a few lines of code here because we would like to * throw any trailing-junk syntax error before any semantic error - * resulting from apply_typmod. We can't easily fold the two - * cases together because we mustn't apply apply_typmod to a NaN. + * resulting from apply_typmod. We can't easily fold the two cases + * together because we mustn't apply apply_typmod to a NaN. */ while (*cp) { if (!isspace((unsigned char) *cp)) ereport(ERROR, (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION), - errmsg("invalid input syntax for type numeric: \"%s\"", - str))); + errmsg("invalid input syntax for type numeric: \"%s\"", + str))); cp++; } @@ -1938,8 +1938,8 @@ numeric_power(PG_FUNCTION_ARGS) /* * The SQL spec requires that we emit a particular SQLSTATE error code for - * certain error conditions. Specifically, we don't return a divide-by-zero - * error code for 0 ^ -1. + * certain error conditions. Specifically, we don't return a + * divide-by-zero error code for 0 ^ -1. */ if (cmp_var(&arg1, &const_zero) == 0 && cmp_var(&arg2, &const_zero) < 0) @@ -2654,9 +2654,9 @@ int2_sum(PG_FUNCTION_ARGS) /* * If we're invoked by nodeAgg, we can cheat and modify our first * parameter in-place to avoid palloc overhead. If not, we need to return - * the new value of the transition variable. - * (If int8 is pass-by-value, then of course this is useless as well - * as incorrect, so just ifdef it out.) + * the new value of the transition variable. (If int8 is pass-by-value, + * then of course this is useless as well as incorrect, so just ifdef it + * out.) */ #ifndef USE_FLOAT8_BYVAL /* controls int8 too */ if (fcinfo->context && @@ -2705,9 +2705,9 @@ int4_sum(PG_FUNCTION_ARGS) /* * If we're invoked by nodeAgg, we can cheat and modify our first * parameter in-place to avoid palloc overhead. If not, we need to return - * the new value of the transition variable. - * (If int8 is pass-by-value, then of course this is useless as well - * as incorrect, so just ifdef it out.) + * the new value of the transition variable. (If int8 is pass-by-value, + * then of course this is useless as well as incorrect, so just ifdef it + * out.) */ #ifndef USE_FLOAT8_BYVAL /* controls int8 too */ if (fcinfo->context && @@ -4209,6 +4209,7 @@ div_var(NumericVar *var1, NumericVar *var2, NumericVar *result, /* If rounding needed, figure one more digit to ensure correct result */ if (round) res_ndigits++; + /* * The working dividend normally requires res_ndigits + var2ndigits * digits, but make it at least var1ndigits so we can load all of var1 @@ -4222,8 +4223,8 @@ div_var(NumericVar *var1, NumericVar *var2, NumericVar *result, /* * We need a workspace with room for the working dividend (div_ndigits+1 * digits) plus room for the possibly-normalized divisor (var2ndigits - * digits). It is convenient also to have a zero at divisor[0] with - * the actual divisor data in divisor[1 .. var2ndigits]. Transferring the + * digits). It is convenient also to have a zero at divisor[0] with the + * actual divisor data in divisor[1 .. var2ndigits]. Transferring the * digits into the workspace also allows us to realloc the result (which * might be the same as either input var) before we begin the main loop. * Note that we use palloc0 to ensure that divisor[0], dividend[0], and @@ -4244,8 +4245,8 @@ div_var(NumericVar *var1, NumericVar *var2, NumericVar *result, if (var2ndigits == 1) { /* - * If there's only a single divisor digit, we can use a fast path - * (cf. Knuth section 4.3.1 exercise 16). + * If there's only a single divisor digit, we can use a fast path (cf. + * Knuth section 4.3.1 exercise 16). */ divisor1 = divisor[1]; carry = 0; @@ -4264,12 +4265,12 @@ div_var(NumericVar *var1, NumericVar *var2, NumericVar *result, * * We need the first divisor digit to be >= NBASE/2. If it isn't, * make it so by scaling up both the divisor and dividend by the - * factor "d". (The reason for allocating dividend[0] above is to + * factor "d". (The reason for allocating dividend[0] above is to * leave room for possible carry here.) */ if (divisor[1] < HALF_NBASE) { - int d = NBASE / (divisor[1] + 1); + int d = NBASE / (divisor[1] + 1); carry = 0; for (i = var2ndigits; i > 0; i--) @@ -4295,22 +4296,22 @@ div_var(NumericVar *var1, NumericVar *var2, NumericVar *result, divisor2 = divisor[2]; /* - * Begin the main loop. Each iteration of this loop produces the - * j'th quotient digit by dividing dividend[j .. j + var2ndigits] - * by the divisor; this is essentially the same as the common manual + * Begin the main loop. Each iteration of this loop produces the j'th + * quotient digit by dividing dividend[j .. j + var2ndigits] by the + * divisor; this is essentially the same as the common manual * procedure for long division. */ for (j = 0; j < res_ndigits; j++) { /* Estimate quotient digit from the first two dividend digits */ - int next2digits = dividend[j] * NBASE + dividend[j+1]; - int qhat; + int next2digits = dividend[j] * NBASE + dividend[j + 1]; + int qhat; /* * If next2digits are 0, then quotient digit must be 0 and there's - * no need to adjust the working dividend. It's worth testing - * here to fall out ASAP when processing trailing zeroes in - * a dividend. + * no need to adjust the working dividend. It's worth testing + * here to fall out ASAP when processing trailing zeroes in a + * dividend. */ if (next2digits == 0) { @@ -4322,14 +4323,15 @@ div_var(NumericVar *var1, NumericVar *var2, NumericVar *result, qhat = NBASE - 1; else qhat = next2digits / divisor1; + /* * Adjust quotient digit if it's too large. Knuth proves that - * after this step, the quotient digit will be either correct - * or just one too large. (Note: it's OK to use dividend[j+2] - * here because we know the divisor length is at least 2.) + * after this step, the quotient digit will be either correct or + * just one too large. (Note: it's OK to use dividend[j+2] here + * because we know the divisor length is at least 2.) */ while (divisor2 * qhat > - (next2digits - qhat * divisor1) * NBASE + dividend[j+2]) + (next2digits - qhat * divisor1) * NBASE + dividend[j + 2]) qhat--; /* As above, need do nothing more when quotient digit is 0 */ @@ -5253,16 +5255,16 @@ power_var(NumericVar *base, NumericVar *exp, NumericVar *result) } /* - * This avoids log(0) for cases of 0 raised to a non-integer. - * 0 ^ 0 handled by power_var_int(). + * This avoids log(0) for cases of 0 raised to a non-integer. 0 ^ 0 + * handled by power_var_int(). */ if (cmp_var(base, &const_zero) == 0) { set_var_from_var(&const_zero, result); - result->dscale = NUMERIC_MIN_SIG_DIGITS; /* no need to round */ + result->dscale = NUMERIC_MIN_SIG_DIGITS; /* no need to round */ return; } - + init_var(&ln_base); init_var(&ln_num); @@ -5330,11 +5332,13 @@ power_var_int(NumericVar *base, int exp, NumericVar *result, int rscale) switch (exp) { case 0: + /* - * While 0 ^ 0 can be either 1 or indeterminate (error), we - * treat it as 1 because most programming languages do this. - * SQL:2003 also requires a return value of 1. - * http://en.wikipedia.org/wiki/Exponentiation#Zero_to_the_zero_power + * While 0 ^ 0 can be either 1 or indeterminate (error), we treat + * it as 1 because most programming languages do this. SQL:2003 + * also requires a return value of 1. + * http://en.wikipedia.org/wiki/Exponentiation#Zero_to_the_zero_pow + * er */ set_var_from_var(&const_one, result); result->dscale = rscale; /* no need to round */ |