PostgreSQL Source Code  git master
pgtypes_numeric.h File Reference
#include <pgtypes.h>
Include dependency graph for pgtypes_numeric.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  numeric
 
struct  decimal
 

Macros

#define NUMERIC_POS   0x0000
 
#define NUMERIC_NEG   0x4000
 
#define NUMERIC_NAN   0xC000
 
#define NUMERIC_NULL   0xF000
 
#define NUMERIC_MAX_PRECISION   1000
 
#define NUMERIC_MAX_DISPLAY_SCALE   NUMERIC_MAX_PRECISION
 
#define NUMERIC_MIN_DISPLAY_SCALE   0
 
#define NUMERIC_MIN_SIG_DIGITS   16
 
#define DECSIZE   30
 

Typedefs

typedef unsigned char NumericDigit
 

Functions

numericPGTYPESnumeric_new (void)
 
decimalPGTYPESdecimal_new (void)
 
void PGTYPESnumeric_free (numeric *)
 
void PGTYPESdecimal_free (decimal *)
 
numericPGTYPESnumeric_from_asc (char *, char **)
 
char * PGTYPESnumeric_to_asc (numeric *, int)
 
int PGTYPESnumeric_add (numeric *, numeric *, numeric *)
 
int PGTYPESnumeric_sub (numeric *, numeric *, numeric *)
 
int PGTYPESnumeric_mul (numeric *, numeric *, numeric *)
 
int PGTYPESnumeric_div (numeric *, numeric *, numeric *)
 
int PGTYPESnumeric_cmp (numeric *, numeric *)
 
int PGTYPESnumeric_from_int (signed int, numeric *)
 
int PGTYPESnumeric_from_long (signed long int, numeric *)
 
int PGTYPESnumeric_copy (numeric *, numeric *)
 
int PGTYPESnumeric_from_double (double, numeric *)
 
int PGTYPESnumeric_to_double (numeric *, double *)
 
int PGTYPESnumeric_to_int (numeric *, int *)
 
int PGTYPESnumeric_to_long (numeric *, long *)
 
int PGTYPESnumeric_to_decimal (numeric *, decimal *)
 
int PGTYPESnumeric_from_decimal (decimal *, numeric *)
 

Macro Definition Documentation

◆ DECSIZE

#define DECSIZE   30

Definition at line 15 of file pgtypes_numeric.h.

◆ NUMERIC_MAX_DISPLAY_SCALE

#define NUMERIC_MAX_DISPLAY_SCALE   NUMERIC_MAX_PRECISION

Definition at line 11 of file pgtypes_numeric.h.

◆ NUMERIC_MAX_PRECISION

#define NUMERIC_MAX_PRECISION   1000

Definition at line 10 of file pgtypes_numeric.h.

◆ NUMERIC_MIN_DISPLAY_SCALE

#define NUMERIC_MIN_DISPLAY_SCALE   0

Definition at line 12 of file pgtypes_numeric.h.

◆ NUMERIC_MIN_SIG_DIGITS

#define NUMERIC_MIN_SIG_DIGITS   16

Definition at line 13 of file pgtypes_numeric.h.

◆ NUMERIC_NAN

#define NUMERIC_NAN   0xC000

Definition at line 8 of file pgtypes_numeric.h.

◆ NUMERIC_NEG

#define NUMERIC_NEG   0x4000

Definition at line 7 of file pgtypes_numeric.h.

◆ NUMERIC_NULL

#define NUMERIC_NULL   0xF000

Definition at line 9 of file pgtypes_numeric.h.

◆ NUMERIC_POS

#define NUMERIC_POS   0x0000

Definition at line 6 of file pgtypes_numeric.h.

Typedef Documentation

◆ NumericDigit

typedef unsigned char NumericDigit

Definition at line 17 of file pgtypes_numeric.h.

Function Documentation

◆ PGTYPESdecimal_free()

void PGTYPESdecimal_free ( decimal var)

Definition at line 396 of file numeric.c.

397 {
398  free(var);
399 }
#define free(a)
Definition: header.h:65

References free.

Referenced by main().

◆ PGTYPESdecimal_new()

decimal* PGTYPESdecimal_new ( void  )

Definition at line 63 of file numeric.c.

64 {
65  decimal *var;
66 
67  if ((var = (decimal *) pgtypes_alloc(sizeof(decimal))) == NULL)
68  return NULL;
69 
70  memset(var, 0, sizeof(decimal));
71 
72  return var;
73 }
char * pgtypes_alloc(long size)
Definition: common.c:10

References pgtypes_alloc().

Referenced by main().

◆ PGTYPESnumeric_add()

int PGTYPESnumeric_add ( numeric var1,
numeric var2,
numeric result 
)

Definition at line 641 of file numeric.c.

