13 #define Max(x, y) ((x) > (y) ? (x) : (y))
14 #define Min(x, y) ((x) < (y) ? (x) : (y))
16 #define init_var(v) memset(v,0,sizeof(numeric))
18 #define digitbuf_alloc(size) ((NumericDigit *) pgtypes_alloc(size))
19 #define digitbuf_free(buf) free(buf)
66 memset(var, 0,
sizeof(
decimal));
87 if (!isspace((
unsigned char) *(*ptr)))
100 if (!isspace((
unsigned char) *(*ptr)))
136 if (!isdigit((
unsigned char) *(*ptr)))
144 if (isdigit((
unsigned char) *(*ptr)))
146 dest->digits[
i++] = *(*ptr)++ -
'0';
152 else if (*(*ptr) ==
'.')
168 if (*(*ptr) ==
'e' || *(*ptr) ==
'E')
174 exponent = strtol(*ptr, &endptr, 10);
175 if (endptr == (*ptr))
181 if (exponent >= INT_MAX / 2 || exponent <= -(INT_MAX / 2))
186 dest->weight += (int) exponent;
187 dest->dscale -= (int) exponent;
188 if (
dest->dscale < 0)
195 if (!isspace((
unsigned char) *(*ptr)))
204 while (
dest->ndigits > 0 && *(
dest->digits) == 0)
210 if (
dest->ndigits == 0)
248 int carry = (var->
digits[
i] > 4) ? 1 : 0;
290 if (i <= var->weight && d < var->ndigits)
291 *cp++ = var->
digits[d++] +
'0';
305 if (i <= var->weight && d < var->ndigits)
306 *cp++ = var->
digits[d++] +
'0';
327 char **ptr = (endptr != NULL) ? endptr : &realptr;
415 while (w1 > w2 && i1 < var1->ndigits)
417 if (var1->
digits[i1++] != 0)
421 while (w2 > w1 && i2 < var2->ndigits)
423 if (var2->
digits[i2++] != 0)
430 while (i1 < var1->ndigits && i2 < var2->ndigits)
442 while (i1 < var1->ndigits)
444 if (var1->
digits[i1++] != 0)
447 while (i2 < var2->ndigits)
449 if (var2->
digits[i2++] != 0)
479 int var1ndigits = var1->
ndigits;
480 int var2ndigits = var2->
ndigits;
487 res_ndigits = res_rscale + res_weight + 1;
488 if (res_ndigits <= 0)
493 res_digits = res_buf;
495 i1 = res_rscale + var1->
weight + 1;
496 i2 = res_rscale + var2->
weight + 1;
497 for (
i = res_ndigits - 1;
i >= 0;
i--)
501 if (i1 >= 0 && i1 < var1ndigits)
502 carry += var1digits[i1];
503 if (i2 >= 0 && i2 < var2ndigits)
504 carry += var2digits[i2];
508 res_digits[
i] = carry - 10;
513 res_digits[
i] = carry;
518 while (res_ndigits > 0 && *res_digits == 0)
524 while (res_ndigits > 0 && res_digits[res_ndigits - 1] == 0)
527 if (res_ndigits == 0)
532 result->
buf = res_buf;
533 result->
digits = res_digits;
534 result->
weight = res_weight;
535 result->
rscale = res_rscale;
536 result->
dscale = res_dscale;
567 int var1ndigits = var1->
ndigits;
568 int var2ndigits = var2->
ndigits;
572 res_weight = var1->
weight;
575 res_ndigits = res_rscale + res_weight + 1;
576 if (res_ndigits <= 0)
581 res_digits = res_buf;
583 i1 = res_rscale + var1->
weight + 1;
584 i2 = res_rscale + var2->
weight + 1;
585 for (
i = res_ndigits - 1;
i >= 0;
i--)
589 if (i1 >= 0 && i1 < var1ndigits)
590 borrow += var1digits[i1];
591 if (i2 >= 0 && i2 < var2ndigits)
592 borrow -= var2digits[i2];
596 res_digits[
i] = borrow + 10;
601 res_digits[
i] = borrow;
606 while (res_ndigits > 0 && *res_digits == 0)
612 while (res_ndigits > 0 && res_digits[res_ndigits - 1] == 0)
615 if (res_ndigits == 0)
620 result->
buf = res_buf;
621 result->
digits = res_digits;
622 result->
weight = res_weight;
623 result->
rscale = res_rscale;
624 result->
dscale = res_dscale;
649 if (
add_abs(var1, var2, result) != 0)
677 if (
sub_abs(var1, var2, result) != 0)
688 if (
sub_abs(var2, var1, result) != 0)
723 if (
sub_abs(var1, var2, result) != 0)
734 if (
sub_abs(var2, var1, result) != 0)
747 if (
add_abs(var1, var2, result) != 0)
779 if (
add_abs(var1, var2, result) != 0)
809 if (
sub_abs(var1, var2, result) != 0)
820 if (
sub_abs(var2, var1, result) != 0)
855 if (
sub_abs(var1, var2, result) != 0)
866 if (
sub_abs(var2, var1, result) != 0)
879 if (
add_abs(var1, var2, result) != 0)
919 res_digits = res_buf;
920 memset(res_digits, 0, res_ndigits);
923 for (i1 = var1->
ndigits - 1; i1 >= 0; i1--)
928 for (i2 = var2->
ndigits - 1; i2 >= 0; i2--)
931 res_digits[
i--] = sum % 10;
937 i = res_weight + global_rscale + 2;
938 if (
i >= 0 &&
i < res_ndigits)
940 sum = (res_digits[
i] > 4) ? 1 : 0;
945 sum += res_digits[
i];
946 res_digits[
i--] = sum % 10;
951 while (res_ndigits > 0 && *res_digits == 0)
957 while (res_ndigits > 0 && res_digits[res_ndigits - 1] == 0)
960 if (res_ndigits == 0)
967 result->
buf = res_buf;
968 result->
digits = res_digits;
970 result->
weight = res_weight;
971 result->
rscale = global_rscale;
972 result->
sign = res_sign;
1011 firstdigit1 = var1->
digits[
i];
1012 if (firstdigit1 != 0)
1023 firstdigit2 = var2->
digits[
i];
1024 if (firstdigit2 != 0)
1035 qweight = weight1 - weight2;
1036 if (firstdigit1 <= firstdigit2)
1041 res_dscale =
Max(res_dscale, var1->
dscale);
1042 res_dscale =
Max(res_dscale, var2->
dscale);
1047 *rscale = res_dscale + 4;
1068 int first_nextdigit;
1078 ndigits_tmp = var2->
ndigits + 1;
1079 if (ndigits_tmp == 1)
1093 res_ndigits = rscale + res_weight;
1094 if (res_ndigits <= 0)
1111 for (
int i = 1;
i < 10;
i++)
1117 divisor[1].
ndigits = ndigits_tmp;
1121 if (divisor[1].
buf == NULL)
1124 divisor[1].
digits[0] = 0;
1125 memcpy(&(divisor[1].
digits[1]), var2->
digits, ndigits_tmp - 1);
1135 if (dividend.
buf == NULL)
1146 if (tmp_buf == NULL)
1149 result->
buf = tmp_buf;
1150 res_digits = result->
buf;
1151 result->
digits = res_digits;
1152 result->
ndigits = res_ndigits;
1153 result->
weight = res_weight;
1155 result->
sign = res_sign;
1158 first_div = divisor[1].
digits[1] * 10;
1159 if (ndigits_tmp > 2)
1160 first_div += divisor[1].
digits[2];
1163 first_nextdigit = 0;
1166 rscale_tmp = divisor[1].
rscale;
1168 for (ri = 0; ri <= res_ndigits; ri++)
1170 first_have = first_have * 10;
1171 if (first_nextdigit >= 0 && first_nextdigit < dividend.
ndigits)
1172 first_have += dividend.
digits[first_nextdigit];
1175 guess = (first_have * 10) / first_div + 1;
1181 if (divisor[guess].
buf == NULL)
1186 memcpy(&divisor[guess], &divisor[1],
sizeof(
numeric));
1188 if (divisor[guess].
buf == NULL)
1190 divisor[guess].
digits = divisor[guess].
buf;
1191 for (
i = divisor[1].ndigits - 1;
i >= 0;
i--)
1193 sum += divisor[1].
digits[
i] * guess;
1194 divisor[guess].
digits[
i] = sum % 10;
1199 divisor[guess].
weight = weight_tmp;
1200 divisor[guess].
rscale = rscale_tmp;
1209 res_digits[ri + 1] = guess;
1222 if (
sub_abs(÷nd, &divisor[guess], ÷nd) != 0)
1225 first_nextdigit = dividend.
weight - weight_tmp;
1227 if (first_nextdigit >= 0 && first_nextdigit < dividend.
ndigits)
1228 first_have = dividend.
digits[first_nextdigit];
1233 if (ri == res_ndigits + 1)
1235 int carry = (res_digits[ri] > 4) ? 1 : 0;
1240 while (carry && ri > 0)
1242 carry += res_digits[--ri];
1243 res_digits[ri] = carry % 10;
1259 result->
dscale = res_dscale;
1267 if (dividend.
buf != NULL)
1270 for (
int i = 1;
i < 10;
i++)
1272 if (divisor[
i].
buf != NULL)
1312 signed long int long_int = int_val;
1330 signed long int abs_long_val = long_val;
1331 signed long int extract;
1332 signed long int reach_limit;
1334 if (abs_long_val < 0)
1347 }
while (reach_limit - 1 < abs_long_val && reach_limit <= LONG_MAX / 10);
1349 if (reach_limit > LONG_MAX / 10)
1371 extract = abs_long_val - (abs_long_val % reach_limit);
1372 var->
digits[
i] = extract / reach_limit;
1373 abs_long_val -= extract;
1382 }
while (abs_long_val > 0);
1413 char buffer[DBL_DIG + 100];
1417 if (
sprintf(buffer,
"%.*g", DBL_DIG, d) <= 0)
1439 if (varcopy == NULL)
1458 val = strtod(tmp, &endptr);
1459 if (errno == ERANGE)
1470 if (*endptr !=
'\0')
1503 #if SIZEOF_LONG > SIZEOF_INT
1505 if (l < INT_MIN || l > INT_MAX)
1527 *lp = strtol(s, &endptr, 10);
1535 if (errno == ERANGE)
1537 if (*lp == LONG_MIN)
void err(int eval, const char *fmt,...)
char * pgtypes_alloc(long size)
int PGTYPESnumeric_from_double(double d, numeric *dst)
#define digitbuf_free(buf)
int PGTYPESnumeric_copy(numeric *src, numeric *dst)
#define digitbuf_alloc(size)
static int set_var_from_str(char *str, char **ptr, numeric *dest)
int PGTYPESnumeric_from_decimal(decimal *src, numeric *dst)
int PGTYPESnumeric_to_decimal(numeric *src, decimal *dst)
void PGTYPESdecimal_free(decimal *var)
int PGTYPESnumeric_mul(numeric *var1, numeric *var2, numeric *result)
int PGTYPESnumeric_to_long(numeric *nv, long *lp)
static int numericvar_to_double(numeric *var, double *dp)
int PGTYPESnumeric_to_double(numeric *nv, double *dp)
int PGTYPESnumeric_from_long(signed long int long_val, numeric *var)
int PGTYPESnumeric_to_int(numeric *nv, int *ip)
static char * get_str_from_var(numeric *var, int dscale)
char * PGTYPESnumeric_to_asc(numeric *num, int dscale)
numeric * PGTYPESnumeric_new(void)
int PGTYPESnumeric_from_int(signed int int_val, numeric *var)
static int alloc_var(numeric *var, int ndigits)
static int select_div_scale(numeric *var1, numeric *var2, int *rscale)
int PGTYPESnumeric_sub(numeric *var1, numeric *var2, numeric *result)
decimal * PGTYPESdecimal_new(void)
static int sub_abs(numeric *var1, numeric *var2, numeric *result)
static int add_abs(numeric *var1, numeric *var2, numeric *result)
static void zero_var(numeric *var)
numeric * PGTYPESnumeric_from_asc(char *str, char **endptr)
void PGTYPESnumeric_free(numeric *var)
int PGTYPESnumeric_cmp(numeric *var1, numeric *var2)
int PGTYPESnumeric_div(numeric *var1, numeric *var2, numeric *result)
int PGTYPESnumeric_add(numeric *var1, numeric *var2, numeric *result)
static int cmp_abs(numeric *var1, numeric *var2)
#define NUMERIC_MAX_DISPLAY_SCALE
#define NUMERIC_MIN_SIG_DIGITS
#define NUMERIC_MIN_DISPLAY_SCALE
#define PGTYPES_NUM_BAD_NUMERIC
#define PGTYPES_NUM_OVERFLOW
#define PGTYPES_NUM_UNDERFLOW
#define PGTYPES_NUM_DIVIDE_ZERO
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
static pg_noinline void Size size
NumericDigit digits[DECSIZE]