|
|
|
@ -8,7 +8,7 @@ |
|
|
|
|
#define Max(x, y) ((x) > (y) ? (x) : (y)) |
|
|
|
|
#define Min(x, y) ((x) < (y) ? (x) : (y)) |
|
|
|
|
|
|
|
|
|
#define init_var(v) memset(v,0,sizeof(Numeric)) |
|
|
|
|
#define init_var(v) memset(v,0,sizeof(numeric)) |
|
|
|
|
|
|
|
|
|
#define digitbuf_alloc(size) ((NumericDigit *) pgtypes_alloc(size)) |
|
|
|
|
#define digitbuf_free(buf) \ |
|
|
|
@ -28,7 +28,7 @@ |
|
|
|
|
* ---------- |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
apply_typmod(Numeric *var, long typmod) |
|
|
|
|
apply_typmod(numeric *var, long typmod) |
|
|
|
|
{ |
|
|
|
|
int precision; |
|
|
|
|
int scale; |
|
|
|
@ -108,7 +108,7 @@ apply_typmod(Numeric *var, long typmod) |
|
|
|
|
* ---------- |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
alloc_var(Numeric *var, int ndigits) |
|
|
|
|
alloc_var(numeric *var, int ndigits) |
|
|
|
|
{ |
|
|
|
|
digitbuf_free(var->buf); |
|
|
|
|
var->buf = digitbuf_alloc(ndigits + 1); |
|
|
|
@ -120,12 +120,12 @@ alloc_var(Numeric *var, int ndigits) |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Numeric * |
|
|
|
|
numeric * |
|
|
|
|
PGTYPESnumeric_new(void) |
|
|
|
|
{ |
|
|
|
|
Numeric *var; |
|
|
|
|
numeric *var; |
|
|
|
|
|
|
|
|
|
if ((var = (Numeric *) pgtypes_alloc(sizeof(Numeric))) == NULL) |
|
|
|
|
if ((var = (numeric *) pgtypes_alloc(sizeof(numeric))) == NULL) |
|
|
|
|
return NULL; |
|
|
|
|
|
|
|
|
|
if (alloc_var(var, 0) < 0) |
|
|
|
@ -141,7 +141,7 @@ PGTYPESnumeric_new(void) |
|
|
|
|
* ---------- |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
set_var_from_str(char *str, char **ptr, Numeric *dest) |
|
|
|
|
set_var_from_str(char *str, char **ptr, numeric *dest) |
|
|
|
|
{ |
|
|
|
|
bool have_dp = FALSE; |
|
|
|
|
int i = 0; |
|
|
|
@ -270,7 +270,7 @@ set_var_from_str(char *str, char **ptr, Numeric *dest) |
|
|
|
|
* ---------- |
|
|
|
|
*/ |
|
|
|
|
static char * |
|
|
|
|
get_str_from_var(Numeric *var, int dscale) |
|
|
|
|
get_str_from_var(numeric *var, int dscale) |
|
|
|
|
{ |
|
|
|
|
char *str; |
|
|
|
|
char *cp; |
|
|
|
@ -356,10 +356,10 @@ get_str_from_var(Numeric *var, int dscale) |
|
|
|
|
return str; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Numeric * |
|
|
|
|
numeric * |
|
|
|
|
PGTYPESnumeric_from_asc(char *str, char **endptr) |
|
|
|
|
{ |
|
|
|
|
Numeric *value = (Numeric *) pgtypes_alloc(sizeof(Numeric)); |
|
|
|
|
numeric *value = (numeric *) pgtypes_alloc(sizeof(numeric)); |
|
|
|
|
int ret; |
|
|
|
|
|
|
|
|
|
#if 0 |
|
|
|
@ -384,9 +384,9 @@ PGTYPESnumeric_from_asc(char *str, char **endptr) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
char * |
|
|
|
|
PGTYPESnumeric_to_asc(Numeric *num, int dscale) |
|
|
|
|
PGTYPESnumeric_to_asc(numeric *num, int dscale) |
|
|
|
|
{ |
|
|
|
|
if (dscale <= 0) |
|
|
|
|
if (dscale < 0) |
|
|
|
|
dscale = num->dscale; |
|
|
|
|
|
|
|
|
|
return (get_str_from_var(num, dscale)); |
|
|
|
@ -400,7 +400,7 @@ PGTYPESnumeric_to_asc(Numeric *num, int dscale) |
|
|
|
|
* ---------- |
|
|
|
|
*/ |
|
|
|
|
static void |
|
|
|
|
zero_var(Numeric *var) |
|
|
|
|
zero_var(numeric *var) |
|
|
|
|
{ |
|
|
|
|
digitbuf_free(var->buf); |
|
|
|
|
var->buf = NULL; |
|
|
|
@ -411,7 +411,7 @@ zero_var(Numeric *var) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
PGTYPESnumeric_free(Numeric *var) |
|
|
|
|
PGTYPESnumeric_free(numeric *var) |
|
|
|
|
{ |
|
|
|
|
digitbuf_free(var->buf); |
|
|
|
|
free(var); |
|
|
|
@ -427,7 +427,7 @@ PGTYPESnumeric_free(Numeric *var) |
|
|
|
|
* ---------- |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
cmp_abs(Numeric *var1, Numeric *var2) |
|
|
|
|
cmp_abs(numeric *var1, numeric *var2) |
|
|
|
|
{ |
|
|
|
|
int i1 = 0; |
|
|
|
|
int i2 = 0; |
|
|
|
@ -485,7 +485,7 @@ cmp_abs(Numeric *var1, Numeric *var2) |
|
|
|
|
* ---------- |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
add_abs(Numeric *var1, Numeric *var2, Numeric *result) |
|
|
|
|
add_abs(numeric *var1, numeric *var2, numeric *result) |
|
|
|
|
{ |
|
|
|
|
NumericDigit *res_buf; |
|
|
|
|
NumericDigit *res_digits; |
|
|
|
@ -573,7 +573,7 @@ add_abs(Numeric *var1, Numeric *var2, Numeric *result) |
|
|
|
|
* ---------- |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
sub_abs(Numeric *var1, Numeric *var2, Numeric *result) |
|
|
|
|
sub_abs(numeric *var1, numeric *var2, numeric *result) |
|
|
|
|
{ |
|
|
|
|
NumericDigit *res_buf; |
|
|
|
|
NumericDigit *res_digits; |
|
|
|
@ -657,7 +657,7 @@ sub_abs(Numeric *var1, Numeric *var2, Numeric *result) |
|
|
|
|
* ---------- |
|
|
|
|
*/ |
|
|
|
|
int |
|
|
|
|
PGTYPESnumeric_add(Numeric *var1, Numeric *var2, Numeric *result) |
|
|
|
|
PGTYPESnumeric_add(numeric *var1, numeric *var2, numeric *result) |
|
|
|
|
{ |
|
|
|
|
/*
|
|
|
|
|
* Decide on the signs of the two variables what to do |
|
|
|
@ -786,7 +786,7 @@ PGTYPESnumeric_add(Numeric *var1, Numeric *var2, Numeric *result) |
|
|
|
|
* ---------- |
|
|
|
|
*/ |
|
|
|
|
int |
|
|
|
|
PGTYPESnumeric_sub(Numeric *var1, Numeric *var2, Numeric *result) |
|
|
|
|
PGTYPESnumeric_sub(numeric *var1, numeric *var2, numeric *result) |
|
|
|
|
{ |
|
|
|
|
/*
|
|
|
|
|
* Decide on the signs of the two variables what to do |
|
|
|
@ -917,7 +917,7 @@ PGTYPESnumeric_sub(Numeric *var1, Numeric *var2, Numeric *result) |
|
|
|
|
* ---------- |
|
|
|
|
*/ |
|
|
|
|
int |
|
|
|
|
PGTYPESnumeric_mul(Numeric *var1, Numeric *var2, Numeric *result) |
|
|
|
|
PGTYPESnumeric_mul(numeric *var1, numeric *var2, numeric *result) |
|
|
|
|
{ |
|
|
|
|
NumericDigit *res_buf; |
|
|
|
|
NumericDigit *res_digits; |
|
|
|
@ -1008,7 +1008,7 @@ PGTYPESnumeric_mul(Numeric *var1, Numeric *var2, Numeric *result) |
|
|
|
|
* Note that this must be called before div_var. |
|
|
|
|
*/ |
|
|
|
|
static int |
|
|
|
|
select_div_scale(Numeric *var1, Numeric *var2, int *rscale) |
|
|
|
|
select_div_scale(numeric *var1, numeric *var2, int *rscale) |
|
|
|
|
{ |
|
|
|
|
int weight1, |
|
|
|
|
weight2, |
|
|
|
@ -1075,14 +1075,14 @@ select_div_scale(Numeric *var1, Numeric *var2, int *rscale) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
PGTYPESnumeric_div(Numeric *var1, Numeric *var2, Numeric *result) |
|
|
|
|
PGTYPESnumeric_div(numeric *var1, numeric *var2, numeric *result) |
|
|
|
|
{ |
|
|
|
|
NumericDigit *res_digits; |
|
|
|
|
int res_ndigits; |
|
|
|
|
int res_sign; |
|
|
|
|
int res_weight; |
|
|
|
|
Numeric dividend; |
|
|
|
|
Numeric divisor[10]; |
|
|
|
|
numeric dividend; |
|
|
|
|
numeric divisor[10]; |
|
|
|
|
int ndigits_tmp; |
|
|
|
|
int weight_tmp; |
|
|
|
|
int rscale_tmp; |
|
|
|
@ -1198,7 +1198,7 @@ PGTYPESnumeric_div(Numeric *var1, Numeric *var2, Numeric *result) |
|
|
|
|
int i; |
|
|
|
|
long sum = 0; |
|
|
|
|
|
|
|
|
|
memcpy(&divisor[guess], &divisor[1], sizeof(Numeric)); |
|
|
|
|
memcpy(&divisor[guess], &divisor[1], sizeof(numeric)); |
|
|
|
|
divisor[guess].buf = digitbuf_alloc(divisor[guess].ndigits); |
|
|
|
|
divisor[guess].digits = divisor[guess].buf; |
|
|
|
|
for (i = divisor[1].ndigits - 1; i >= 0; i--) |
|
|
|
@ -1281,7 +1281,7 @@ PGTYPESnumeric_div(Numeric *var1, Numeric *var2, Numeric *result) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
PGTYPESnumeric_cmp(Numeric *var1, Numeric *var2) |
|
|
|
|
PGTYPESnumeric_cmp(numeric *var1, numeric *var2) |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
/* use cmp_abs function to calculate the result */ |
|
|
|
@ -1312,7 +1312,7 @@ PGTYPESnumeric_cmp(Numeric *var1, Numeric *var2) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
PGTYPESnumeric_from_int(signed int int_val, Numeric *var) |
|
|
|
|
PGTYPESnumeric_from_int(signed int int_val, numeric *var) |
|
|
|
|
{ |
|
|
|
|
/* implicit conversion */ |
|
|
|
|
signed long int long_int = int_val; |
|
|
|
@ -1321,7 +1321,7 @@ PGTYPESnumeric_from_int(signed int int_val, Numeric *var) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
PGTYPESnumeric_from_long(signed long int long_val, Numeric *var) |
|
|
|
|
PGTYPESnumeric_from_long(signed long int long_val, numeric *var) |
|
|
|
|
{ |
|
|
|
|
/* calculate the size of the long int number */ |
|
|
|
|
/* a number n needs log_10 n digits */ |
|
|
|
@ -1382,7 +1382,7 @@ PGTYPESnumeric_from_long(signed long int long_val, Numeric *var) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
PGTYPESnumeric_copy(Numeric *src, Numeric *dst) |
|
|
|
|
PGTYPESnumeric_copy(numeric *src, numeric *dst) |
|
|
|
|
{ |
|
|
|
|
int i; |
|
|
|
|
|
|
|
|
@ -1403,10 +1403,10 @@ PGTYPESnumeric_copy(Numeric *src, Numeric *dst) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
PGTYPESnumeric_from_double(double d, Numeric *dst) |
|
|
|
|
PGTYPESnumeric_from_double(double d, numeric *dst) |
|
|
|
|
{ |
|
|
|
|
char buffer[100]; |
|
|
|
|
Numeric *tmp; |
|
|
|
|
numeric *tmp; |
|
|
|
|
|
|
|
|
|
if (sprintf(buffer, "%f", d) == 0) |
|
|
|
|
return -1; |
|
|
|
@ -1420,7 +1420,7 @@ PGTYPESnumeric_from_double(double d, Numeric *dst) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static int |
|
|
|
|
numericvar_to_double_no_overflow(Numeric *var, double *dp) |
|
|
|
|
numericvar_to_double_no_overflow(numeric *var, double *dp) |
|
|
|
|
{ |
|
|
|
|
char *tmp; |
|
|
|
|
double val; |
|
|
|
@ -1444,7 +1444,7 @@ numericvar_to_double_no_overflow(Numeric *var, double *dp) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
PGTYPESnumeric_to_double(Numeric *nv, double *dp) |
|
|
|
|
PGTYPESnumeric_to_double(numeric *nv, double *dp) |
|
|
|
|
{ |
|
|
|
|
double tmp; |
|
|
|
|
int i; |
|
|
|
@ -1456,7 +1456,7 @@ PGTYPESnumeric_to_double(Numeric *nv, double *dp) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
PGTYPESnumeric_to_int(Numeric *nv, int *ip) |
|
|
|
|
PGTYPESnumeric_to_int(numeric *nv, int *ip) |
|
|
|
|
{ |
|
|
|
|
long l; |
|
|
|
|
int i; |
|
|
|
@ -1475,7 +1475,7 @@ PGTYPESnumeric_to_int(Numeric *nv, int *ip) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
PGTYPESnumeric_to_long(Numeric *nv, long *lp) |
|
|
|
|
PGTYPESnumeric_to_long(numeric *nv, long *lp) |
|
|
|
|
{ |
|
|
|
|
int i; |
|
|
|
|
long l = 0; |
|
|
|
@ -1503,7 +1503,7 @@ PGTYPESnumeric_to_long(Numeric *nv, long *lp) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
PGTYPESnumeric_to_decimal(Numeric *src, Decimal * dst) |
|
|
|
|
PGTYPESnumeric_to_decimal(numeric *src, decimal * dst) |
|
|
|
|
{ |
|
|
|
|
int i; |
|
|
|
|
|
|
|
|
@ -1526,7 +1526,7 @@ PGTYPESnumeric_to_decimal(Numeric *src, Decimal * dst) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
int |
|
|
|
|
PGTYPESnumeric_from_decimal(Decimal * src, Numeric *dst) |
|
|
|
|
PGTYPESnumeric_from_decimal(decimal * src, numeric *dst) |
|
|
|
|
{ |
|
|
|
|
int i; |
|
|
|
|
|
|
|
|
|