642 {
643  /*
644  * Decide on the signs of the two variables what to do
645  */
646  if (var1->sign == NUMERIC_POS)
647  {
648  if (var2->sign == NUMERIC_POS)
649  {
650  /*
651  * Both are positive result = +(ABS(var1) + ABS(var2))
652  */
653  if (add_abs(var1, var2, result) != 0)
654  return -1;
655  result->sign = NUMERIC_POS;
656  }
657  else
658  {
659  /*
660  * var1 is positive, var2 is negative Must compare absolute values
661  */
662  switch (cmp_abs(var1, var2))
663  {
664  case 0:
665  /* ----------
666  * ABS(var1) == ABS(var2)
667  * result = ZERO
668  * ----------
669  */
670  zero_var(result);
671  result->rscale = Max(var1->rscale, var2->rscale);
672  result->dscale = Max(var1->dscale, var2->dscale);
673  break;
674 
675  case 1:
676  /* ----------
677  * ABS(var1) > ABS(var2)
678  * result = +(ABS(var1) - ABS(var2))
679  * ----------
680  */
681  if (sub_abs(var1, var2, result) != 0)
682  return -1;
683  result->sign = NUMERIC_POS;
684  break;
685 
686  case -1:
687  /* ----------
688  * ABS(var1) < ABS(var2)
689  * result = -(ABS(var2) - ABS(var1))
690  * ----------
691  */
692  if (sub_abs(var2, var1, result) != 0)
693  return -1;
694  result->sign = NUMERIC_NEG;
695  break;
696  }
697  }
698  }
699  else
700  {
701  if (var2->sign == NUMERIC_POS)
702  {
703  /* ----------
704  * var1 is negative, var2 is positive
705  * Must compare absolute values
706  * ----------
707  */
708  switch (cmp_abs(var1, var2))
709  {
710  case 0:
711  /* ----------
712  * ABS(var1) == ABS(var2)
713  * result = ZERO
714  * ----------
715  */
716  zero_var(result);
717  result->rscale = Max(var1->rscale, var2->rscale);
718  result->dscale = Max(var1->dscale, var2->dscale);
719  break;
720 
721  case 1:
722  /* ----------
723  * ABS(var1) > ABS(var2)
724  * result = -(ABS(var1) - ABS(var2))
725  * ----------
726  */
727  if (sub_abs(var1, var2, result) != 0)
728  return -1;
729  result->sign = NUMERIC_NEG;
730  break;
731 
732  case -1:
733  /* ----------
734  * ABS(var1) < ABS(var2)
735  * result = +(ABS(var2) - ABS(var1))
736  * ----------
737  */
738  if (sub_abs(var2, var1, result) != 0)
739  return -1;
740  result->sign = NUMERIC_POS;
741  break;
742  }
743  }
744  else
745  {
746  /* ----------
747  * Both are negative
748  * result = -(ABS(var1) + ABS(var2))
749  * ----------
750  */
751  if (add_abs(var1, var2, result) != 0)
752  return -1;
753  result->sign = NUMERIC_NEG;
754  }
755  }
756 
757  return 0;
758 }
#define NUMERIC_NEG
Definition: numeric.c:167
#define NUMERIC_POS
Definition: numeric.c:166
#define Max(x, y)
Definition: numeric.c:13
static int sub_abs(numeric *var1, numeric *var2, numeric *result)
Definition: numeric.c:557
static int add_abs(numeric *var1, numeric *var2, numeric *result)
Definition: numeric.c:469
static void zero_var(numeric *var)
Definition: numeric.c:378
static int cmp_abs(numeric *var1, numeric *var2)
Definition: numeric.c:411

References add_abs(), cmp_abs(), numeric::dscale, Max, NUMERIC_NEG, NUMERIC_POS, numeric::rscale, numeric::sign, sub_abs(), and zero_var().

Referenced by decadd(), and main().

◆ PGTYPESnumeric_cmp()

int PGTYPESnumeric_cmp ( numeric var1,
numeric var2 
)

Definition at line 1286 of file numeric.c.

1287 {
1288  /* use cmp_abs function to calculate the result */
1289 
1290  /* both are positive: normal comparison with cmp_abs */
1291  if (var1->sign == NUMERIC_POS && var2->sign == NUMERIC_POS)
1292  return cmp_abs(var1, var2);
1293 
1294  /* both are negative: return the inverse of the normal comparison */
1295  if (var1->sign == NUMERIC_NEG && var2->sign == NUMERIC_NEG)
1296  {
1297  /*
1298  * instead of inverting the result, we invert the parameter ordering
1299  */
1300  return cmp_abs(var2, var1);
1301  }
1302 
1303  /* one is positive, one is negative: trivial */
1304  if (var1->sign == NUMERIC_POS && var2->sign == NUMERIC_NEG)
1305  return 1;
1306  if (var1->sign == NUMERIC_NEG && var2->sign == NUMERIC_POS)
1307  return -1;
1308 
1309  errno = PGTYPES_NUM_BAD_NUMERIC;
1310  return INT_MAX;
1311 }
#define PGTYPES_NUM_BAD_NUMERIC
Definition: pgtypes_error.h:4

References cmp_abs(), NUMERIC_NEG, NUMERIC_POS, PGTYPES_NUM_BAD_NUMERIC, and numeric::sign.

Referenced by deccmp(), and main().

◆ PGTYPESnumeric_copy()

int PGTYPESnumeric_copy ( numeric src,
numeric dst 
)

Definition at line 1393 of file numeric.c.

1394 {
1395  int i;
1396 
1397  if (dst == NULL)
1398  return -1;
1399  zero_var(dst);
1400 
1401  dst->weight = src->weight;
1402  dst->rscale = src->rscale;
1403  dst->dscale = src->dscale;
1404  dst->sign = src->sign;
1405 
1406  if (alloc_var(dst, src->ndigits) != 0)
1407  return -1;
1408 
1409  for (i = 0; i < src->ndigits; i++)
1410  dst->digits[i] = src->digits[i];
1411 
1412  return 0;
1413 }
static int alloc_var(numeric *var, int ndigits)
Definition: numeric.c:33
int i
Definition: isn.c:73
NumericDigit * digits

References alloc_var(), numeric::digits, numeric::dscale, i, numeric::ndigits, numeric::rscale, numeric::sign, numeric::weight, and zero_var().

Referenced by ecpg_get_data(), ecpg_store_input(), and main().

◆ PGTYPESnumeric_div()

int PGTYPESnumeric_div ( numeric var1,
numeric var2,
numeric result 
)

Definition at line 1057 of file numeric.c.

1058 {
1059  NumericDigit *res_digits;
1060  int res_ndigits;
1061  int res_sign;
1062  int res_weight;
1063  numeric dividend;
1064  numeric divisor[10];
1065  int ndigits_tmp;
1066  int weight_tmp;
1067  int rscale_tmp;
1068  int ri;
1069  int i;
1070  long guess;
1071  long first_have;
1072  long first_div;
1073  int first_nextdigit;
1074  int stat = 0;
1075  int rscale;
1076  int res_dscale = select_div_scale(var1, var2, &rscale);
1077  int err = -1;
1078  NumericDigit *tmp_buf;
1079 
1080  /*
1081  * First of all division by zero check
1082  */
1083  ndigits_tmp = var2->ndigits + 1;
1084  if (ndigits_tmp == 1)
1085  {
1086  errno = PGTYPES_NUM_DIVIDE_ZERO;
1087  return -1;
1088  }
1089 
1090  /*
1091  * Determine the result sign, weight and number of digits to calculate
1092  */
1093  if (var1->sign == var2->sign)
1094  res_sign = NUMERIC_POS;
1095  else
1096  res_sign = NUMERIC_NEG;
1097  res_weight = var1->weight - var2->weight + 1;
1098  res_ndigits = rscale + res_weight;
1099  if (res_ndigits <= 0)
1100  res_ndigits = 1;
1101 
1102  /*
1103  * Now result zero check
1104  */
1105  if (var1->ndigits == 0)
1106  {
1107  zero_var(result);
1108  result->rscale = rscale;
1109  return 0;
1110  }
1111 
1112  /*
1113  * Initialize local variables
1114  */
1115  init_var(&dividend);
1116  for (i = 1; i < 10; i++)
1117  init_var(&divisor[i]);
1118 
1119  /*
1120  * Make a copy of the divisor which has one leading zero digit
1121  */
1122  divisor[1].ndigits = ndigits_tmp;
1123  divisor[1].rscale = var2->ndigits;
1124  divisor[1].sign = NUMERIC_POS;
1125  divisor[1].buf = digitbuf_alloc(ndigits_tmp);
1126  if (divisor[1].buf == NULL)
1127  goto done;
1128  divisor[1].digits = divisor[1].buf;
1129  divisor[1].digits[0] = 0;
1130  memcpy(&(divisor[1].digits[1]), var2->digits, ndigits_tmp - 1);
1131 
1132  /*
1133  * Make a copy of the dividend
1134  */
1135  dividend.ndigits = var1->ndigits;
1136  dividend.weight = 0;
1137  dividend.rscale = var1->ndigits;
1138  dividend.sign = NUMERIC_POS;
1139  dividend.buf = digitbuf_alloc(var1->ndigits);
1140  if (dividend.buf == NULL)
1141  goto done;
1142  dividend.digits = dividend.buf;
1143  memcpy(dividend.digits, var1->digits, var1->ndigits);
1144 
1145  /*
1146  * Setup the result. Do the allocation in a temporary buffer first, so we
1147  * don't free result->buf unless we have successfully allocated a buffer
1148  * to replace it with.
1149  */
1150  tmp_buf = digitbuf_alloc(res_ndigits + 2);
1151  if (tmp_buf == NULL)
1152  goto done;
1153  digitbuf_free(result->buf);
1154  result->buf = tmp_buf;
1155  res_digits = result->buf;
1156  result->digits = res_digits;
1157  result->ndigits = res_ndigits;
1158  result->weight = res_weight;
1159  result->rscale = rscale;
1160  result->sign = res_sign;
1161  res_digits[0] = 0;
1162 
1163  first_div = divisor[1].digits[1] * 10;
1164  if (ndigits_tmp > 2)
1165  first_div += divisor[1].digits[2];
1166 
1167  first_have = 0;
1168  first_nextdigit = 0;
1169 
1170  weight_tmp = 1;
1171  rscale_tmp = divisor[1].rscale;
1172 
1173  for (ri = 0; ri <= res_ndigits; ri++)
1174  {
1175  first_have = first_have * 10;
1176  if (first_nextdigit >= 0 && first_nextdigit < dividend.ndigits)
1177  first_have += dividend.digits[first_nextdigit];
1178  first_nextdigit++;
1179 
1180  guess = (first_have * 10) / first_div + 1;
1181  if (guess > 9)
1182  guess = 9;
1183 
1184  while (guess > 0)
1185  {
1186  if (divisor[guess].buf == NULL)
1187  {
1188  int i;
1189  long sum = 0;
1190 
1191  memcpy(&divisor[guess], &divisor[1], sizeof(numeric));
1192  divisor[guess].buf = digitbuf_alloc(divisor[guess].ndigits);
1193  if (divisor[guess].buf == NULL)
1194  goto done;
1195  divisor[guess].digits = divisor[guess].buf;
1196  for (i = divisor[1].ndigits - 1; i >= 0; i--)
1197  {
1198  sum += divisor[1].digits[i] * guess;
1199  divisor[guess].digits[i] = sum % 10;
1200  sum /= 10;
1201  }
1202  }
1203 
1204  divisor[guess].weight = weight_tmp;
1205  divisor[guess].rscale = rscale_tmp;
1206 
1207  stat = cmp_abs(&dividend, &divisor[guess]);
1208  if (stat >= 0)
1209  break;
1210 
1211  guess--;
1212  }
1213 
1214  res_digits[ri + 1] = guess;
1215  if (stat == 0)
1216  {
1217  ri++;
1218  break;
1219  }
1220 
1221  weight_tmp--;
1222  rscale_tmp++;
1223 
1224  if (guess == 0)
1225  continue;
1226 
1227  if (sub_abs(&dividend, &divisor[guess], &dividend) != 0)
1228  goto done;
1229 
1230  first_nextdigit = dividend.weight - weight_tmp;
1231  first_have = 0;
1232  if (first_nextdigit >= 0 && first_nextdigit < dividend.ndigits)
1233  first_have = dividend.digits[first_nextdigit];
1234  first_nextdigit++;
1235  }
1236 
1237  result->ndigits = ri + 1;
1238  if (ri == res_ndigits + 1)
1239  {
1240  int carry = (res_digits[ri] > 4) ? 1 : 0;
1241 
1242  result->ndigits = ri;
1243  res_digits[ri] = 0;
1244 
1245  while (carry && ri > 0)
1246  {
1247  carry += res_digits[--ri];
1248  res_digits[ri] = carry % 10;
1249  carry /= 10;
1250  }
1251  }
1252 
1253  while (result->ndigits > 0 && *(result->digits) == 0)
1254  {
1255  (result->digits)++;
1256  (result->weight)--;
1257  (result->ndigits)--;
1258  }
1259  while (result->ndigits > 0 && result->digits[result->ndigits - 1] == 0)
1260  (result->ndigits)--;
1261  if (result->ndigits == 0)
1262  result->sign = NUMERIC_POS;
1263 
1264  result->dscale = res_dscale;
1265  err = 0; /* if we've made it this far, return success */
1266 
1267 done:
1268 
1269  /*
1270  * Tidy up
1271  */
1272  if (dividend.buf != NULL)
1273  digitbuf_free(dividend.buf);
1274 
1275  for (i = 1; i < 10; i++)
1276  {
1277  if (divisor[i].buf != NULL)
1278  digitbuf_free(divisor[i].buf);
1279  }
1280 
1281  return err;
1282 }
int16 NumericDigit
Definition: numeric.c:102
int digits
Definition: informix.c:666
#define digitbuf_free(buf)
Definition: numeric.c:19
#define digitbuf_alloc(size)
Definition: numeric.c:18
static int select_div_scale(numeric *var1, numeric *var2, int *rscale)
Definition: numeric.c:991
#define init_var(v)
Definition: numeric.c:16
static char * buf
Definition: pg_test_fsync.c:67
#define PGTYPES_NUM_DIVIDE_ZERO
Definition: pgtypes_error.h:5
NumericDigit * buf

References buf, numeric::buf, cmp_abs(), digitbuf_alloc, digitbuf_free, digits, numeric::digits, numeric::dscale, i, init_var, numeric::ndigits, NUMERIC_NEG, NUMERIC_POS, PGTYPES_NUM_DIVIDE_ZERO, numeric::rscale, select_div_scale(), numeric::sign, sub_abs(), numeric::weight, and zero_var().

Referenced by decdiv(), and main().

◆ PGTYPESnumeric_free()

◆ PGTYPESnumeric_from_asc()

numeric* PGTYPESnumeric_from_asc ( char *  str,
char **  endptr 
)

Definition at line 325 of file numeric.c.

326 {
327  numeric *value = (numeric *) pgtypes_alloc(sizeof(numeric));
328  int ret;
329 
330  char *realptr;
331  char **ptr = (endptr != NULL) ? endptr : &realptr;
332 
333  if (!value)
334  return NULL;
335 
336  ret = set_var_from_str(str, ptr, value);
337  if (ret)
338  {
340  return NULL;
341  }
342 
343  return value;
344 }
static struct @151 value
static int set_var_from_str(char *str, char **ptr, numeric *dest)
Definition: numeric.c:82
void PGTYPESnumeric_free(numeric *var)
Definition: numeric.c:389

References pgtypes_alloc(), PGTYPESnumeric_free(), set_var_from_str(), generate_unaccent_rules::str, and value.

Referenced by deccvasc(), ecpg_get_data(), ecpg_set_compat_sqlda(), ecpg_set_native_sqlda(), main(), and sqlda_common_total_size().

◆ PGTYPESnumeric_from_decimal()

int PGTYPESnumeric_from_decimal ( decimal src,
numeric dst 
)

Definition at line 1575 of file numeric.c.

1576 {
1577  int i;
1578 
1579  zero_var(dst);
1580 
1581  dst->weight = src->weight;
1582  dst->rscale = src->rscale;
1583  dst->dscale = src->dscale;
1584  dst->sign = src->sign;
1585 
1586  if (alloc_var(dst, src->ndigits) != 0)
1587  return -1;
1588 
1589  for (i = 0; i < src->ndigits; i++)
1590  dst->digits[i] = src->digits[i];
1591 
1592  return 0;
1593 }
NumericDigit digits[DECSIZE]

References alloc_var(), numeric::digits, decimal::digits, numeric::dscale, decimal::dscale, i, decimal::ndigits, numeric::rscale, decimal::rscale, numeric::sign, decimal::sign, numeric::weight, decimal::weight, and zero_var().

Referenced by deccall2(), deccall3(), dectoasc(), dectodbl(), dectoint(), dectolong(), ecpg_store_input(), and main().

◆ PGTYPESnumeric_from_double()

int PGTYPESnumeric_from_double ( double  d,
numeric dst 
)

Definition at line 1416 of file numeric.c.

1417 {
1418  char buffer[DBL_DIG + 100];
1419  numeric *tmp;
1420  int i;
1421 
1422  if (sprintf(buffer, "%.*g", DBL_DIG, d) <= 0)
1423  return -1;
1424 
1425  if ((tmp = PGTYPESnumeric_from_asc(buffer, NULL)) == NULL)
1426  return -1;
1427  i = PGTYPESnumeric_copy(tmp, dst);
1428  PGTYPESnumeric_free(tmp);
1429  if (i != 0)
1430  return -1;
1431 
1432  errno = 0;
1433  return 0;
1434 }
int PGTYPESnumeric_copy(numeric *src, numeric *dst)
Definition: numeric.c:1393
numeric * PGTYPESnumeric_from_asc(char *str, char **endptr)
Definition: numeric.c:325
#define sprintf
Definition: port.h:227

References i, PGTYPESnumeric_copy(), PGTYPESnumeric_free(), PGTYPESnumeric_from_asc(), and sprintf.

Referenced by deccvdbl().

◆ PGTYPESnumeric_from_int()

int PGTYPESnumeric_from_int ( signed int  int_val,
numeric var 
)

Definition at line 1314 of file numeric.c.

1315 {
1316  /* implicit conversion */
1317  signed long int long_int = int_val;
1318 
1319  return PGTYPESnumeric_from_long(long_int, var);
1320 }
int PGTYPESnumeric_from_long(signed long int long_val, numeric *var)
Definition: numeric.c:1323

References PGTYPESnumeric_from_long().

Referenced by deccvint(), and main().

◆ PGTYPESnumeric_from_long()

int PGTYPESnumeric_from_long ( signed long int  long_val,
numeric var 
)

Definition at line 1323 of file numeric.c.

1324 {
1325  /* calculate the size of the long int number */
1326  /* a number n needs log_10 n digits */
1327 
1328  /*
1329  * however we multiply by 10 each time and compare instead of calculating
1330  * the logarithm
1331  */
1332 
1333  int size = 0;
1334  int i;
1335  signed long int abs_long_val = long_val;
1336  signed long int extract;
1337  signed long int reach_limit;
1338 
1339  if (abs_long_val < 0)
1340  {
1341  abs_long_val *= -1;
1342  var->sign = NUMERIC_NEG;
1343  }
1344  else
1345  var->sign = NUMERIC_POS;
1346 
1347  reach_limit = 1;
1348  do
1349  {
1350  size++;
1351  reach_limit *= 10;
1352  } while (reach_limit - 1 < abs_long_val && reach_limit <= LONG_MAX / 10);
1353 
1354  if (reach_limit > LONG_MAX / 10)
1355  {
1356  /* add the first digit and a .0 */
1357  size += 2;
1358  }
1359  else
1360  {
1361  /* always add a .0 */
1362  size++;
1363  reach_limit /= 10;
1364  }
1365 
1366  if (alloc_var(var, size) < 0)
1367  return -1;
1368 
1369  var->rscale = 1;
1370  var->dscale = 1;
1371  var->weight = size - 2;
1372 
1373  i = 0;
1374  do
1375  {
1376  extract = abs_long_val - (abs_long_val % reach_limit);
1377  var->digits[i] = extract / reach_limit;
1378  abs_long_val -= extract;
1379  i++;
1380  reach_limit /= 10;
1381 
1382  /*
1383  * we can abandon if abs_long_val reaches 0, because the memory is
1384  * initialized properly and filled with '0', so converting 10000 in
1385  * only one step is no problem
1386  */
1387  } while (abs_long_val > 0);
1388 
1389  return 0;
1390 }

References alloc_var(), numeric::digits, numeric::dscale, i, NUMERIC_NEG, NUMERIC_POS, numeric::rscale, numeric::sign, and numeric::weight.

Referenced by deccvlong(), and main().

◆ PGTYPESnumeric_mul()

int PGTYPESnumeric_mul ( numeric var1,
numeric var2,
numeric result 
)

Definition at line 900 of file numeric.c.

901 {
902  NumericDigit *res_buf;
903  NumericDigit *res_digits;
904  int res_ndigits;
905  int res_weight;
906  int res_sign;
907  int i,
908  ri,
909  i1,
910  i2;
911  long sum = 0;
912  int global_rscale = var1->rscale + var2->rscale;
913 
914  res_weight = var1->weight + var2->weight + 2;
915  res_ndigits = var1->ndigits + var2->ndigits + 1;
916  if (var1->sign == var2->sign)
917  res_sign = NUMERIC_POS;
918  else
919  res_sign = NUMERIC_NEG;
920 
921  if ((res_buf = digitbuf_alloc(res_ndigits)) == NULL)
922  return -1;
923  res_digits = res_buf;
924  memset(res_digits, 0, res_ndigits);
925 
926  ri = res_ndigits;
927  for (i1 = var1->ndigits - 1; i1 >= 0; i1--)
928  {
929  sum = 0;
930  i = --ri;
931 
932  for (i2 = var2->ndigits - 1; i2 >= 0; i2--)
933  {
934  sum += res_digits[i] + var1->digits[i1] * var2->digits[i2];
935  res_digits[i--] = sum % 10;
936  sum /= 10;
937  }
938  res_digits[i] = sum;
939  }
940 
941  i = res_weight + global_rscale + 2;
942  if (i >= 0 && i < res_ndigits)
943  {
944  sum = (res_digits[i] > 4) ? 1 : 0;
945  res_ndigits = i;
946  i--;
947  while (sum)
948  {
949  sum += res_digits[i];
950  res_digits[i--] = sum % 10;
951  sum /= 10;
952  }
953  }
954 
955  while (res_ndigits > 0 && *res_digits == 0)
956  {
957  res_digits++;
958  res_weight--;
959  res_ndigits--;
960  }
961  while (res_ndigits > 0 && res_digits[res_ndigits - 1] == 0)
962  res_ndigits--;
963 
964  if (res_ndigits == 0)
965  {
966  res_sign = NUMERIC_POS;
967  res_weight = 0;
968  }
969 
970  digitbuf_free(result->buf);
971  result->buf = res_buf;
972  result->digits = res_digits;
973  result->ndigits = res_ndigits;
974  result->weight = res_weight;
975  result->rscale = global_rscale;
976  result->sign = res_sign;
977  result->dscale = var1->dscale + var2->dscale;
978 
979  return 0;
980 }

References numeric::buf, digitbuf_alloc, digitbuf_free, numeric::digits, numeric::dscale, i, numeric::ndigits, NUMERIC_NEG, NUMERIC_POS, numeric::rscale, numeric::sign, and numeric::weight.

Referenced by decmul(), and main().

◆ PGTYPESnumeric_new()

numeric* PGTYPESnumeric_new ( void  )

Definition at line 46 of file numeric.c.

47 {
48  numeric *var;
49 
50  if ((var = (numeric *) pgtypes_alloc(sizeof(numeric))) == NULL)
51  return NULL;
52 
53  if (alloc_var(var, 0) < 0)
54  {
55  free(var);
56  return NULL;
57  }
58 
59  return var;
60 }

References alloc_var(), free, and pgtypes_alloc().

Referenced by deccall2(), deccall3(), deccvdbl(), deccvint(), deccvlong(), dectoasc(), dectodbl(), dectoint(), dectolong(), ecpg_get_data(), ecpg_store_input(), and main().

◆ PGTYPESnumeric_sub()

int PGTYPESnumeric_sub ( numeric var1,
numeric var2,
numeric result 
)

Definition at line 769 of file numeric.c.

770 {
771  /*
772  * Decide on the signs of the two variables what to do
773  */
774  if (var1->sign == NUMERIC_POS)
775  {
776  if (var2->sign == NUMERIC_NEG)
777  {
778  /* ----------
779  * var1 is positive, var2 is negative
780  * result = +(ABS(var1) + ABS(var2))
781  * ----------
782  */
783  if (add_abs(var1, var2, result) != 0)
784  return -1;
785  result->sign = NUMERIC_POS;
786  }
787  else
788  {
789  /* ----------
790  * Both are positive
791  * Must compare absolute values
792  * ----------
793  */
794  switch (cmp_abs(var1, var2))
795  {
796  case 0:
797  /* ----------
798  * ABS(var1) == ABS(var2)
799  * result = ZERO
800  * ----------
801  */
802  zero_var(result);
803  result->rscale = Max(var1->rscale, var2->rscale);
804  result->dscale = Max(var1->dscale, var2->dscale);
805  break;
806 
807  case 1:
808  /* ----------
809  * ABS(var1) > ABS(var2)
810  * result = +(ABS(var1) - ABS(var2))
811  * ----------
812  */
813  if (sub_abs(var1, var2, result) != 0)
814  return -1;
815  result->sign = NUMERIC_POS;
816  break;
817 
818  case -1:
819  /* ----------
820  * ABS(var1) < ABS(var2)
821  * result = -(ABS(var2) - ABS(var1))
822  * ----------
823  */
824  if (sub_abs(var2, var1, result) != 0)
825  return -1;
826  result->sign = NUMERIC_NEG;
827  break;
828  }
829  }
830  }
831  else
832  {
833  if (var2->sign == NUMERIC_NEG)
834  {
835  /* ----------
836  * Both are negative
837  * Must compare absolute values
838  * ----------
839  */
840  switch (cmp_abs(var1, var2))
841  {
842  case 0:
843  /* ----------
844  * ABS(var1) == ABS(var2)
845  * result = ZERO
846  * ----------
847  */
848  zero_var(result);
849  result->rscale = Max(var1->rscale, var2->rscale);
850  result->dscale = Max(var1->dscale, var2->dscale);
851  break;
852 
853  case 1:
854  /* ----------
855  * ABS(var1) > ABS(var2)
856  * result = -(ABS(var1) - ABS(var2))
857  * ----------
858  */
859  if (sub_abs(var1, var2, result) != 0)
860  return -1;
861  result->sign = NUMERIC_NEG;
862  break;
863 
864  case -1:
865  /* ----------
866  * ABS(var1) < ABS(var2)
867  * result = +(ABS(var2) - ABS(var1))
868  * ----------
869  */
870  if (sub_abs(var2, var1, result) != 0)
871  return -1;
872  result->sign = NUMERIC_POS;
873  break;
874  }
875  }
876  else
877  {
878  /* ----------
879  * var1 is negative, var2 is positive
880  * result = -(ABS(var1) + ABS(var2))
881  * ----------
882  */
883  if (add_abs(var1, var2, result) != 0)
884  return -1;
885  result->sign = NUMERIC_NEG;
886  }
887  }
888 
889  return 0;
890 }

References add_abs(), cmp_abs(), numeric::dscale, Max, NUMERIC_NEG, NUMERIC_POS, numeric::rscale, numeric::sign, sub_abs(), and zero_var().

Referenced by decsub(), and main().

◆ PGTYPESnumeric_to_asc()

char* PGTYPESnumeric_to_asc ( numeric num,
int  dscale 
)

Definition at line 347 of file numeric.c.

348 {
349  numeric *numcopy = PGTYPESnumeric_new();
350  char *s;
351 
352  if (numcopy == NULL)
353  return NULL;
354 
355  if (PGTYPESnumeric_copy(num, numcopy) < 0)
356  {
357  PGTYPESnumeric_free(numcopy);
358  return NULL;
359  }
360 
361  if (dscale < 0)
362  dscale = num->dscale;
363 
364  /* get_str_from_var may change its argument */
365  s = get_str_from_var(numcopy, dscale);
366  PGTYPESnumeric_free(numcopy);
367  return s;
368 }
static char * get_str_from_var(numeric *var, int dscale)
Definition: numeric.c:230
numeric * PGTYPESnumeric_new(void)
Definition: numeric.c:46

References numeric::dscale, get_str_from_var(), PGTYPESnumeric_copy(), PGTYPESnumeric_free(), and PGTYPESnumeric_new().

Referenced by dectoasc(), ecpg_store_input(), and main().

◆ PGTYPESnumeric_to_decimal()

int PGTYPESnumeric_to_decimal ( numeric src,
decimal dst 
)

Definition at line 1552 of file numeric.c.

1553 {
1554  int i;
1555 
1556  if (src->ndigits > DECSIZE)
1557  {
1558  errno = PGTYPES_NUM_OVERFLOW;
1559  return -1;
1560  }
1561 
1562  dst->weight = src->weight;
1563  dst->rscale = src->rscale;
1564  dst->dscale = src->dscale;
1565  dst->sign = src->sign;
1566  dst->ndigits = src->ndigits;
1567 
1568  for (i = 0; i < src->ndigits; i++)
1569  dst->digits[i] = src->digits[i];
1570 
1571  return 0;
1572 }
#define PGTYPES_NUM_OVERFLOW
Definition: pgtypes_error.h:3
#define DECSIZE

References DECSIZE, numeric::digits, decimal::digits, numeric::dscale, decimal::dscale, i, numeric::ndigits, decimal::ndigits, PGTYPES_NUM_OVERFLOW, numeric::rscale, decimal::rscale, numeric::sign, decimal::sign, numeric::weight, and decimal::weight.

Referenced by deccall3(), deccvasc(), deccvdbl(), deccvint(), deccvlong(), ecpg_get_data(), and main().

◆ PGTYPESnumeric_to_double()

int PGTYPESnumeric_to_double ( numeric nv,
double *  dp 
)

Definition at line 1488 of file numeric.c.

1489 {
1490  double tmp;
1491 
1492  if (numericvar_to_double(nv, &tmp) != 0)
1493  return -1;
1494  *dp = tmp;
1495  return 0;
1496 }
static int numericvar_to_double(numeric *var, double *dp)
Definition: numeric.c:1437

References numericvar_to_double().

Referenced by dectodbl(), and main().

◆ PGTYPESnumeric_to_int()

int PGTYPESnumeric_to_int ( numeric nv,
int *  ip 
)

Definition at line 1499 of file numeric.c.

1500 {
1501  long l;
1502  int i;
1503 
1504  if ((i = PGTYPESnumeric_to_long(nv, &l)) != 0)
1505  return i;
1506 
1507 /* silence compilers that might complain about useless tests */
1508 #if SIZEOF_LONG > SIZEOF_INT
1509 
1510  if (l < INT_MIN || l > INT_MAX)
1511  {
1512  errno = PGTYPES_NUM_OVERFLOW;
1513  return -1;
1514  }
1515 
1516 #endif
1517 
1518  *ip = (int) l;
1519  return 0;
1520 }
int PGTYPESnumeric_to_long(numeric *nv, long *lp)
Definition: numeric.c:1523

References i, PGTYPES_NUM_OVERFLOW, and PGTYPESnumeric_to_long().

Referenced by dectoint(), and main().

◆ PGTYPESnumeric_to_long()

int PGTYPESnumeric_to_long ( numeric nv,
long *  lp 
)

Definition at line 1523 of file numeric.c.

1524 {
1525  char *s = PGTYPESnumeric_to_asc(nv, 0);
1526  char *endptr;
1527 
1528  if (s == NULL)
1529  return -1;
1530 
1531  errno = 0;
1532  *lp = strtol(s, &endptr, 10);
1533  if (endptr == s)
1534  {
1535  /* this should not happen actually */
1536  free(s);
1537  return -1;
1538  }
1539  free(s);
1540  if (errno == ERANGE)
1541  {
1542  if (*lp == LONG_MIN)
1543  errno = PGTYPES_NUM_UNDERFLOW;
1544  else
1545  errno = PGTYPES_NUM_OVERFLOW;
1546  return -1;
1547  }
1548  return 0;
1549 }
char * PGTYPESnumeric_to_asc(numeric *num, int dscale)
Definition: numeric.c:347
#define PGTYPES_NUM_UNDERFLOW
Definition: pgtypes_error.h:6

References free, PGTYPES_NUM_OVERFLOW, PGTYPES_NUM_UNDERFLOW, and PGTYPESnumeric_to_asc().

Referenced by dectolong(), and main().