1 /*-------------------------------------------------------------------------
4 * An exact numeric data type for the Postgres database system
6 * Original coding 1998, Jan Wieck. Heavily revised 2003, Tom Lane.
8 * Many of the algorithmic ideas are borrowed from David M. Smith's "FM"
9 * multiple-precision math library, most recently published as Algorithm
10 * 786: Multiple-Precision Complex Arithmetic and Functions, ACM
11 * Transactions on Mathematical Software, Vol. 24, No. 4, December 1998,
14 * Copyright (c) 1998-2007, PostgreSQL Global Development Group
17 * $PostgreSQL: pgsql/src/backend/utils/adt/numeric.c,v 1.105 2007/06/15 20:56:50 tgl Exp $
19 *-------------------------------------------------------------------------
29 #include "access/hash.h"
30 #include "catalog/pg_type.h"
31 #include "libpq/pqformat.h"
32 #include "miscadmin.h"
33 #include "utils/array.h"
34 #include "utils/builtins.h"
35 #include "utils/int8.h"
36 #include "utils/numeric.h"
39 * Uncomment the following to enable compilation of dump_numeric()
40 * and dump_var() and to get a dump of any result produced by make_result().
49 * Numeric values are represented in a base-NBASE floating point format.
50 * Each "digit" ranges from 0 to NBASE-1. The type NumericDigit is signed
51 * and wide enough to store a digit. We assume that NBASE*NBASE can fit in
52 * an int. Although the purely calculational routines could handle any even
53 * NBASE that's less than sqrt(INT_MAX), in practice we are only interested
54 * in NBASE a power of ten, so that I/O conversions and decimal rounding
55 * are easy. Also, it's actually more efficient if NBASE is rather less than
56 * sqrt(INT_MAX), so that there is "headroom" for mul_var and div_var to
57 * postpone processing carries.
64 #define DEC_DIGITS 1 /* decimal digits per NBASE digit */
65 #define MUL_GUARD_DIGITS 4 /* these are measured in NBASE digits */
66 #define DIV_GUARD_DIGITS 8
68 typedef signed char NumericDigit;
74 #define DEC_DIGITS 2 /* decimal digits per NBASE digit */
75 #define MUL_GUARD_DIGITS 3 /* these are measured in NBASE digits */
76 #define DIV_GUARD_DIGITS 6
78 typedef signed char NumericDigit;
83 #define HALF_NBASE 5000
84 #define DEC_DIGITS 4 /* decimal digits per NBASE digit */
85 #define MUL_GUARD_DIGITS 2 /* these are measured in NBASE digits */
86 #define DIV_GUARD_DIGITS 4
88 typedef int16 NumericDigit;
93 * The value represented by a NumericVar is determined by the sign, weight,
94 * ndigits, and digits[] array.
95 * Note: the first digit of a NumericVar's value is assumed to be multiplied
96 * by NBASE ** weight. Another way to say it is that there are weight+1
97 * digits before the decimal point. It is possible to have weight < 0.
99 * buf points at the physical start of the palloc'd digit buffer for the
100 * NumericVar. digits points at the first digit in actual use (the one
101 * with the specified weight). We normally leave an unused digit or two
102 * (preset to zeroes) between buf and digits, so that there is room to store
103 * a carry out of the top digit without special pushups. We just need to
104 * decrement digits (and increment weight) to make room for the carry digit.
105 * (There is no such extra space in a numeric value stored in the database,
106 * only in a NumericVar in memory.)
108 * If buf is NULL then the digit buffer isn't actually palloc'd and should
109 * not be freed --- see the constants below for an example.
111 * dscale, or display scale, is the nominal precision expressed as number
112 * of digits after the decimal point (it must always be >= 0 at present).
113 * dscale may be more than the number of physically stored fractional digits,
114 * implying that we have suppressed storage of significant trailing zeroes.
115 * It should never be less than the number of stored digits, since that would
116 * imply hiding digits that are present. NOTE that dscale is always expressed
117 * in *decimal* digits, and so it may correspond to a fractional number of
118 * base-NBASE digits --- divide by DEC_DIGITS to convert to NBASE digits.
120 * rscale, or result scale, is the target precision for a computation.
121 * Like dscale it is expressed as number of *decimal* digits after the decimal
122 * point, and is always >= 0 at present.
123 * Note that rscale is not stored in variables --- it's figured on-the-fly
124 * from the dscales of the inputs.
126 * NB: All the variable-level functions are written in a style that makes it
127 * possible to give one and the same variable as argument and destination.
128 * This is feasible because the digit buffer is separate from the variable.
131 typedef struct NumericVar
133 int ndigits; /* # of digits in digits[] - can be 0! */
134 int weight; /* weight of first digit */
135 int sign; /* NUMERIC_POS, NUMERIC_NEG, or NUMERIC_NAN */
136 int dscale; /* display scale */
137 NumericDigit *buf; /* start of palloc'd space for digits[] */
138 NumericDigit *digits; /* base-NBASE digits */
143 * Some preinitialized constants
146 static NumericDigit const_zero_data[1] = {0};
147 static NumericVar const_zero =
148 {0, 0, NUMERIC_POS, 0, NULL, const_zero_data};
150 static NumericDigit const_one_data[1] = {1};
151 static NumericVar const_one =
152 {1, 0, NUMERIC_POS, 0, NULL, const_one_data};
154 static NumericDigit const_two_data[1] = {2};
155 static NumericVar const_two =
156 {1, 0, NUMERIC_POS, 0, NULL, const_two_data};
159 static NumericDigit const_zero_point_five_data[1] = {5000};
160 #elif DEC_DIGITS == 2
161 static NumericDigit const_zero_point_five_data[1] = {50};
162 #elif DEC_DIGITS == 1
163 static NumericDigit const_zero_point_five_data[1] = {5};
165 static NumericVar const_zero_point_five =
166 {1, -1, NUMERIC_POS, 1, NULL, const_zero_point_five_data};
169 static NumericDigit const_zero_point_nine_data[1] = {9000};
170 #elif DEC_DIGITS == 2
171 static NumericDigit const_zero_point_nine_data[1] = {90};
172 #elif DEC_DIGITS == 1
173 static NumericDigit const_zero_point_nine_data[1] = {9};
175 static NumericVar const_zero_point_nine =
176 {1, -1, NUMERIC_POS, 1, NULL, const_zero_point_nine_data};
179 static NumericDigit const_zero_point_01_data[1] = {100};
180 static NumericVar const_zero_point_01 =
181 {1, -1, NUMERIC_POS, 2, NULL, const_zero_point_01_data};
182 #elif DEC_DIGITS == 2
183 static NumericDigit const_zero_point_01_data[1] = {1};
184 static NumericVar const_zero_point_01 =
185 {1, -1, NUMERIC_POS, 2, NULL, const_zero_point_01_data};
186 #elif DEC_DIGITS == 1
187 static NumericDigit const_zero_point_01_data[1] = {1};
188 static NumericVar const_zero_point_01 =
189 {1, -2, NUMERIC_POS, 2, NULL, const_zero_point_01_data};
193 static NumericDigit const_one_point_one_data[2] = {1, 1000};
194 #elif DEC_DIGITS == 2
195 static NumericDigit const_one_point_one_data[2] = {1, 10};
196 #elif DEC_DIGITS == 1
197 static NumericDigit const_one_point_one_data[2] = {1, 1};
199 static NumericVar const_one_point_one =
200 {2, 0, NUMERIC_POS, 1, NULL, const_one_point_one_data};
202 static NumericVar const_nan =
203 {0, 0, NUMERIC_NAN, 0, NULL, NULL};
206 static const int round_powers[4] = {0, 1000, 100, 10};
216 static void dump_numeric(const char *str, Numeric num);
217 static void dump_var(const char *str, NumericVar *var);
219 #define dump_numeric(s,n)
220 #define dump_var(s,v)
223 #define digitbuf_alloc(ndigits) \
224 ((NumericDigit *) palloc((ndigits) * sizeof(NumericDigit)))
225 #define digitbuf_free(buf) \
231 #define init_var(v) MemSetAligned(v, 0, sizeof(NumericVar))
233 #define NUMERIC_DIGITS(num) ((NumericDigit *)(num)->n_data)
234 #define NUMERIC_NDIGITS(num) \
235 ((VARSIZE(num) - NUMERIC_HDRSZ) / sizeof(NumericDigit))
237 static void alloc_var(NumericVar *var, int ndigits);
238 static void free_var(NumericVar *var);
239 static void zero_var(NumericVar *var);
241 static void set_var_from_str(const char *str, NumericVar *dest);
242 static void set_var_from_num(Numeric value, NumericVar *dest);
243 static void set_var_from_var(NumericVar *value, NumericVar *dest);
244 static char *get_str_from_var(NumericVar *var, int dscale);
246 static Numeric make_result(NumericVar *var);
248 static void apply_typmod(NumericVar *var, int32 typmod);
250 static int32 numericvar_to_int4(NumericVar *var);
251 static bool numericvar_to_int8(NumericVar *var, int64 *result);
252 static void int8_to_numericvar(int64 val, NumericVar *var);
253 static double numeric_to_double_no_overflow(Numeric num);
254 static double numericvar_to_double_no_overflow(NumericVar *var);
256 static int cmp_numerics(Numeric num1, Numeric num2);
257 static int cmp_var(NumericVar *var1, NumericVar *var2);
258 static int cmp_var_common(const NumericDigit *var1digits, int var1ndigits,
259 int var1weight, int var1sign,
260 const NumericDigit *var2digits, int var2ndigits,
261 int var2weight, int var2sign);
262 static void add_var(NumericVar *var1, NumericVar *var2, NumericVar *result);
263 static void sub_var(NumericVar *var1, NumericVar *var2, NumericVar *result);
264 static void mul_var(NumericVar *var1, NumericVar *var2, NumericVar *result,
266 static void div_var(NumericVar *var1, NumericVar *var2, NumericVar *result,
267 int rscale, bool round);
268 static int select_div_scale(NumericVar *var1, NumericVar *var2);
269 static void mod_var(NumericVar *var1, NumericVar *var2, NumericVar *result);
270 static void ceil_var(NumericVar *var, NumericVar *result);
271 static void floor_var(NumericVar *var, NumericVar *result);
273 static void sqrt_var(NumericVar *arg, NumericVar *result, int rscale);
274 static void exp_var(NumericVar *arg, NumericVar *result, int rscale);
275 static void exp_var_internal(NumericVar *arg, NumericVar *result, int rscale);
276 static void ln_var(NumericVar *arg, NumericVar *result, int rscale);
277 static void log_var(NumericVar *base, NumericVar *num, NumericVar *result);
278 static void power_var(NumericVar *base, NumericVar *exp, NumericVar *result);
279 static void power_var_int(NumericVar *base, int exp, NumericVar *result,
282 static int cmp_abs(NumericVar *var1, NumericVar *var2);
283 static int cmp_abs_common(const NumericDigit *var1digits, int var1ndigits,
285 const NumericDigit *var2digits, int var2ndigits,
287 static void add_abs(NumericVar *var1, NumericVar *var2, NumericVar *result);
288 static void sub_abs(NumericVar *var1, NumericVar *var2, NumericVar *result);
289 static void round_var(NumericVar *var, int rscale);
290 static void trunc_var(NumericVar *var, int rscale);
291 static void strip_var(NumericVar *var);
292 static void compute_bucket(Numeric operand, Numeric bound1, Numeric bound2,
293 NumericVar *count_var, NumericVar *result_var);
296 /* ----------------------------------------------------------------------
298 * Input-, output- and rounding-functions
300 * ----------------------------------------------------------------------
307 * Input function for numeric data type
310 numeric_in(PG_FUNCTION_ARGS)
312 char *str = PG_GETARG_CSTRING(0);
315 Oid typelem = PG_GETARG_OID(1);
317 int32 typmod = PG_GETARG_INT32(2);
324 if (pg_strcasecmp(str, "NaN") == 0)
325 PG_RETURN_NUMERIC(make_result(&const_nan));
328 * Use set_var_from_str() to parse the input string and return it in the
329 * packed DB storage format
332 set_var_from_str(str, &value);
334 apply_typmod(&value, typmod);
336 res = make_result(&value);
339 PG_RETURN_NUMERIC(res);
346 * Output function for numeric data type
349 numeric_out(PG_FUNCTION_ARGS)
351 Numeric num = PG_GETARG_NUMERIC(0);
358 if (NUMERIC_IS_NAN(num))
359 PG_RETURN_CSTRING(pstrdup("NaN"));
362 * Get the number in the variable format.
364 * Even if we didn't need to change format, we'd still need to copy the
365 * value to have a modifiable copy for rounding. set_var_from_num() also
366 * guarantees there is extra digit space in case we produce a carry out
370 set_var_from_num(num, &x);
372 str = get_str_from_var(&x, x.dscale);
376 PG_RETURN_CSTRING(str);
380 * numeric_recv - converts external binary format to numeric
382 * External format is a sequence of int16's:
383 * ndigits, weight, sign, dscale, NumericDigits.
386 numeric_recv(PG_FUNCTION_ARGS)
388 StringInfo buf = (StringInfo) PG_GETARG_POINTER(0);
391 Oid typelem = PG_GETARG_OID(1);
393 int32 typmod = PG_GETARG_INT32(2);
401 len = (uint16) pq_getmsgint(buf, sizeof(uint16));
402 if (len < 0 || len > NUMERIC_MAX_PRECISION + NUMERIC_MAX_RESULT_SCALE)
404 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
405 errmsg("invalid length in external \"numeric\" value")));
407 alloc_var(&value, len);
409 value.weight = (int16) pq_getmsgint(buf, sizeof(int16));
410 value.sign = (uint16) pq_getmsgint(buf, sizeof(uint16));
411 if (!(value.sign == NUMERIC_POS ||
412 value.sign == NUMERIC_NEG ||
413 value.sign == NUMERIC_NAN))
415 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
416 errmsg("invalid sign in external \"numeric\" value")));
418 value.dscale = (uint16) pq_getmsgint(buf, sizeof(uint16));
419 for (i = 0; i < len; i++)
421 NumericDigit d = pq_getmsgint(buf, sizeof(NumericDigit));
423 if (d < 0 || d >= NBASE)
425 (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
426 errmsg("invalid digit in external \"numeric\" value")));
430 apply_typmod(&value, typmod);
432 res = make_result(&value);
435 PG_RETURN_NUMERIC(res);
439 * numeric_send - converts numeric to binary format
442 numeric_send(PG_FUNCTION_ARGS)
444 Numeric num = PG_GETARG_NUMERIC(0);
450 set_var_from_num(num, &x);
452 pq_begintypsend(&buf);
454 pq_sendint(&buf, x.ndigits, sizeof(int16));
455 pq_sendint(&buf, x.weight, sizeof(int16));
456 pq_sendint(&buf, x.sign, sizeof(int16));
457 pq_sendint(&buf, x.dscale, sizeof(int16));
458 for (i = 0; i < x.ndigits; i++)
459 pq_sendint(&buf, x.digits[i], sizeof(NumericDigit));
463 PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
470 * This is a special function called by the Postgres database system
471 * before a value is stored in a tuple's attribute. The precision and
472 * scale of the attribute have to be applied on the value.
475 numeric(PG_FUNCTION_ARGS)
477 Numeric num = PG_GETARG_NUMERIC(0);
478 int32 typmod = PG_GETARG_INT32(1);
490 if (NUMERIC_IS_NAN(num))
491 PG_RETURN_NUMERIC(make_result(&const_nan));
494 * If the value isn't a valid type modifier, simply return a copy of the
497 if (typmod < (int32) (VARHDRSZ))
499 new = (Numeric) palloc(VARSIZE(num));
500 memcpy(new, num, VARSIZE(num));
501 PG_RETURN_NUMERIC(new);
505 * Get the precision and scale out of the typmod value
507 tmp_typmod = typmod - VARHDRSZ;
508 precision = (tmp_typmod >> 16) & 0xffff;
509 scale = tmp_typmod & 0xffff;
510 maxdigits = precision - scale;
513 * If the number is certainly in bounds and due to the target scale no
514 * rounding could be necessary, just make a copy of the input and modify
515 * its scale fields. (Note we assume the existing dscale is honest...)
517 ddigits = (num->n_weight + 1) * DEC_DIGITS;
518 if (ddigits <= maxdigits && scale >= NUMERIC_DSCALE(num))
520 new = (Numeric) palloc(VARSIZE(num));
521 memcpy(new, num, VARSIZE(num));
522 new->n_sign_dscale = NUMERIC_SIGN(new) |
523 ((uint16) scale & NUMERIC_DSCALE_MASK);
524 PG_RETURN_NUMERIC(new);
528 * We really need to fiddle with things - unpack the number into a
529 * variable and let apply_typmod() do it.
533 set_var_from_num(num, &var);
534 apply_typmod(&var, typmod);
535 new = make_result(&var);
539 PG_RETURN_NUMERIC(new);
543 numerictypmodin(PG_FUNCTION_ARGS)
545 ArrayType *ta = PG_GETARG_ARRAYTYPE_P(0);
550 tl = ArrayGetIntegerTypmods(ta, &n);
554 if (tl[0] < 1 || tl[0] > NUMERIC_MAX_PRECISION)
556 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
557 errmsg("NUMERIC precision %d must be between 1 and %d",
558 tl[0], NUMERIC_MAX_PRECISION)));
559 if (tl[1] < 0 || tl[1] > tl[0])
561 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
562 errmsg("NUMERIC scale %d must be between 0 and precision %d",
564 typmod = ((tl[0] << 16) | tl[1]) + VARHDRSZ;
568 if (tl[0] < 1 || tl[0] > NUMERIC_MAX_PRECISION)
570 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
571 errmsg("NUMERIC precision %d must be between 1 and %d",
572 tl[0], NUMERIC_MAX_PRECISION)));
573 /* scale defaults to zero */
574 typmod = (tl[0] << 16) + VARHDRSZ;
579 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
580 errmsg("invalid NUMERIC type modifier")));
581 typmod = 0; /* keep compiler quiet */
584 PG_RETURN_INT32(typmod);
588 numerictypmodout(PG_FUNCTION_ARGS)
590 int32 typmod = PG_GETARG_INT32(0);
591 char *res = (char *) palloc(64);
594 snprintf(res, 64, "(%d,%d)",
595 ((typmod - VARHDRSZ) >> 16) & 0xffff,
596 (typmod - VARHDRSZ) & 0xffff);
600 PG_RETURN_CSTRING(res);
604 /* ----------------------------------------------------------------------
606 * Sign manipulation, rounding and the like
608 * ----------------------------------------------------------------------
612 numeric_abs(PG_FUNCTION_ARGS)
614 Numeric num = PG_GETARG_NUMERIC(0);
620 if (NUMERIC_IS_NAN(num))
621 PG_RETURN_NUMERIC(make_result(&const_nan));
624 * Do it the easy way directly on the packed format
626 res = (Numeric) palloc(VARSIZE(num));
627 memcpy(res, num, VARSIZE(num));
629 res->n_sign_dscale = NUMERIC_POS | NUMERIC_DSCALE(num);
631 PG_RETURN_NUMERIC(res);
636 numeric_uminus(PG_FUNCTION_ARGS)
638 Numeric num = PG_GETARG_NUMERIC(0);
644 if (NUMERIC_IS_NAN(num))
645 PG_RETURN_NUMERIC(make_result(&const_nan));
648 * Do it the easy way directly on the packed format
650 res = (Numeric) palloc(VARSIZE(num));
651 memcpy(res, num, VARSIZE(num));
654 * The packed format is known to be totally zero digit trimmed always. So
655 * we can identify a ZERO by the fact that there are no digits at all. Do
658 if (VARSIZE(num) != NUMERIC_HDRSZ)
660 /* Else, flip the sign */
661 if (NUMERIC_SIGN(num) == NUMERIC_POS)
662 res->n_sign_dscale = NUMERIC_NEG | NUMERIC_DSCALE(num);
664 res->n_sign_dscale = NUMERIC_POS | NUMERIC_DSCALE(num);
667 PG_RETURN_NUMERIC(res);
672 numeric_uplus(PG_FUNCTION_ARGS)
674 Numeric num = PG_GETARG_NUMERIC(0);
677 res = (Numeric) palloc(VARSIZE(num));
678 memcpy(res, num, VARSIZE(num));
680 PG_RETURN_NUMERIC(res);
686 * returns -1 if the argument is less than 0, 0 if the argument is equal
687 * to 0, and 1 if the argument is greater than zero.
690 numeric_sign(PG_FUNCTION_ARGS)
692 Numeric num = PG_GETARG_NUMERIC(0);
699 if (NUMERIC_IS_NAN(num))
700 PG_RETURN_NUMERIC(make_result(&const_nan));
705 * The packed format is known to be totally zero digit trimmed always. So
706 * we can identify a ZERO by the fact that there are no digits at all.
708 if (VARSIZE(num) == NUMERIC_HDRSZ)
709 set_var_from_var(&const_zero, &result);
713 * And if there are some, we return a copy of ONE with the sign of our
716 set_var_from_var(&const_one, &result);
717 result.sign = NUMERIC_SIGN(num);
720 res = make_result(&result);
723 PG_RETURN_NUMERIC(res);
730 * Round a value to have 'scale' digits after the decimal point.
731 * We allow negative 'scale', implying rounding before the decimal
732 * point --- Oracle interprets rounding that way.
735 numeric_round(PG_FUNCTION_ARGS)
737 Numeric num = PG_GETARG_NUMERIC(0);
738 int32 scale = PG_GETARG_INT32(1);
745 if (NUMERIC_IS_NAN(num))
746 PG_RETURN_NUMERIC(make_result(&const_nan));
749 * Limit the scale value to avoid possible overflow in calculations
751 scale = Max(scale, -NUMERIC_MAX_RESULT_SCALE);
752 scale = Min(scale, NUMERIC_MAX_RESULT_SCALE);
755 * Unpack the argument and round it at the proper digit position
758 set_var_from_num(num, &arg);
760 round_var(&arg, scale);
762 /* We don't allow negative output dscale */
767 * Return the rounded result
769 res = make_result(&arg);
772 PG_RETURN_NUMERIC(res);
779 * Truncate a value to have 'scale' digits after the decimal point.
780 * We allow negative 'scale', implying a truncation before the decimal
781 * point --- Oracle interprets truncation that way.
784 numeric_trunc(PG_FUNCTION_ARGS)
786 Numeric num = PG_GETARG_NUMERIC(0);
787 int32 scale = PG_GETARG_INT32(1);
794 if (NUMERIC_IS_NAN(num))
795 PG_RETURN_NUMERIC(make_result(&const_nan));
798 * Limit the scale value to avoid possible overflow in calculations
800 scale = Max(scale, -NUMERIC_MAX_RESULT_SCALE);
801 scale = Min(scale, NUMERIC_MAX_RESULT_SCALE);
804 * Unpack the argument and truncate it at the proper digit position
807 set_var_from_num(num, &arg);
809 trunc_var(&arg, scale);
811 /* We don't allow negative output dscale */
816 * Return the truncated result
818 res = make_result(&arg);
821 PG_RETURN_NUMERIC(res);
828 * Return the smallest integer greater than or equal to the argument
831 numeric_ceil(PG_FUNCTION_ARGS)
833 Numeric num = PG_GETARG_NUMERIC(0);
837 if (NUMERIC_IS_NAN(num))
838 PG_RETURN_NUMERIC(make_result(&const_nan));
842 set_var_from_num(num, &result);
843 ceil_var(&result, &result);
845 res = make_result(&result);
848 PG_RETURN_NUMERIC(res);
855 * Return the largest integer equal to or less than the argument
858 numeric_floor(PG_FUNCTION_ARGS)
860 Numeric num = PG_GETARG_NUMERIC(0);
864 if (NUMERIC_IS_NAN(num))
865 PG_RETURN_NUMERIC(make_result(&const_nan));
869 set_var_from_num(num, &result);
870 floor_var(&result, &result);
872 res = make_result(&result);
875 PG_RETURN_NUMERIC(res);
879 * Implements the numeric version of the width_bucket() function
880 * defined by SQL2003. See also width_bucket_float8().
882 * 'bound1' and 'bound2' are the lower and upper bounds of the
883 * histogram's range, respectively. 'count' is the number of buckets
884 * in the histogram. width_bucket() returns an integer indicating the
885 * bucket number that 'operand' belongs to in an equiwidth histogram
886 * with the specified characteristics. An operand smaller than the
887 * lower bound is assigned to bucket 0. An operand greater than the
888 * upper bound is assigned to an additional bucket (with number
889 * count+1). We don't allow "NaN" for any of the numeric arguments.
892 width_bucket_numeric(PG_FUNCTION_ARGS)
894 Numeric operand = PG_GETARG_NUMERIC(0);
895 Numeric bound1 = PG_GETARG_NUMERIC(1);
896 Numeric bound2 = PG_GETARG_NUMERIC(2);
897 int32 count = PG_GETARG_INT32(3);
898 NumericVar count_var;
899 NumericVar result_var;
904 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
905 errmsg("count must be greater than zero")));
907 if (NUMERIC_IS_NAN(operand) ||
908 NUMERIC_IS_NAN(bound1) ||
909 NUMERIC_IS_NAN(bound2))
911 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
912 errmsg("operand, lower bound and upper bound cannot be NaN")));
914 init_var(&result_var);
915 init_var(&count_var);
917 /* Convert 'count' to a numeric, for ease of use later */
918 int8_to_numericvar((int64) count, &count_var);
920 switch (cmp_numerics(bound1, bound2))
924 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_WIDTH_BUCKET_FUNCTION),
925 errmsg("lower bound cannot equal upper bound")));
927 /* bound1 < bound2 */
929 if (cmp_numerics(operand, bound1) < 0)
930 set_var_from_var(&const_zero, &result_var);
931 else if (cmp_numerics(operand, bound2) >= 0)
932 add_var(&count_var, &const_one, &result_var);
934 compute_bucket(operand, bound1, bound2,
935 &count_var, &result_var);
938 /* bound1 > bound2 */
940 if (cmp_numerics(operand, bound1) > 0)
941 set_var_from_var(&const_zero, &result_var);
942 else if (cmp_numerics(operand, bound2) <= 0)
943 add_var(&count_var, &const_one, &result_var);
945 compute_bucket(operand, bound1, bound2,
946 &count_var, &result_var);
950 /* if result exceeds the range of a legal int4, we ereport here */
951 result = numericvar_to_int4(&result_var);
953 free_var(&count_var);
954 free_var(&result_var);
956 PG_RETURN_INT32(result);
960 * If 'operand' is not outside the bucket range, determine the correct
961 * bucket for it to go. The calculations performed by this function
962 * are derived directly from the SQL2003 spec.
965 compute_bucket(Numeric operand, Numeric bound1, Numeric bound2,
966 NumericVar *count_var, NumericVar *result_var)
968 NumericVar bound1_var;
969 NumericVar bound2_var;
970 NumericVar operand_var;
972 init_var(&bound1_var);
973 init_var(&bound2_var);
974 init_var(&operand_var);
976 set_var_from_num(bound1, &bound1_var);
977 set_var_from_num(bound2, &bound2_var);
978 set_var_from_num(operand, &operand_var);
980 if (cmp_var(&bound1_var, &bound2_var) < 0)
982 sub_var(&operand_var, &bound1_var, &operand_var);
983 sub_var(&bound2_var, &bound1_var, &bound2_var);
984 div_var(&operand_var, &bound2_var, result_var,
985 select_div_scale(&operand_var, &bound2_var), true);
989 sub_var(&bound1_var, &operand_var, &operand_var);
990 sub_var(&bound1_var, &bound2_var, &bound1_var);
991 div_var(&operand_var, &bound1_var, result_var,
992 select_div_scale(&operand_var, &bound1_var), true);
995 mul_var(result_var, count_var, result_var,
996 result_var->dscale + count_var->dscale);
997 add_var(result_var, &const_one, result_var);
998 floor_var(result_var, result_var);
1000 free_var(&bound1_var);
1001 free_var(&bound2_var);
1002 free_var(&operand_var);
1005 /* ----------------------------------------------------------------------
1007 * Comparison functions
1009 * Note: btree indexes need these routines not to leak memory; therefore,
1010 * be careful to free working copies of toasted datums. Most places don't
1011 * need to be so careful.
1012 * ----------------------------------------------------------------------
1017 numeric_cmp(PG_FUNCTION_ARGS)
1019 Numeric num1 = PG_GETARG_NUMERIC(0);
1020 Numeric num2 = PG_GETARG_NUMERIC(1);
1023 result = cmp_numerics(num1, num2);
1025 PG_FREE_IF_COPY(num1, 0);
1026 PG_FREE_IF_COPY(num2, 1);
1028 PG_RETURN_INT32(result);
1033 numeric_eq(PG_FUNCTION_ARGS)
1035 Numeric num1 = PG_GETARG_NUMERIC(0);
1036 Numeric num2 = PG_GETARG_NUMERIC(1);
1039 result = cmp_numerics(num1, num2) == 0;
1041 PG_FREE_IF_COPY(num1, 0);
1042 PG_FREE_IF_COPY(num2, 1);
1044 PG_RETURN_BOOL(result);
1048 numeric_ne(PG_FUNCTION_ARGS)
1050 Numeric num1 = PG_GETARG_NUMERIC(0);
1051 Numeric num2 = PG_GETARG_NUMERIC(1);
1054 result = cmp_numerics(num1, num2) != 0;
1056 PG_FREE_IF_COPY(num1, 0);
1057 PG_FREE_IF_COPY(num2, 1);
1059 PG_RETURN_BOOL(result);
1063 numeric_gt(PG_FUNCTION_ARGS)
1065 Numeric num1 = PG_GETARG_NUMERIC(0);
1066 Numeric num2 = PG_GETARG_NUMERIC(1);
1069 result = cmp_numerics(num1, num2) > 0;
1071 PG_FREE_IF_COPY(num1, 0);
1072 PG_FREE_IF_COPY(num2, 1);
1074 PG_RETURN_BOOL(result);
1078 numeric_ge(PG_FUNCTION_ARGS)
1080 Numeric num1 = PG_GETARG_NUMERIC(0);
1081 Numeric num2 = PG_GETARG_NUMERIC(1);
1084 result = cmp_numerics(num1, num2) >= 0;
1086 PG_FREE_IF_COPY(num1, 0);
1087 PG_FREE_IF_COPY(num2, 1);
1089 PG_RETURN_BOOL(result);
1093 numeric_lt(PG_FUNCTION_ARGS)
1095 Numeric num1 = PG_GETARG_NUMERIC(0);
1096 Numeric num2 = PG_GETARG_NUMERIC(1);
1099 result = cmp_numerics(num1, num2) < 0;
1101 PG_FREE_IF_COPY(num1, 0);
1102 PG_FREE_IF_COPY(num2, 1);
1104 PG_RETURN_BOOL(result);
1108 numeric_le(PG_FUNCTION_ARGS)
1110 Numeric num1 = PG_GETARG_NUMERIC(0);
1111 Numeric num2 = PG_GETARG_NUMERIC(1);
1114 result = cmp_numerics(num1, num2) <= 0;
1116 PG_FREE_IF_COPY(num1, 0);
1117 PG_FREE_IF_COPY(num2, 1);
1119 PG_RETURN_BOOL(result);
1123 cmp_numerics(Numeric num1, Numeric num2)
1128 * We consider all NANs to be equal and larger than any non-NAN. This is
1129 * somewhat arbitrary; the important thing is to have a consistent sort
1132 if (NUMERIC_IS_NAN(num1))
1134 if (NUMERIC_IS_NAN(num2))
1135 result = 0; /* NAN = NAN */
1137 result = 1; /* NAN > non-NAN */
1139 else if (NUMERIC_IS_NAN(num2))
1141 result = -1; /* non-NAN < NAN */
1145 result = cmp_var_common(NUMERIC_DIGITS(num1), NUMERIC_NDIGITS(num1),
1146 num1->n_weight, NUMERIC_SIGN(num1),
1147 NUMERIC_DIGITS(num2), NUMERIC_NDIGITS(num2),
1148 num2->n_weight, NUMERIC_SIGN(num2));
1155 hash_numeric(PG_FUNCTION_ARGS)
1157 Numeric key = PG_GETARG_NUMERIC(0);
1166 /* If it's NaN, don't try to hash the rest of the fields */
1167 if (NUMERIC_IS_NAN(key))
1168 PG_RETURN_UINT32(0);
1170 weight = key->n_weight;
1175 * Omit any leading or trailing zeros from the input to the
1176 * hash. The numeric implementation *should* guarantee that
1177 * leading and trailing zeros are suppressed, but we're
1178 * paranoid. Note that we measure the starting and ending offsets
1179 * in units of NumericDigits, not bytes.
1181 for (i = 0; i < NUMERIC_NDIGITS(key); i++)
1183 if (NUMERIC_DIGITS(key)[i] != (NumericDigit) 0)
1188 * The weight is effectively the # of digits before the
1189 * decimal point, so decrement it for each leading zero we
1196 * If there are no non-zero digits, then the value of the number
1197 * is zero, regardless of any other fields.
1199 if (NUMERIC_NDIGITS(key) == start_offset)
1200 PG_RETURN_UINT32(-1);
1202 for (i = NUMERIC_NDIGITS(key) - 1; i >= 0; i--)
1204 if (NUMERIC_DIGITS(key)[i] != (NumericDigit) 0)
1210 /* If we get here, there should be at least one non-zero digit */
1211 Assert(start_offset + end_offset < NUMERIC_NDIGITS(key));
1214 * Note that we don't hash on the Numeric's scale, since two
1215 * numerics can compare equal but have different scales. We also
1216 * don't hash on the sign, although we could: since a sign
1217 * difference implies inequality, this shouldn't affect correctness.
1219 hash_len = NUMERIC_NDIGITS(key) - start_offset - end_offset;
1220 digit_hash = hash_any((unsigned char *) (NUMERIC_DIGITS(key) + start_offset),
1221 hash_len * sizeof(NumericDigit));
1223 /* Mix in the weight, via XOR */
1224 result = digit_hash ^ weight;
1226 PG_RETURN_DATUM(result);
1230 /* ----------------------------------------------------------------------
1232 * Basic arithmetic functions
1234 * ----------------------------------------------------------------------
1244 numeric_add(PG_FUNCTION_ARGS)
1246 Numeric num1 = PG_GETARG_NUMERIC(0);
1247 Numeric num2 = PG_GETARG_NUMERIC(1);
1256 if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2))
1257 PG_RETURN_NUMERIC(make_result(&const_nan));
1260 * Unpack the values, let add_var() compute the result and return it.
1266 set_var_from_num(num1, &arg1);
1267 set_var_from_num(num2, &arg2);
1269 add_var(&arg1, &arg2, &result);
1271 res = make_result(&result);
1277 PG_RETURN_NUMERIC(res);
1284 * Subtract one numeric from another
1287 numeric_sub(PG_FUNCTION_ARGS)
1289 Numeric num1 = PG_GETARG_NUMERIC(0);
1290 Numeric num2 = PG_GETARG_NUMERIC(1);
1299 if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2))
1300 PG_RETURN_NUMERIC(make_result(&const_nan));
1303 * Unpack the values, let sub_var() compute the result and return it.
1309 set_var_from_num(num1, &arg1);
1310 set_var_from_num(num2, &arg2);
1312 sub_var(&arg1, &arg2, &result);
1314 res = make_result(&result);
1320 PG_RETURN_NUMERIC(res);
1327 * Calculate the product of two numerics
1330 numeric_mul(PG_FUNCTION_ARGS)
1332 Numeric num1 = PG_GETARG_NUMERIC(0);
1333 Numeric num2 = PG_GETARG_NUMERIC(1);
1342 if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2))
1343 PG_RETURN_NUMERIC(make_result(&const_nan));
1346 * Unpack the values, let mul_var() compute the result and return it.
1347 * Unlike add_var() and sub_var(), mul_var() will round its result. In the
1348 * case of numeric_mul(), which is invoked for the * operator on numerics,
1349 * we request exact representation for the product (rscale = sum(dscale of
1350 * arg1, dscale of arg2)).
1356 set_var_from_num(num1, &arg1);
1357 set_var_from_num(num2, &arg2);
1359 mul_var(&arg1, &arg2, &result, arg1.dscale + arg2.dscale);
1361 res = make_result(&result);
1367 PG_RETURN_NUMERIC(res);
1374 * Divide one numeric into another
1377 numeric_div(PG_FUNCTION_ARGS)
1379 Numeric num1 = PG_GETARG_NUMERIC(0);
1380 Numeric num2 = PG_GETARG_NUMERIC(1);
1390 if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2))
1391 PG_RETURN_NUMERIC(make_result(&const_nan));
1394 * Unpack the arguments
1400 set_var_from_num(num1, &arg1);
1401 set_var_from_num(num2, &arg2);
1404 * Select scale for division result
1406 rscale = select_div_scale(&arg1, &arg2);
1409 * Do the divide and return the result
1411 div_var(&arg1, &arg2, &result, rscale, true);
1413 res = make_result(&result);
1419 PG_RETURN_NUMERIC(res);
1426 * Calculate the modulo of two numerics
1429 numeric_mod(PG_FUNCTION_ARGS)
1431 Numeric num1 = PG_GETARG_NUMERIC(0);
1432 Numeric num2 = PG_GETARG_NUMERIC(1);
1438 if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2))
1439 PG_RETURN_NUMERIC(make_result(&const_nan));
1445 set_var_from_num(num1, &arg1);
1446 set_var_from_num(num2, &arg2);
1448 mod_var(&arg1, &arg2, &result);
1450 res = make_result(&result);
1456 PG_RETURN_NUMERIC(res);
1463 * Increment a number by one
1466 numeric_inc(PG_FUNCTION_ARGS)
1468 Numeric num = PG_GETARG_NUMERIC(0);
1475 if (NUMERIC_IS_NAN(num))
1476 PG_RETURN_NUMERIC(make_result(&const_nan));
1479 * Compute the result and return it
1483 set_var_from_num(num, &arg);
1485 add_var(&arg, &const_one, &arg);
1487 res = make_result(&arg);
1491 PG_RETURN_NUMERIC(res);
1496 * numeric_smaller() -
1498 * Return the smaller of two numbers
1501 numeric_smaller(PG_FUNCTION_ARGS)
1503 Numeric num1 = PG_GETARG_NUMERIC(0);
1504 Numeric num2 = PG_GETARG_NUMERIC(1);
1507 * Use cmp_numerics so that this will agree with the comparison operators,
1508 * particularly as regards comparisons involving NaN.
1510 if (cmp_numerics(num1, num2) < 0)
1511 PG_RETURN_NUMERIC(num1);
1513 PG_RETURN_NUMERIC(num2);
1518 * numeric_larger() -
1520 * Return the larger of two numbers
1523 numeric_larger(PG_FUNCTION_ARGS)
1525 Numeric num1 = PG_GETARG_NUMERIC(0);
1526 Numeric num2 = PG_GETARG_NUMERIC(1);
1529 * Use cmp_numerics so that this will agree with the comparison operators,
1530 * particularly as regards comparisons involving NaN.
1532 if (cmp_numerics(num1, num2) > 0)
1533 PG_RETURN_NUMERIC(num1);
1535 PG_RETURN_NUMERIC(num2);
1539 /* ----------------------------------------------------------------------
1541 * Advanced math functions
1543 * ----------------------------------------------------------------------
1552 numeric_fac(PG_FUNCTION_ARGS)
1554 int64 num = PG_GETARG_INT64(0);
1561 res = make_result(&const_one);
1562 PG_RETURN_NUMERIC(res);
1564 /* Fail immediately if the result would overflow */
1567 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1568 errmsg("value overflows numeric format")));
1573 int8_to_numericvar(num, &result);
1575 for (num = num - 1; num > 1; num--)
1577 /* this loop can take awhile, so allow it to be interrupted */
1578 CHECK_FOR_INTERRUPTS();
1580 int8_to_numericvar(num, &fact);
1582 mul_var(&result, &fact, &result, 0);
1585 res = make_result(&result);
1590 PG_RETURN_NUMERIC(res);
1597 * Compute the square root of a numeric.
1600 numeric_sqrt(PG_FUNCTION_ARGS)
1602 Numeric num = PG_GETARG_NUMERIC(0);
1612 if (NUMERIC_IS_NAN(num))
1613 PG_RETURN_NUMERIC(make_result(&const_nan));
1616 * Unpack the argument and determine the result scale. We choose a scale
1617 * to give at least NUMERIC_MIN_SIG_DIGITS significant digits; but in any
1618 * case not less than the input's dscale.
1623 set_var_from_num(num, &arg);
1625 /* Assume the input was normalized, so arg.weight is accurate */
1626 sweight = (arg.weight + 1) * DEC_DIGITS / 2 - 1;
1628 rscale = NUMERIC_MIN_SIG_DIGITS - sweight;
1629 rscale = Max(rscale, arg.dscale);
1630 rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
1631 rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
1634 * Let sqrt_var() do the calculation and return the result.
1636 sqrt_var(&arg, &result, rscale);
1638 res = make_result(&result);
1643 PG_RETURN_NUMERIC(res);
1650 * Raise e to the power of x
1653 numeric_exp(PG_FUNCTION_ARGS)
1655 Numeric num = PG_GETARG_NUMERIC(0);
1665 if (NUMERIC_IS_NAN(num))
1666 PG_RETURN_NUMERIC(make_result(&const_nan));
1669 * Unpack the argument and determine the result scale. We choose a scale
1670 * to give at least NUMERIC_MIN_SIG_DIGITS significant digits; but in any
1671 * case not less than the input's dscale.
1676 set_var_from_num(num, &arg);
1678 /* convert input to float8, ignoring overflow */
1679 val = numericvar_to_double_no_overflow(&arg);
1682 * log10(result) = num * log10(e), so this is approximately the decimal
1683 * weight of the result:
1685 val *= 0.434294481903252;
1687 /* limit to something that won't cause integer overflow */
1688 val = Max(val, -NUMERIC_MAX_RESULT_SCALE);
1689 val = Min(val, NUMERIC_MAX_RESULT_SCALE);
1691 rscale = NUMERIC_MIN_SIG_DIGITS - (int) val;
1692 rscale = Max(rscale, arg.dscale);
1693 rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
1694 rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
1697 * Let exp_var() do the calculation and return the result.
1699 exp_var(&arg, &result, rscale);
1701 res = make_result(&result);
1706 PG_RETURN_NUMERIC(res);
1713 * Compute the natural logarithm of x
1716 numeric_ln(PG_FUNCTION_ARGS)
1718 Numeric num = PG_GETARG_NUMERIC(0);
1728 if (NUMERIC_IS_NAN(num))
1729 PG_RETURN_NUMERIC(make_result(&const_nan));
1734 set_var_from_num(num, &arg);
1736 /* Approx decimal digits before decimal point */
1737 dec_digits = (arg.weight + 1) * DEC_DIGITS;
1740 rscale = NUMERIC_MIN_SIG_DIGITS - (int) log10(dec_digits - 1);
1741 else if (dec_digits < 1)
1742 rscale = NUMERIC_MIN_SIG_DIGITS - (int) log10(1 - dec_digits);
1744 rscale = NUMERIC_MIN_SIG_DIGITS;
1746 rscale = Max(rscale, arg.dscale);
1747 rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
1748 rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
1750 ln_var(&arg, &result, rscale);
1752 res = make_result(&result);
1757 PG_RETURN_NUMERIC(res);
1764 * Compute the logarithm of x in a given base
1767 numeric_log(PG_FUNCTION_ARGS)
1769 Numeric num1 = PG_GETARG_NUMERIC(0);
1770 Numeric num2 = PG_GETARG_NUMERIC(1);
1779 if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2))
1780 PG_RETURN_NUMERIC(make_result(&const_nan));
1789 set_var_from_num(num1, &arg1);
1790 set_var_from_num(num2, &arg2);
1793 * Call log_var() to compute and return the result; note it handles scale
1796 log_var(&arg1, &arg2, &result);
1798 res = make_result(&result);
1804 PG_RETURN_NUMERIC(res);
1811 * Raise b to the power of x
1814 numeric_power(PG_FUNCTION_ARGS)
1816 Numeric num1 = PG_GETARG_NUMERIC(0);
1817 Numeric num2 = PG_GETARG_NUMERIC(1);
1821 NumericVar arg2_trunc;
1827 if (NUMERIC_IS_NAN(num1) || NUMERIC_IS_NAN(num2))
1828 PG_RETURN_NUMERIC(make_result(&const_nan));
1835 init_var(&arg2_trunc);
1838 set_var_from_num(num1, &arg1);
1839 set_var_from_num(num2, &arg2);
1840 set_var_from_var(&arg2, &arg2_trunc);
1842 trunc_var(&arg2_trunc, 0);
1845 * Return special SQLSTATE error codes for a few conditions mandated by
1848 if ((cmp_var(&arg1, &const_zero) == 0 &&
1849 cmp_var(&arg2, &const_zero) < 0) ||
1850 (cmp_var(&arg1, &const_zero) < 0 &&
1851 cmp_var(&arg2, &arg2_trunc) != 0))
1853 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
1854 errmsg("invalid argument for power function")));
1857 * Call power_var() to compute and return the result; note it handles
1858 * scale selection itself.
1860 power_var(&arg1, &arg2, &result);
1862 res = make_result(&result);
1866 free_var(&arg2_trunc);
1869 PG_RETURN_NUMERIC(res);
1873 /* ----------------------------------------------------------------------
1875 * Type conversion functions
1877 * ----------------------------------------------------------------------
1882 int4_numeric(PG_FUNCTION_ARGS)
1884 int32 val = PG_GETARG_INT32(0);
1890 int8_to_numericvar((int64) val, &result);
1892 res = make_result(&result);
1896 PG_RETURN_NUMERIC(res);
1901 numeric_int4(PG_FUNCTION_ARGS)
1903 Numeric num = PG_GETARG_NUMERIC(0);
1907 /* XXX would it be better to return NULL? */
1908 if (NUMERIC_IS_NAN(num))
1910 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1911 errmsg("cannot convert NaN to integer")));
1913 /* Convert to variable format, then convert to int4 */
1915 set_var_from_num(num, &x);
1916 result = numericvar_to_int4(&x);
1918 PG_RETURN_INT32(result);
1922 * Given a NumericVar, convert it to an int32. If the NumericVar
1923 * exceeds the range of an int32, raise the appropriate error via
1924 * ereport(). The input NumericVar is *not* free'd.
1927 numericvar_to_int4(NumericVar *var)
1932 if (!numericvar_to_int8(var, &val))
1934 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1935 errmsg("integer out of range")));
1937 /* Down-convert to int4 */
1938 result = (int32) val;
1940 /* Test for overflow by reverse-conversion. */
1941 if ((int64) result != val)
1943 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1944 errmsg("integer out of range")));
1950 int8_numeric(PG_FUNCTION_ARGS)
1952 int64 val = PG_GETARG_INT64(0);
1958 int8_to_numericvar(val, &result);
1960 res = make_result(&result);
1964 PG_RETURN_NUMERIC(res);
1969 numeric_int8(PG_FUNCTION_ARGS)
1971 Numeric num = PG_GETARG_NUMERIC(0);
1975 /* XXX would it be better to return NULL? */
1976 if (NUMERIC_IS_NAN(num))
1978 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1979 errmsg("cannot convert NaN to bigint")));
1981 /* Convert to variable format and thence to int8 */
1983 set_var_from_num(num, &x);
1985 if (!numericvar_to_int8(&x, &result))
1987 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1988 errmsg("bigint out of range")));
1992 PG_RETURN_INT64(result);
1997 int2_numeric(PG_FUNCTION_ARGS)
1999 int16 val = PG_GETARG_INT16(0);
2005 int8_to_numericvar((int64) val, &result);
2007 res = make_result(&result);
2011 PG_RETURN_NUMERIC(res);
2016 numeric_int2(PG_FUNCTION_ARGS)
2018 Numeric num = PG_GETARG_NUMERIC(0);
2023 /* XXX would it be better to return NULL? */
2024 if (NUMERIC_IS_NAN(num))
2026 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
2027 errmsg("cannot convert NaN to smallint")));
2029 /* Convert to variable format and thence to int8 */
2031 set_var_from_num(num, &x);
2033 if (!numericvar_to_int8(&x, &val))
2035 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2036 errmsg("smallint out of range")));
2040 /* Down-convert to int2 */
2041 result = (int16) val;
2043 /* Test for overflow by reverse-conversion. */
2044 if ((int64) result != val)
2046 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
2047 errmsg("smallint out of range")));
2049 PG_RETURN_INT16(result);
2054 float8_numeric(PG_FUNCTION_ARGS)
2056 float8 val = PG_GETARG_FLOAT8(0);
2059 char buf[DBL_DIG + 100];
2062 PG_RETURN_NUMERIC(make_result(&const_nan));
2064 sprintf(buf, "%.*g", DBL_DIG, val);
2068 set_var_from_str(buf, &result);
2069 res = make_result(&result);
2073 PG_RETURN_NUMERIC(res);
2078 numeric_float8(PG_FUNCTION_ARGS)
2080 Numeric num = PG_GETARG_NUMERIC(0);
2084 if (NUMERIC_IS_NAN(num))
2085 PG_RETURN_FLOAT8(get_float8_nan());
2087 tmp = DatumGetCString(DirectFunctionCall1(numeric_out,
2088 NumericGetDatum(num)));
2090 result = DirectFunctionCall1(float8in, CStringGetDatum(tmp));
2094 PG_RETURN_DATUM(result);
2098 /* Convert numeric to float8; if out of range, return +/- HUGE_VAL */
2100 numeric_float8_no_overflow(PG_FUNCTION_ARGS)
2102 Numeric num = PG_GETARG_NUMERIC(0);
2105 if (NUMERIC_IS_NAN(num))
2106 PG_RETURN_FLOAT8(get_float8_nan());
2108 val = numeric_to_double_no_overflow(num);
2110 PG_RETURN_FLOAT8(val);
2114 float4_numeric(PG_FUNCTION_ARGS)
2116 float4 val = PG_GETARG_FLOAT4(0);
2119 char buf[FLT_DIG + 100];
2122 PG_RETURN_NUMERIC(make_result(&const_nan));
2124 sprintf(buf, "%.*g", FLT_DIG, val);
2128 set_var_from_str(buf, &result);
2129 res = make_result(&result);
2133 PG_RETURN_NUMERIC(res);
2138 numeric_float4(PG_FUNCTION_ARGS)
2140 Numeric num = PG_GETARG_NUMERIC(0);
2144 if (NUMERIC_IS_NAN(num))
2145 PG_RETURN_FLOAT4(get_float4_nan());
2147 tmp = DatumGetCString(DirectFunctionCall1(numeric_out,
2148 NumericGetDatum(num)));
2150 result = DirectFunctionCall1(float4in, CStringGetDatum(tmp));
2154 PG_RETURN_DATUM(result);
2158 /* ----------------------------------------------------------------------
2160 * Aggregate functions
2162 * The transition datatype for all these aggregates is a 3-element array
2163 * of Numeric, holding the values N, sum(X), sum(X*X) in that order.
2165 * We represent N as a numeric mainly to avoid having to build a special
2166 * datatype; it's unlikely it'd overflow an int4, but ...
2168 * ----------------------------------------------------------------------
2172 do_numeric_accum(ArrayType *transarray, Numeric newval)
2181 /* We assume the input is array of numeric */
2182 deconstruct_array(transarray,
2183 NUMERICOID, -1, false, 'i',
2184 &transdatums, NULL, &ndatums);
2186 elog(ERROR, "expected 3-element numeric array");
2188 sumX = transdatums[1];
2189 sumX2 = transdatums[2];
2191 N = DirectFunctionCall1(numeric_inc, N);
2192 sumX = DirectFunctionCall2(numeric_add, sumX,
2193 NumericGetDatum(newval));
2194 sumX2 = DirectFunctionCall2(numeric_add, sumX2,
2195 DirectFunctionCall2(numeric_mul,
2196 NumericGetDatum(newval),
2197 NumericGetDatum(newval)));
2200 transdatums[1] = sumX;
2201 transdatums[2] = sumX2;
2203 result = construct_array(transdatums, 3,
2204 NUMERICOID, -1, false, 'i');
2210 * Improve avg performance by not caclulating sum(X*X).
2213 do_numeric_avg_accum(ArrayType *transarray, Numeric newval)
2221 /* We assume the input is array of numeric */
2222 deconstruct_array(transarray,
2223 NUMERICOID, -1, false, 'i',
2224 &transdatums, NULL, &ndatums);
2226 elog(ERROR, "expected 2-element numeric array");
2228 sumX = transdatums[1];
2230 N = DirectFunctionCall1(numeric_inc, N);
2231 sumX = DirectFunctionCall2(numeric_add, sumX,
2232 NumericGetDatum(newval));
2235 transdatums[1] = sumX;
2237 result = construct_array(transdatums, 2,
2238 NUMERICOID, -1, false, 'i');
2244 numeric_accum(PG_FUNCTION_ARGS)
2246 ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2247 Numeric newval = PG_GETARG_NUMERIC(1);
2249 PG_RETURN_ARRAYTYPE_P(do_numeric_accum(transarray, newval));
2253 * Optimized case for average of numeric.
2256 numeric_avg_accum(PG_FUNCTION_ARGS)
2258 ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2259 Numeric newval = PG_GETARG_NUMERIC(1);
2261 PG_RETURN_ARRAYTYPE_P(do_numeric_avg_accum(transarray, newval));
2265 * Integer data types all use Numeric accumulators to share code and
2266 * avoid risk of overflow. For int2 and int4 inputs, Numeric accumulation
2267 * is overkill for the N and sum(X) values, but definitely not overkill
2268 * for the sum(X*X) value. Hence, we use int2_accum and int4_accum only
2269 * for stddev/variance --- there are faster special-purpose accumulator
2270 * routines for SUM and AVG of these datatypes.
2274 int2_accum(PG_FUNCTION_ARGS)
2276 ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2277 Datum newval2 = PG_GETARG_DATUM(1);
2280 newval = DatumGetNumeric(DirectFunctionCall1(int2_numeric, newval2));
2282 PG_RETURN_ARRAYTYPE_P(do_numeric_accum(transarray, newval));
2286 int4_accum(PG_FUNCTION_ARGS)
2288 ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2289 Datum newval4 = PG_GETARG_DATUM(1);
2292 newval = DatumGetNumeric(DirectFunctionCall1(int4_numeric, newval4));
2294 PG_RETURN_ARRAYTYPE_P(do_numeric_accum(transarray, newval));
2298 int8_accum(PG_FUNCTION_ARGS)
2300 ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2301 Datum newval8 = PG_GETARG_DATUM(1);
2304 newval = DatumGetNumeric(DirectFunctionCall1(int8_numeric, newval8));
2306 PG_RETURN_ARRAYTYPE_P(do_numeric_accum(transarray, newval));
2310 * Optimized case for average of int8.
2313 int8_avg_accum(PG_FUNCTION_ARGS)
2315 ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2316 Datum newval8 = PG_GETARG_DATUM(1);
2319 newval = DatumGetNumeric(DirectFunctionCall1(int8_numeric, newval8));
2321 PG_RETURN_ARRAYTYPE_P(do_numeric_avg_accum(transarray, newval));
2326 numeric_avg(PG_FUNCTION_ARGS)
2328 ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2334 /* We assume the input is array of numeric */
2335 deconstruct_array(transarray,
2336 NUMERICOID, -1, false, 'i',
2337 &transdatums, NULL, &ndatums);
2339 elog(ERROR, "expected 2-element numeric array");
2340 N = DatumGetNumeric(transdatums[0]);
2341 sumX = DatumGetNumeric(transdatums[1]);
2343 /* SQL92 defines AVG of no values to be NULL */
2344 /* N is zero iff no digits (cf. numeric_uminus) */
2345 if (VARSIZE(N) == NUMERIC_HDRSZ)
2348 PG_RETURN_DATUM(DirectFunctionCall2(numeric_div,
2349 NumericGetDatum(sumX),
2350 NumericGetDatum(N)));
2354 * Workhorse routine for the standard deviance and variance
2355 * aggregates. 'transarray' is the aggregate's transition
2356 * array. 'variance' specifies whether we should calculate the
2357 * variance or the standard deviation. 'sample' indicates whether the
2358 * caller is interested in the sample or the population
2361 * If appropriate variance statistic is undefined for the input,
2362 * *is_null is set to true and NULL is returned.
2365 numeric_stddev_internal(ArrayType *transarray,
2366 bool variance, bool sample,
2384 /* We assume the input is array of numeric */
2385 deconstruct_array(transarray,
2386 NUMERICOID, -1, false, 'i',
2387 &transdatums, NULL, &ndatums);
2389 elog(ERROR, "expected 3-element numeric array");
2390 N = DatumGetNumeric(transdatums[0]);
2391 sumX = DatumGetNumeric(transdatums[1]);
2392 sumX2 = DatumGetNumeric(transdatums[2]);
2394 if (NUMERIC_IS_NAN(N) || NUMERIC_IS_NAN(sumX) || NUMERIC_IS_NAN(sumX2))
2395 return make_result(&const_nan);
2398 set_var_from_num(N, &vN);
2401 * Sample stddev and variance are undefined when N <= 1; population stddev
2402 * is undefined when N == 0. Return NULL in either case.
2409 if (cmp_var(&vN, comp) <= 0)
2416 init_var(&vNminus1);
2417 sub_var(&vN, &const_one, &vNminus1);
2420 set_var_from_num(sumX, &vsumX);
2422 set_var_from_num(sumX2, &vsumX2);
2424 /* compute rscale for mul_var calls */
2425 rscale = vsumX.dscale * 2;
2427 mul_var(&vsumX, &vsumX, &vsumX, rscale); /* vsumX = sumX * sumX */
2428 mul_var(&vN, &vsumX2, &vsumX2, rscale); /* vsumX2 = N * sumX2 */
2429 sub_var(&vsumX2, &vsumX, &vsumX2); /* N * sumX2 - sumX * sumX */
2431 if (cmp_var(&vsumX2, &const_zero) <= 0)
2433 /* Watch out for roundoff error producing a negative numerator */
2434 res = make_result(&const_zero);
2438 mul_var(&vN, &vNminus1, &vNminus1, 0); /* N * (N - 1) */
2439 rscale = select_div_scale(&vsumX2, &vNminus1);
2440 div_var(&vsumX2, &vNminus1, &vsumX, rscale, true); /* variance */
2442 sqrt_var(&vsumX, &vsumX, rscale); /* stddev */
2444 res = make_result(&vsumX);
2448 free_var(&vNminus1);
2456 numeric_var_samp(PG_FUNCTION_ARGS)
2461 res = numeric_stddev_internal(PG_GETARG_ARRAYTYPE_P(0),
2462 true, true, &is_null);
2467 PG_RETURN_NUMERIC(res);
2471 numeric_stddev_samp(PG_FUNCTION_ARGS)
2476 res = numeric_stddev_internal(PG_GETARG_ARRAYTYPE_P(0),
2477 false, true, &is_null);
2482 PG_RETURN_NUMERIC(res);
2486 numeric_var_pop(PG_FUNCTION_ARGS)
2491 res = numeric_stddev_internal(PG_GETARG_ARRAYTYPE_P(0),
2492 true, false, &is_null);
2497 PG_RETURN_NUMERIC(res);
2501 numeric_stddev_pop(PG_FUNCTION_ARGS)
2506 res = numeric_stddev_internal(PG_GETARG_ARRAYTYPE_P(0),
2507 false, false, &is_null);
2512 PG_RETURN_NUMERIC(res);
2516 * SUM transition functions for integer datatypes.
2518 * To avoid overflow, we use accumulators wider than the input datatype.
2519 * A Numeric accumulator is needed for int8 input; for int4 and int2
2520 * inputs, we use int8 accumulators which should be sufficient for practical
2521 * purposes. (The latter two therefore don't really belong in this file,
2522 * but we keep them here anyway.)
2524 * Because SQL92 defines the SUM() of no values to be NULL, not zero,
2525 * the initial condition of the transition data value needs to be NULL. This
2526 * means we can't rely on ExecAgg to automatically insert the first non-null
2527 * data value into the transition data: it doesn't know how to do the type
2528 * conversion. The upshot is that these routines have to be marked non-strict
2529 * and handle substitution of the first non-null input themselves.
2533 int2_sum(PG_FUNCTION_ARGS)
2537 if (PG_ARGISNULL(0))
2539 /* No non-null input seen so far... */
2540 if (PG_ARGISNULL(1))
2541 PG_RETURN_NULL(); /* still no non-null */
2542 /* This is the first non-null input. */
2543 newval = (int64) PG_GETARG_INT16(1);
2544 PG_RETURN_INT64(newval);
2548 * If we're invoked by nodeAgg, we can cheat and modify out first
2549 * parameter in-place to avoid palloc overhead. If not, we need to return
2550 * the new value of the transition variable.
2552 if (fcinfo->context && IsA(fcinfo->context, AggState))
2554 int64 *oldsum = (int64 *) PG_GETARG_POINTER(0);
2556 /* Leave the running sum unchanged in the new input is null */
2557 if (!PG_ARGISNULL(1))
2558 *oldsum = *oldsum + (int64) PG_GETARG_INT16(1);
2560 PG_RETURN_POINTER(oldsum);
2564 int64 oldsum = PG_GETARG_INT64(0);
2566 /* Leave sum unchanged if new input is null. */
2567 if (PG_ARGISNULL(1))
2568 PG_RETURN_INT64(oldsum);
2570 /* OK to do the addition. */
2571 newval = oldsum + (int64) PG_GETARG_INT16(1);
2573 PG_RETURN_INT64(newval);
2578 int4_sum(PG_FUNCTION_ARGS)
2582 if (PG_ARGISNULL(0))
2584 /* No non-null input seen so far... */
2585 if (PG_ARGISNULL(1))
2586 PG_RETURN_NULL(); /* still no non-null */
2587 /* This is the first non-null input. */
2588 newval = (int64) PG_GETARG_INT32(1);
2589 PG_RETURN_INT64(newval);
2593 * If we're invoked by nodeAgg, we can cheat and modify out first
2594 * parameter in-place to avoid palloc overhead. If not, we need to return
2595 * the new value of the transition variable.
2597 if (fcinfo->context && IsA(fcinfo->context, AggState))
2599 int64 *oldsum = (int64 *) PG_GETARG_POINTER(0);
2601 /* Leave the running sum unchanged in the new input is null */
2602 if (!PG_ARGISNULL(1))
2603 *oldsum = *oldsum + (int64) PG_GETARG_INT32(1);
2605 PG_RETURN_POINTER(oldsum);
2609 int64 oldsum = PG_GETARG_INT64(0);
2611 /* Leave sum unchanged if new input is null. */
2612 if (PG_ARGISNULL(1))
2613 PG_RETURN_INT64(oldsum);
2615 /* OK to do the addition. */
2616 newval = oldsum + (int64) PG_GETARG_INT32(1);
2618 PG_RETURN_INT64(newval);
2623 int8_sum(PG_FUNCTION_ARGS)
2628 if (PG_ARGISNULL(0))
2630 /* No non-null input seen so far... */
2631 if (PG_ARGISNULL(1))
2632 PG_RETURN_NULL(); /* still no non-null */
2633 /* This is the first non-null input. */
2634 newval = DirectFunctionCall1(int8_numeric, PG_GETARG_DATUM(1));
2635 PG_RETURN_DATUM(newval);
2639 * Note that we cannot special-case the nodeAgg case here, as we do for
2640 * int2_sum and int4_sum: numeric is of variable size, so we cannot modify
2641 * our first parameter in-place.
2644 oldsum = PG_GETARG_NUMERIC(0);
2646 /* Leave sum unchanged if new input is null. */
2647 if (PG_ARGISNULL(1))
2648 PG_RETURN_NUMERIC(oldsum);
2650 /* OK to do the addition. */
2651 newval = DirectFunctionCall1(int8_numeric, PG_GETARG_DATUM(1));
2653 PG_RETURN_DATUM(DirectFunctionCall2(numeric_add,
2654 NumericGetDatum(oldsum), newval));
2659 * Routines for avg(int2) and avg(int4). The transition datatype
2660 * is a two-element int8 array, holding count and sum.
2663 typedef struct Int8TransTypeData
2665 #ifndef INT64_IS_BUSTED
2669 /* "int64" isn't really 64 bits, so fake up properly-aligned fields */
2675 } Int8TransTypeData;
2678 int2_avg_accum(PG_FUNCTION_ARGS)
2680 ArrayType *transarray;
2681 int16 newval = PG_GETARG_INT16(1);
2682 Int8TransTypeData *transdata;
2685 * If we're invoked by nodeAgg, we can cheat and modify our first
2686 * parameter in-place to reduce palloc overhead. Otherwise we need to make
2687 * a copy of it before scribbling on it.
2689 if (fcinfo->context && IsA(fcinfo->context, AggState))
2690 transarray = PG_GETARG_ARRAYTYPE_P(0);
2692 transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
2694 if (ARR_HASNULL(transarray) ||
2695 ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
2696 elog(ERROR, "expected 2-element int8 array");
2698 transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
2700 transdata->sum += newval;
2702 PG_RETURN_ARRAYTYPE_P(transarray);
2706 int4_avg_accum(PG_FUNCTION_ARGS)
2708 ArrayType *transarray;
2709 int32 newval = PG_GETARG_INT32(1);
2710 Int8TransTypeData *transdata;
2713 * If we're invoked by nodeAgg, we can cheat and modify our first
2714 * parameter in-place to reduce palloc overhead. Otherwise we need to make
2715 * a copy of it before scribbling on it.
2717 if (fcinfo->context && IsA(fcinfo->context, AggState))
2718 transarray = PG_GETARG_ARRAYTYPE_P(0);
2720 transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
2722 if (ARR_HASNULL(transarray) ||
2723 ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
2724 elog(ERROR, "expected 2-element int8 array");
2726 transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
2728 transdata->sum += newval;
2730 PG_RETURN_ARRAYTYPE_P(transarray);
2734 int8_avg(PG_FUNCTION_ARGS)
2736 ArrayType *transarray = PG_GETARG_ARRAYTYPE_P(0);
2737 Int8TransTypeData *transdata;
2741 if (ARR_HASNULL(transarray) ||
2742 ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
2743 elog(ERROR, "expected 2-element int8 array");
2744 transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
2746 /* SQL92 defines AVG of no values to be NULL */
2747 if (transdata->count == 0)
2750 countd = DirectFunctionCall1(int8_numeric,
2751 Int64GetDatumFast(transdata->count));
2752 sumd = DirectFunctionCall1(int8_numeric,
2753 Int64GetDatumFast(transdata->sum));
2755 PG_RETURN_DATUM(DirectFunctionCall2(numeric_div, sumd, countd));
2759 /* ----------------------------------------------------------------------
2763 * ----------------------------------------------------------------------
2766 #ifdef NUMERIC_DEBUG
2769 * dump_numeric() - Dump a value in the db storage format for debugging
2772 dump_numeric(const char *str, Numeric num)
2774 NumericDigit *digits = NUMERIC_DIGITS(num);
2778 ndigits = NUMERIC_NDIGITS(num);
2780 printf("%s: NUMERIC w=%d d=%d ", str, num->n_weight, NUMERIC_DSCALE(num));
2781 switch (NUMERIC_SIGN(num))
2793 printf("SIGN=0x%x", NUMERIC_SIGN(num));
2797 for (i = 0; i < ndigits; i++)
2798 printf(" %0*d", DEC_DIGITS, digits[i]);
2804 * dump_var() - Dump a value in the variable format for debugging
2807 dump_var(const char *str, NumericVar *var)
2811 printf("%s: VAR w=%d d=%d ", str, var->weight, var->dscale);
2824 printf("SIGN=0x%x", var->sign);
2828 for (i = 0; i < var->ndigits; i++)
2829 printf(" %0*d", DEC_DIGITS, var->digits[i]);
2833 #endif /* NUMERIC_DEBUG */
2836 /* ----------------------------------------------------------------------
2838 * Local functions follow
2840 * In general, these do not support NaNs --- callers must eliminate
2841 * the possibility of NaN first. (make_result() is an exception.)
2843 * ----------------------------------------------------------------------
2850 * Allocate a digit buffer of ndigits digits (plus a spare digit for rounding)
2853 alloc_var(NumericVar *var, int ndigits)
2855 digitbuf_free(var->buf);
2856 var->buf = digitbuf_alloc(ndigits + 1);
2857 var->buf[0] = 0; /* spare digit for rounding */
2858 var->digits = var->buf + 1;
2859 var->ndigits = ndigits;
2866 * Return the digit buffer of a variable to the free pool
2869 free_var(NumericVar *var)
2871 digitbuf_free(var->buf);
2874 var->sign = NUMERIC_NAN;
2881 * Set a variable to ZERO.
2882 * Note: its dscale is not touched.
2885 zero_var(NumericVar *var)
2887 digitbuf_free(var->buf);
2891 var->weight = 0; /* by convention; doesn't really matter */
2892 var->sign = NUMERIC_POS; /* anything but NAN... */
2897 * set_var_from_str()
2899 * Parse a string and put the number into a variable
2902 set_var_from_str(const char *str, NumericVar *dest)
2904 const char *cp = str;
2905 bool have_dp = FALSE;
2907 unsigned char *decdigits;
2908 int sign = NUMERIC_POS;
2915 NumericDigit *digits;
2918 * We first parse the string to extract decimal digits and determine the
2919 * correct decimal weight. Then convert to NBASE representation.
2922 /* skip leading spaces */
2925 if (!isspace((unsigned char) *cp))
2949 if (!isdigit((unsigned char) *cp))
2951 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
2952 errmsg("invalid input syntax for type numeric: \"%s\"", str)));
2954 decdigits = (unsigned char *) palloc(strlen(cp) + DEC_DIGITS * 2);
2956 /* leading padding for digit alignment later */
2957 memset(decdigits, 0, DEC_DIGITS);
2962 if (isdigit((unsigned char) *cp))
2964 decdigits[i++] = *cp++ - '0';
2970 else if (*cp == '.')
2974 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
2975 errmsg("invalid input syntax for type numeric: \"%s\"",
2984 ddigits = i - DEC_DIGITS;
2985 /* trailing padding for digit alignment later */
2986 memset(decdigits + i, 0, DEC_DIGITS - 1);
2988 /* Handle exponent, if any */
2989 if (*cp == 'e' || *cp == 'E')
2995 exponent = strtol(cp, &endptr, 10);
2998 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
2999 errmsg("invalid input syntax for type numeric: \"%s\"",
3002 if (exponent > NUMERIC_MAX_PRECISION ||
3003 exponent < -NUMERIC_MAX_PRECISION)
3005 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
3006 errmsg("invalid input syntax for type numeric: \"%s\"",
3008 dweight += (int) exponent;
3009 dscale -= (int) exponent;
3014 /* Should be nothing left but spaces */
3017 if (!isspace((unsigned char) *cp))
3019 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
3020 errmsg("invalid input syntax for type numeric: \"%s\"",
3026 * Okay, convert pure-decimal representation to base NBASE. First we need
3027 * to determine the converted weight and ndigits. offset is the number of
3028 * decimal zeroes to insert before the first given digit to have a
3029 * correctly aligned first NBASE digit.
3032 weight = (dweight + 1 + DEC_DIGITS - 1) / DEC_DIGITS - 1;
3034 weight = -((-dweight - 1) / DEC_DIGITS + 1);
3035 offset = (weight + 1) * DEC_DIGITS - (dweight + 1);
3036 ndigits = (ddigits + offset + DEC_DIGITS - 1) / DEC_DIGITS;
3038 alloc_var(dest, ndigits);
3040 dest->weight = weight;
3041 dest->dscale = dscale;
3043 i = DEC_DIGITS - offset;
3044 digits = dest->digits;
3046 while (ndigits-- > 0)
3049 *digits++ = ((decdigits[i] * 10 + decdigits[i + 1]) * 10 +
3050 decdigits[i + 2]) * 10 + decdigits[i + 3];
3051 #elif DEC_DIGITS == 2
3052 *digits++ = decdigits[i] * 10 + decdigits[i + 1];
3053 #elif DEC_DIGITS == 1
3054 *digits++ = decdigits[i];
3056 #error unsupported NBASE
3063 /* Strip any leading/trailing zeroes, and normalize weight if zero */
3069 * set_var_from_num() -
3071 * Convert the packed db format into a variable
3074 set_var_from_num(Numeric num, NumericVar *dest)
3078 ndigits = NUMERIC_NDIGITS(num);
3080 alloc_var(dest, ndigits);
3082 dest->weight = num->n_weight;
3083 dest->sign = NUMERIC_SIGN(num);
3084 dest->dscale = NUMERIC_DSCALE(num);
3086 memcpy(dest->digits, num->n_data, ndigits * sizeof(NumericDigit));
3091 * set_var_from_var() -
3093 * Copy one variable into another
3096 set_var_from_var(NumericVar *value, NumericVar *dest)
3098 NumericDigit *newbuf;
3100 newbuf = digitbuf_alloc(value->ndigits + 1);
3101 newbuf[0] = 0; /* spare digit for rounding */
3102 memcpy(newbuf + 1, value->digits, value->ndigits * sizeof(NumericDigit));
3104 digitbuf_free(dest->buf);
3106 memmove(dest, value, sizeof(NumericVar));
3108 dest->digits = newbuf + 1;
3113 * get_str_from_var() -
3115 * Convert a var to text representation (guts of numeric_out).
3116 * CAUTION: var's contents may be modified by rounding!
3117 * Returns a palloc'd string.
3120 get_str_from_var(NumericVar *var, int dscale)
3137 * Check if we must round up before printing the value and do so.
3139 round_var(var, dscale);
3142 * Allocate space for the result.
3144 * i is set to to # of decimal digits before decimal point. dscale is the
3145 * # of decimal digits we will print after decimal point. We may generate
3146 * as many as DEC_DIGITS-1 excess digits at the end, and in addition we
3147 * need room for sign, decimal point, null terminator.
3149 i = (var->weight + 1) * DEC_DIGITS;
3153 str = palloc(i + dscale + DEC_DIGITS + 2);
3157 * Output a dash for negative values
3159 if (var->sign == NUMERIC_NEG)
3163 * Output all digits before the decimal point
3165 if (var->weight < 0)
3167 d = var->weight + 1;
3172 for (d = 0; d <= var->weight; d++)
3174 dig = (d < var->ndigits) ? var->digits[d] : 0;
3175 /* In the first digit, suppress extra leading decimal zeroes */
3178 bool putit = (d > 0);
3197 #elif DEC_DIGITS == 2
3200 if (d1 > 0 || d > 0)
3203 #elif DEC_DIGITS == 1
3206 #error unsupported NBASE
3212 * If requested, output a decimal point and all the digits that follow it.
3213 * We initially put out a multiple of DEC_DIGITS digits, then truncate if
3219 endcp = cp + dscale;
3220 for (i = 0; i < dscale; d++, i += DEC_DIGITS)
3222 dig = (d >= 0 && d < var->ndigits) ? var->digits[d] : 0;
3234 #elif DEC_DIGITS == 2
3239 #elif DEC_DIGITS == 1
3242 #error unsupported NBASE
3249 * terminate the string and return it
3259 * Create the packed db numeric format in palloc()'d memory from
3263 make_result(NumericVar *var)
3266 NumericDigit *digits = var->digits;
3267 int weight = var->weight;
3268 int sign = var->sign;
3272 if (sign == NUMERIC_NAN)
3274 result = (Numeric) palloc(NUMERIC_HDRSZ);
3276 SET_VARSIZE(result, NUMERIC_HDRSZ);
3277 result->n_weight = 0;
3278 result->n_sign_dscale = NUMERIC_NAN;
3280 dump_numeric("make_result()", result);
3286 /* truncate leading zeroes */
3287 while (n > 0 && *digits == 0)
3293 /* truncate trailing zeroes */
3294 while (n > 0 && digits[n - 1] == 0)
3297 /* If zero result, force to weight=0 and positive sign */
3304 /* Build the result */
3305 len = NUMERIC_HDRSZ + n * sizeof(NumericDigit);
3306 result = (Numeric) palloc(len);
3307 SET_VARSIZE(result, len);
3308 result->n_weight = weight;
3309 result->n_sign_dscale = sign | (var->dscale & NUMERIC_DSCALE_MASK);
3311 memcpy(result->n_data, digits, n * sizeof(NumericDigit));
3313 /* Check for overflow of int16 fields */
3314 if (result->n_weight != weight ||
3315 NUMERIC_DSCALE(result) != var->dscale)
3317 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3318 errmsg("value overflows numeric format")));
3320 dump_numeric("make_result()", result);
3328 * Do bounds checking and rounding according to the attributes
3332 apply_typmod(NumericVar *var, int32 typmod)
3340 /* Do nothing if we have a default typmod (-1) */
3341 if (typmod < (int32) (VARHDRSZ))
3345 precision = (typmod >> 16) & 0xffff;
3346 scale = typmod & 0xffff;
3347 maxdigits = precision - scale;
3349 /* Round to target scale (and set var->dscale) */
3350 round_var(var, scale);
3353 * Check for overflow - note we can't do this before rounding, because
3354 * rounding could raise the weight. Also note that the var's weight could
3355 * be inflated by leading zeroes, which will be stripped before storage
3356 * but perhaps might not have been yet. In any case, we must recognize a
3357 * true zero, whose weight doesn't mean anything.
3359 ddigits = (var->weight + 1) * DEC_DIGITS;
3360 if (ddigits > maxdigits)
3362 /* Determine true weight; and check for all-zero result */
3363 for (i = 0; i < var->ndigits; i++)
3365 NumericDigit dig = var->digits[i];
3369 /* Adjust for any high-order decimal zero digits */
3375 else if (dig < 1000)
3377 #elif DEC_DIGITS == 2
3380 #elif DEC_DIGITS == 1
3383 #error unsupported NBASE
3385 if (ddigits > maxdigits)
3387 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
3388 errmsg("numeric field overflow"),
3389 errdetail("A field with precision %d, scale %d must round to an absolute value less than %s%d.",
3391 /* Display 10^0 as 1 */
3392 maxdigits ? "10^" : "",
3393 maxdigits ? maxdigits : 1
3397 ddigits -= DEC_DIGITS;
3403 * Convert numeric to int8, rounding if needed.
3405 * If overflow, return FALSE (no error is raised). Return TRUE if okay.
3407 * CAUTION: var's contents may be modified by rounding!
3410 numericvar_to_int8(NumericVar *var, int64 *result)
3412 NumericDigit *digits;
3420 /* Round to nearest integer */
3423 /* Check for zero input */
3425 ndigits = var->ndigits;
3433 * For input like 10000000000, we must treat stripped digits as real. So
3434 * the loop assumes there are weight+1 digits before the decimal point.
3436 weight = var->weight;
3437 Assert(weight >= 0 && ndigits <= weight + 1);
3439 /* Construct the result */
3440 digits = var->digits;
3441 neg = (var->sign == NUMERIC_NEG);
3443 for (i = 1; i <= weight; i++)
3451 * The overflow check is a bit tricky because we want to accept
3452 * INT64_MIN, which will overflow the positive accumulator. We can
3453 * detect this case easily though because INT64_MIN is the only
3454 * nonzero value for which -val == val (on a two's complement machine,
3457 if ((val / NBASE) != oldval) /* possible overflow? */
3459 if (!neg || (-val) != val || val == 0 || oldval < 0)
3464 *result = neg ? -val : val;
3469 * Convert int8 value to numeric.
3472 int8_to_numericvar(int64 val, NumericVar *var)
3479 /* int8 can require at most 19 decimal digits; add one for safety */
3480 alloc_var(var, 20 / DEC_DIGITS);
3483 var->sign = NUMERIC_NEG;
3488 var->sign = NUMERIC_POS;
3498 ptr = var->digits + var->ndigits;
3504 newuval = uval / NBASE;
3505 *ptr = uval - newuval * NBASE;
3509 var->ndigits = ndigits;
3510 var->weight = ndigits - 1;
3514 * Convert numeric to float8; if out of range, return +/- HUGE_VAL
3517 numeric_to_double_no_overflow(Numeric num)
3523 tmp = DatumGetCString(DirectFunctionCall1(numeric_out,
3524 NumericGetDatum(num)));
3526 /* unlike float8in, we ignore ERANGE from strtod */
3527 val = strtod(tmp, &endptr);
3528 if (*endptr != '\0')
3530 /* shouldn't happen ... */
3532 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
3533 errmsg("invalid input syntax for type double precision: \"%s\"",
3542 /* As above, but work from a NumericVar */
3544 numericvar_to_double_no_overflow(NumericVar *var)
3550 tmp = get_str_from_var(var, var->dscale);
3552 /* unlike float8in, we ignore ERANGE from strtod */
3553 val = strtod(tmp, &endptr);
3554 if (*endptr != '\0')
3556 /* shouldn't happen ... */
3558 (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
3559 errmsg("invalid input syntax for type double precision: \"%s\"",
3572 * Compare two values on variable level. We assume zeroes have been
3573 * truncated to no digits.
3576 cmp_var(NumericVar *var1, NumericVar *var2)
3578 return cmp_var_common(var1->digits, var1->ndigits,
3579 var1->weight, var1->sign,
3580 var2->digits, var2->ndigits,
3581 var2->weight, var2->sign);
3585 * cmp_var_common() -
3587 * Main routine of cmp_var(). This function can be used by both
3588 * NumericVar and Numeric.
3591 cmp_var_common(const NumericDigit *var1digits, int var1ndigits,
3592 int var1weight, int var1sign,
3593 const NumericDigit *var2digits, int var2ndigits,
3594 int var2weight, int var2sign)
3596 if (var1ndigits == 0)
3598 if (var2ndigits == 0)
3600 if (var2sign == NUMERIC_NEG)
3604 if (var2ndigits == 0)
3606 if (var1sign == NUMERIC_POS)
3611 if (var1sign == NUMERIC_POS)
3613 if (var2sign == NUMERIC_NEG)
3615 return cmp_abs_common(var1digits, var1ndigits, var1weight,
3616 var2digits, var2ndigits, var2weight);
3619 if (var2sign == NUMERIC_POS)
3622 return cmp_abs_common(var2digits, var2ndigits, var2weight,
3623 var1digits, var1ndigits, var1weight);
3630 * Full version of add functionality on variable level (handling signs).
3631 * result might point to one of the operands too without danger.
3634 add_var(NumericVar *var1, NumericVar *var2, NumericVar *result)
3637 * Decide on the signs of the two variables what to do
3639 if (var1->sign == NUMERIC_POS)
3641 if (var2->sign == NUMERIC_POS)
3644 * Both are positive result = +(ABS(var1) + ABS(var2))
3646 add_abs(var1, var2, result);
3647 result->sign = NUMERIC_POS;
3652 * var1 is positive, var2 is negative Must compare absolute values
3654 switch (cmp_abs(var1, var2))
3658 * ABS(var1) == ABS(var2)
3663 result->dscale = Max(var1->dscale, var2->dscale);
3668 * ABS(var1) > ABS(var2)
3669 * result = +(ABS(var1) - ABS(var2))
3672 sub_abs(var1, var2, result);
3673 result->sign = NUMERIC_POS;
3678 * ABS(var1) < ABS(var2)
3679 * result = -(ABS(var2) - ABS(var1))
3682 sub_abs(var2, var1, result);
3683 result->sign = NUMERIC_NEG;
3690 if (var2->sign == NUMERIC_POS)
3693 * var1 is negative, var2 is positive
3694 * Must compare absolute values
3697 switch (cmp_abs(var1, var2))
3701 * ABS(var1) == ABS(var2)
3706 result->dscale = Max(var1->dscale, var2->dscale);
3711 * ABS(var1) > ABS(var2)
3712 * result = -(ABS(var1) - ABS(var2))
3715 sub_abs(var1, var2, result);
3716 result->sign = NUMERIC_NEG;
3721 * ABS(var1) < ABS(var2)
3722 * result = +(ABS(var2) - ABS(var1))
3725 sub_abs(var2, var1, result);
3726 result->sign = NUMERIC_POS;
3734 * result = -(ABS(var1) + ABS(var2))
3737 add_abs(var1, var2, result);
3738 result->sign = NUMERIC_NEG;
3747 * Full version of sub functionality on variable level (handling signs).
3748 * result might point to one of the operands too without danger.
3751 sub_var(NumericVar *var1, NumericVar *var2, NumericVar *result)
3754 * Decide on the signs of the two variables what to do
3756 if (var1->sign == NUMERIC_POS)
3758 if (var2->sign == NUMERIC_NEG)
3761 * var1 is positive, var2 is negative
3762 * result = +(ABS(var1) + ABS(var2))
3765 add_abs(var1, var2, result);
3766 result->sign = NUMERIC_POS;
3772 * Must compare absolute values
3775 switch (cmp_abs(var1, var2))
3779 * ABS(var1) == ABS(var2)
3784 result->dscale = Max(var1->dscale, var2->dscale);
3789 * ABS(var1) > ABS(var2)
3790 * result = +(ABS(var1) - ABS(var2))
3793 sub_abs(var1, var2, result);
3794 result->sign = NUMERIC_POS;
3799 * ABS(var1) < ABS(var2)
3800 * result = -(ABS(var2) - ABS(var1))
3803 sub_abs(var2, var1, result);
3804 result->sign = NUMERIC_NEG;
3811 if (var2->sign == NUMERIC_NEG)
3815 * Must compare absolute values
3818 switch (cmp_abs(var1, var2))
3822 * ABS(var1) == ABS(var2)
3827 result->dscale = Max(var1->dscale, var2->dscale);
3832 * ABS(var1) > ABS(var2)
3833 * result = -(ABS(var1) - ABS(var2))
3836 sub_abs(var1, var2, result);
3837 result->sign = NUMERIC_NEG;
3842 * ABS(var1) < ABS(var2)
3843 * result = +(ABS(var2) - ABS(var1))
3846 sub_abs(var2, var1, result);
3847 result->sign = NUMERIC_POS;
3854 * var1 is negative, var2 is positive
3855 * result = -(ABS(var1) + ABS(var2))
3858 add_abs(var1, var2, result);
3859 result->sign = NUMERIC_NEG;
3868 * Multiplication on variable level. Product of var1 * var2 is stored
3869 * in result. Result is rounded to no more than rscale fractional digits.
3872 mul_var(NumericVar *var1, NumericVar *var2, NumericVar *result,
3883 NumericDigit *res_digits;
3889 /* copy these values into local vars for speed in inner loop */
3890 int var1ndigits = var1->ndigits;
3891 int var2ndigits = var2->ndigits;
3892 NumericDigit *var1digits = var1->digits;
3893 NumericDigit *var2digits = var2->digits;
3895 if (var1ndigits == 0 || var2ndigits == 0)
3897 /* one or both inputs is zero; so is result */
3899 result->dscale = rscale;
3903 /* Determine result sign and (maximum possible) weight */
3904 if (var1->sign == var2->sign)
3905 res_sign = NUMERIC_POS;
3907 res_sign = NUMERIC_NEG;
3908 res_weight = var1->weight + var2->weight + 2;
3911 * Determine number of result digits to compute. If the exact result
3912 * would have more than rscale fractional digits, truncate the computation
3913 * with MUL_GUARD_DIGITS guard digits. We do that by pretending that one
3914 * or both inputs have fewer digits than they really do.
3916 res_ndigits = var1ndigits + var2ndigits + 1;
3917 maxdigits = res_weight + 1 + (rscale * DEC_DIGITS) + MUL_GUARD_DIGITS;
3918 if (res_ndigits > maxdigits)
3922 /* no useful precision at all in the result... */
3924 result->dscale = rscale;
3927 /* force maxdigits odd so that input ndigits can be equal */
3928 if ((maxdigits & 1) == 0)
3930 if (var1ndigits > var2ndigits)
3932 var1ndigits -= res_ndigits - maxdigits;
3933 if (var1ndigits < var2ndigits)
3934 var1ndigits = var2ndigits = (var1ndigits + var2ndigits) / 2;
3938 var2ndigits -= res_ndigits - maxdigits;
3939 if (var2ndigits < var1ndigits)
3940 var1ndigits = var2ndigits = (var1ndigits + var2ndigits) / 2;
3942 res_ndigits = maxdigits;
3943 Assert(res_ndigits == var1ndigits + var2ndigits + 1);
3947 * We do the arithmetic in an array "dig[]" of signed int's. Since
3948 * INT_MAX is noticeably larger than NBASE*NBASE, this gives us headroom
3949 * to avoid normalizing carries immediately.
3951 * maxdig tracks the maximum possible value of any dig[] entry; when this
3952 * threatens to exceed INT_MAX, we take the time to propagate carries. To
3953 * avoid overflow in maxdig itself, it actually represents the max
3954 * possible value divided by NBASE-1.
3956 dig = (int *) palloc0(res_ndigits * sizeof(int));
3959 ri = res_ndigits - 1;
3960 for (i1 = var1ndigits - 1; i1 >= 0; ri--, i1--)
3962 int var1digit = var1digits[i1];
3967 /* Time to normalize? */
3968 maxdig += var1digit;
3969 if (maxdig > INT_MAX / (NBASE - 1))
3973 for (i = res_ndigits - 1; i >= 0; i--)
3975 newdig = dig[i] + carry;
3976 if (newdig >= NBASE)
3978 carry = newdig / NBASE;
3979 newdig -= carry * NBASE;
3986 /* Reset maxdig to indicate new worst-case */
3987 maxdig = 1 + var1digit;
3990 /* Add appropriate multiple of var2 into the accumulator */
3992 for (i2 = var2ndigits - 1; i2 >= 0; i2--)
3993 dig[i--] += var1digit * var2digits[i2];
3997 * Now we do a final carry propagation pass to normalize the result, which
3998 * we combine with storing the result digits into the output. Note that
3999 * this is still done at full precision w/guard digits.
4001 alloc_var(result, res_ndigits);
4002 res_digits = result->digits;
4004 for (i = res_ndigits - 1; i >= 0; i--)
4006 newdig = dig[i] + carry;
4007 if (newdig >= NBASE)
4009 carry = newdig / NBASE;
4010 newdig -= carry * NBASE;
4014 res_digits[i] = newdig;
4021 * Finally, round the result to the requested precision.
4023 result->weight = res_weight;
4024 result->sign = res_sign;
4026 /* Round to target rscale (and set result->dscale) */
4027 round_var(result, rscale);
4029 /* Strip leading and trailing zeroes */
4037 * Division on variable level. Quotient of var1 / var2 is stored
4038 * in result. Result is rounded to no more than rscale fractional digits.
4041 div_var(NumericVar *var1, NumericVar *var2, NumericVar *result,
4042 int rscale, bool round)
4052 NumericDigit *res_digits;
4060 /* copy these values into local vars for speed in inner loop */
4061 int var1ndigits = var1->ndigits;
4062 int var2ndigits = var2->ndigits;
4063 NumericDigit *var1digits = var1->digits;
4064 NumericDigit *var2digits = var2->digits;
4067 * First of all division by zero check; we must not be handed an
4068 * unnormalized divisor.
4070 if (var2ndigits == 0 || var2digits[0] == 0)
4072 (errcode(ERRCODE_DIVISION_BY_ZERO),
4073 errmsg("division by zero")));
4076 * Now result zero check
4078 if (var1ndigits == 0)
4081 result->dscale = rscale;
4086 * Determine the result sign, weight and number of digits to calculate
4088 if (var1->sign == var2->sign)
4089 res_sign = NUMERIC_POS;
4091 res_sign = NUMERIC_NEG;
4092 res_weight = var1->weight - var2->weight + 1;
4093 /* The number of accurate result digits we need to produce: */
4094 div_ndigits = res_weight + 1 + (rscale + DEC_DIGITS - 1) / DEC_DIGITS;
4095 /* Add guard digits for roundoff error */
4096 div_ndigits += DIV_GUARD_DIGITS;
4097 if (div_ndigits < DIV_GUARD_DIGITS)
4098 div_ndigits = DIV_GUARD_DIGITS;
4099 /* Must be at least var1ndigits, too, to simplify data-loading loop */
4100 if (div_ndigits < var1ndigits)
4101 div_ndigits = var1ndigits;
4104 * We do the arithmetic in an array "div[]" of signed int's. Since
4105 * INT_MAX is noticeably larger than NBASE*NBASE, this gives us headroom
4106 * to avoid normalizing carries immediately.
4108 * We start with div[] containing one zero digit followed by the
4109 * dividend's digits (plus appended zeroes to reach the desired precision
4110 * including guard digits). Each step of the main loop computes an
4111 * (approximate) quotient digit and stores it into div[], removing one
4112 * position of dividend space. A final pass of carry propagation takes
4113 * care of any mistaken quotient digits.
4115 div = (int *) palloc0((div_ndigits + 1) * sizeof(int));
4116 for (i = 0; i < var1ndigits; i++)
4117 div[i + 1] = var1digits[i];
4120 * We estimate each quotient digit using floating-point arithmetic, taking
4121 * the first four digits of the (current) dividend and divisor. This must
4122 * be float to avoid overflow.
4124 fdivisor = (double) var2digits[0];
4125 for (i = 1; i < 4; i++)
4128 if (i < var2ndigits)
4129 fdivisor += (double) var2digits[i];
4131 fdivisorinverse = 1.0 / fdivisor;
4134 * maxdiv tracks the maximum possible absolute value of any div[] entry;
4135 * when this threatens to exceed INT_MAX, we take the time to propagate
4136 * carries. To avoid overflow in maxdiv itself, it actually represents
4137 * the max possible abs. value divided by NBASE-1.
4142 * Outer loop computes next quotient digit, which will go into div[qi]
4144 for (qi = 0; qi < div_ndigits; qi++)
4146 /* Approximate the current dividend value */
4147 fdividend = (double) div[qi];
4148 for (i = 1; i < 4; i++)
4151 if (qi + i <= div_ndigits)
4152 fdividend += (double) div[qi + i];
4154 /* Compute the (approximate) quotient digit */
4155 fquotient = fdividend * fdivisorinverse;
4156 qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
4157 (((int) fquotient) - 1); /* truncate towards -infinity */
4161 /* Do we need to normalize now? */
4162 maxdiv += Abs(qdigit);
4163 if (maxdiv > INT_MAX / (NBASE - 1))
4167 for (i = div_ndigits; i > qi; i--)
4169 newdig = div[i] + carry;
4172 carry = -((-newdig - 1) / NBASE) - 1;
4173 newdig -= carry * NBASE;
4175 else if (newdig >= NBASE)
4177 carry = newdig / NBASE;
4178 newdig -= carry * NBASE;
4184 newdig = div[qi] + carry;
4188 * All the div[] digits except possibly div[qi] are now in the
4191 maxdiv = Abs(newdig) / (NBASE - 1);
4192 maxdiv = Max(maxdiv, 1);
4195 * Recompute the quotient digit since new info may have
4196 * propagated into the top four dividend digits
4198 fdividend = (double) div[qi];
4199 for (i = 1; i < 4; i++)
4202 if (qi + i <= div_ndigits)
4203 fdividend += (double) div[qi + i];
4205 /* Compute the (approximate) quotient digit */
4206 fquotient = fdividend * fdivisorinverse;
4207 qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
4208 (((int) fquotient) - 1); /* truncate towards -infinity */
4209 maxdiv += Abs(qdigit);
4212 /* Subtract off the appropriate multiple of the divisor */
4215 int istop = Min(var2ndigits, div_ndigits - qi + 1);
4217 for (i = 0; i < istop; i++)
4218 div[qi + i] -= qdigit * var2digits[i];
4223 * The dividend digit we are about to replace might still be nonzero.
4224 * Fold it into the next digit position. We don't need to worry about
4225 * overflow here since this should nearly cancel with the subtraction
4228 div[qi + 1] += div[qi] * NBASE;
4234 * Approximate and store the last quotient digit (div[div_ndigits])
4236 fdividend = (double) div[qi];
4237 for (i = 1; i < 4; i++)
4239 fquotient = fdividend * fdivisorinverse;
4240 qdigit = (fquotient >= 0.0) ? ((int) fquotient) :
4241 (((int) fquotient) - 1); /* truncate towards -infinity */
4245 * Now we do a final carry propagation pass to normalize the result, which
4246 * we combine with storing the result digits into the output. Note that
4247 * this is still done at full precision w/guard digits.
4249 alloc_var(result, div_ndigits + 1);
4250 res_digits = result->digits;
4252 for (i = div_ndigits; i >= 0; i--)
4254 newdig = div[i] + carry;
4257 carry = -((-newdig - 1) / NBASE) - 1;
4258 newdig -= carry * NBASE;
4260 else if (newdig >= NBASE)
4262 carry = newdig / NBASE;
4263 newdig -= carry * NBASE;
4267 res_digits[i] = newdig;
4274 * Finally, round the result to the requested precision.
4276 result->weight = res_weight;
4277 result->sign = res_sign;
4279 /* Round to target rscale (and set result->dscale) */
4281 round_var(result, rscale);
4283 trunc_var(result, rscale);
4285 /* Strip leading and trailing zeroes */
4291 * Default scale selection for division
4293 * Returns the appropriate result scale for the division result.
4296 select_div_scale(NumericVar *var1, NumericVar *var2)
4302 NumericDigit firstdigit1,
4307 * The result scale of a division isn't specified in any SQL standard. For
4308 * PostgreSQL we select a result scale that will give at least
4309 * NUMERIC_MIN_SIG_DIGITS significant digits, so that numeric gives a
4310 * result no less accurate than float8; but use a scale not less than
4311 * either input's display scale.
4314 /* Get the actual (normalized) weight and first digit of each input */
4316 weight1 = 0; /* values to use if var1 is zero */
4318 for (i = 0; i < var1->ndigits; i++)
4320 firstdigit1 = var1->digits[i];
4321 if (firstdigit1 != 0)
4323 weight1 = var1->weight - i;
4328 weight2 = 0; /* values to use if var2 is zero */
4330 for (i = 0; i < var2->ndigits; i++)
4332 firstdigit2 = var2->digits[i];
4333 if (firstdigit2 != 0)
4335 weight2 = var2->weight - i;
4341 * Estimate weight of quotient. If the two first digits are equal, we
4342 * can't be sure, but assume that var1 is less than var2.
4344 qweight = weight1 - weight2;
4345 if (firstdigit1 <= firstdigit2)
4348 /* Select result scale */
4349 rscale = NUMERIC_MIN_SIG_DIGITS - qweight * DEC_DIGITS;
4350 rscale = Max(rscale, var1->dscale);
4351 rscale = Max(rscale, var2->dscale);
4352 rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
4353 rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
4362 * Calculate the modulo of two numerics at variable level
4365 mod_var(NumericVar *var1, NumericVar *var2, NumericVar *result)
4373 * We do this using the equation
4374 * mod(x,y) = x - trunc(x/y)*y
4375 * We set rscale the same way numeric_div and numeric_mul do
4376 * to get the right answer from the equation. The final result,
4377 * however, need not be displayed to more precision than the inputs.
4380 rscale = select_div_scale(var1, var2);
4382 div_var(var1, var2, &tmp, rscale, false);
4386 mul_var(var2, &tmp, &tmp, var2->dscale + tmp.dscale);
4388 sub_var(var1, &tmp, result);
4390 round_var(result, Max(var1->dscale, var2->dscale));
4399 * Return the smallest integer greater than or equal to the argument
4403 ceil_var(NumericVar *var, NumericVar *result)
4408 set_var_from_var(var, &tmp);
4412 if (var->sign == NUMERIC_POS && cmp_var(var, &tmp) != 0)
4413 add_var(&tmp, &const_one, &tmp);
4415 set_var_from_var(&tmp, result);
4423 * Return the largest integer equal to or less than the argument
4427 floor_var(NumericVar *var, NumericVar *result)
4432 set_var_from_var(var, &tmp);
4436 if (var->sign == NUMERIC_NEG && cmp_var(var, &tmp) != 0)
4437 sub_var(&tmp, &const_one, &tmp);
4439 set_var_from_var(&tmp, result);
4447 * Compute the square root of x using Newton's algorithm
4450 sqrt_var(NumericVar *arg, NumericVar *result, int rscale)
4454 NumericVar last_val;
4458 local_rscale = rscale + 8;
4460 stat = cmp_var(arg, &const_zero);
4464 result->dscale = rscale;
4469 * SQL2003 defines sqrt() in terms of power, so we need to emit the right
4470 * SQLSTATE error code if the operand is negative.
4474 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_POWER_FUNCTION),
4475 errmsg("cannot take square root of a negative number")));
4479 init_var(&last_val);
4481 /* Copy arg in case it is the same var as result */
4482 set_var_from_var(arg, &tmp_arg);
4485 * Initialize the result to the first guess
4487 alloc_var(result, 1);
4488 result->digits[0] = tmp_arg.digits[0] / 2;
4489 if (result->digits[0] == 0)
4490 result->digits[0] = 1;
4491 result->weight = tmp_arg.weight / 2;
4492 result->sign = NUMERIC_POS;
4494 set_var_from_var(result, &last_val);
4498 div_var(&tmp_arg, result, &tmp_val, local_rscale, true);
4500 add_var(result, &tmp_val, result);
4501 mul_var(result, &const_zero_point_five, result, local_rscale);
4503 if (cmp_var(&last_val, result) == 0)
4505 set_var_from_var(result, &last_val);
4508 free_var(&last_val);
4512 /* Round to requested precision */
4513 round_var(result, rscale);
4520 * Raise e to the power of x
4523 exp_var(NumericVar *arg, NumericVar *result, int rscale)
4531 * We separate the integral and fraction parts of x, then compute
4532 * e^x = e^xint * e^xfrac
4533 * where e = exp(1) and e^xfrac = exp(xfrac) are computed by
4534 * exp_var_internal; the limited range of inputs allows that routine
4535 * to do a good job with a simple Taylor series. Raising e^xint is
4536 * done by repeated multiplications in power_var_int.
4541 set_var_from_var(arg, &x);
4543 if (x.sign == NUMERIC_NEG)
4546 x.sign = NUMERIC_POS;
4549 /* Extract the integer part, remove it from x */
4551 while (x.weight >= 0)
4556 xintval += x.digits[0];
4561 /* Guard against overflow */
4562 if (xintval >= NUMERIC_MAX_RESULT_SCALE * 3)
4564 (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4565 errmsg("argument for function \"exp\" too big")));
4568 /* Select an appropriate scale for internal calculation */
4569 local_rscale = rscale + MUL_GUARD_DIGITS * 2;
4571 /* Compute e^xfrac */
4572 exp_var_internal(&x, result, local_rscale);
4574 /* If there's an integer part, multiply by e^xint */
4580 exp_var_internal(&const_one, &e, local_rscale);
4581 power_var_int(&e, xintval, &e, local_rscale);
4582 mul_var(&e, result, result, local_rscale);
4586 /* Compensate for input sign, and round to requested rscale */
4588 div_var(&const_one, result, result, rscale, true);
4590 round_var(result, rscale);
4597 * exp_var_internal() -
4599 * Raise e to the power of x, where 0 <= x <= 1
4601 * NB: the result should be good to at least rscale digits, but it has
4602 * *not* been rounded off; the caller must do that if wanted.
4605 exp_var_internal(NumericVar *arg, NumericVar *result, int rscale)
4621 set_var_from_var(arg, &x);
4623 Assert(x.sign == NUMERIC_POS);
4625 local_rscale = rscale + 8;
4627 /* Reduce input into range 0 <= x <= 0.01 */
4628 while (cmp_var(&x, &const_zero_point_01) > 0)
4632 mul_var(&x, &const_zero_point_five, &x, x.dscale + 1);
4636 * Use the Taylor series
4638 * exp(x) = 1 + x + x^2/2! + x^3/3! + ...
4640 * Given the limited range of x, this should converge reasonably quickly.
4641 * We run the series until the terms fall below the local_rscale limit.
4643 add_var(&const_one, &x, result);
4644 set_var_from_var(&x, &xpow);
4645 set_var_from_var(&const_one, &ifac);
4646 set_var_from_var(&const_one, &ni);
4650 add_var(&ni, &const_one, &ni);
4651 mul_var(&xpow, &x, &xpow, local_rscale);
4652 mul_var(&ifac, &ni, &ifac, 0);
4653 div_var(&xpow, &ifac, &elem, local_rscale, true);
4655 if (elem.ndigits == 0)
4658 add_var(result, &elem, result);
4661 /* Compensate for argument range reduction */
4663 mul_var(result, result, result, local_rscale);
4676 * Compute the natural log of x
4679 ln_var(NumericVar *arg, NumericVar *result, int rscale)
4689 cmp = cmp_var(arg, &const_zero);
4692 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
4693 errmsg("cannot take logarithm of zero")));
4696 (errcode(ERRCODE_INVALID_ARGUMENT_FOR_LOG),
4697 errmsg("cannot take logarithm of a negative number")));
4699 local_rscale = rscale + 8;
4707 set_var_from_var(arg, &x);
4708 set_var_from_var(&const_two, &fact);
4710 /* Reduce input into range 0.9 < x < 1.1 */
4711 while (cmp_var(&x, &const_zero_point_nine) <= 0)
4714 sqrt_var(&x, &x, local_rscale);
4715 mul_var(&fact, &const_two, &fact, 0);
4717 while (cmp_var(&x, &const_one_point_one) >= 0)
4720 sqrt_var(&x, &x, local_rscale);
4721 mul_var(&fact, &const_two, &fact, 0);
4725 * We use the Taylor series for 0.5 * ln((1+z)/(1-z)),
4727 * z + z^3/3 + z^5/5 + ...
4729 * where z = (x-1)/(x+1) is in the range (approximately) -0.053 .. 0.048
4730 * due to the above range-reduction of x.
4732 * The convergence of this is not as fast as one would like, but is
4733 * tolerable given that z is small.
4735 sub_var(&x, &const_one, result);
4736 add_var(&x, &const_one, &elem);
4737 div_var(result, &elem, result, local_rscale, true);
4738 set_var_from_var(result, &xx);
4739 mul_var(result, result, &x, local_rscale);
4741 set_var_from_var(&const_one, &ni);
4745 add_var(&ni, &const_two, &ni);
4746 mul_var(&xx, &x, &xx, local_rscale);
4747 div_var(&xx, &ni, &elem, local_rscale, true);
4749 if (elem.ndigits == 0)
4752 add_var(result, &elem, result);
4754 if (elem.weight < (result->weight - local_rscale * 2 / DEC_DIGITS))
4758 /* Compensate for argument range reduction, round to requested rscale */
4759 mul_var(result, &fact, result, rscale);
4772 * Compute the logarithm of num in a given base.
4774 * Note: this routine chooses dscale of the result.
4777 log_var(NumericVar *base, NumericVar *num, NumericVar *result)
4788 /* Set scale for ln() calculations --- compare numeric_ln() */
4790 /* Approx decimal digits before decimal point */
4791 dec_digits = (num->weight + 1) * DEC_DIGITS;
4794 rscale = NUMERIC_MIN_SIG_DIGITS - (int) log10(dec_digits - 1);
4795 else if (dec_digits < 1)
4796 rscale = NUMERIC_MIN_SIG_DIGITS - (int) log10(1 - dec_digits);
4798 rscale = NUMERIC_MIN_SIG_DIGITS;
4800 rscale = Max(rscale, base->dscale);
4801 rscale = Max(rscale, num->dscale);
4802 rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
4803 rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
4805 local_rscale = rscale + 8;
4807 /* Form natural logarithms */
4808 ln_var(base, &ln_base, local_rscale);
4809 ln_var(num, &ln_num, local_rscale);
4811 ln_base.dscale = rscale;
4812 ln_num.dscale = rscale;
4814 /* Select scale for division result */
4815 rscale = select_div_scale(&ln_num, &ln_base);
4817 div_var(&ln_num, &ln_base, result, rscale, true);
4827 * Raise base to the power of exp
4829 * Note: this routine chooses dscale of the result.
4832 power_var(NumericVar *base, NumericVar *exp, NumericVar *result)
4841 /* If exp can be represented as an integer, use power_var_int */
4842 if (exp->ndigits == 0 || exp->ndigits <= exp->weight + 1)
4844 /* exact integer, but does it fit in int? */
4848 /* must copy because numericvar_to_int8() scribbles on input */
4850 set_var_from_var(exp, &x);
4851 if (numericvar_to_int8(&x, &expval64))
4853 int expval = (int) expval64;
4855 /* Test for overflow by reverse-conversion. */
4856 if ((int64) expval == expval64)
4858 /* Okay, select rscale */
4859 rscale = NUMERIC_MIN_SIG_DIGITS;
4860 rscale = Max(rscale, base->dscale);
4861 rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
4862 rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
4864 power_var_int(base, expval, result, rscale);
4876 /* Set scale for ln() calculation --- need extra accuracy here */
4878 /* Approx decimal digits before decimal point */
4879 dec_digits = (base->weight + 1) * DEC_DIGITS;
4882 rscale = NUMERIC_MIN_SIG_DIGITS * 2 - (int) log10(dec_digits - 1);
4883 else if (dec_digits < 1)
4884 rscale = NUMERIC_MIN_SIG_DIGITS * 2 - (int) log10(1 - dec_digits);
4886 rscale = NUMERIC_MIN_SIG_DIGITS * 2;
4888 rscale = Max(rscale, base->dscale * 2);
4889 rscale = Max(rscale, exp->dscale * 2);
4890 rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE * 2);
4891 rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE * 2);
4893 local_rscale = rscale + 8;
4895 ln_var(base, &ln_base, local_rscale);
4897 mul_var(&ln_base, exp, &ln_num, local_rscale);
4899 /* Set scale for exp() -- compare numeric_exp() */
4901 /* convert input to float8, ignoring overflow */
4902 val = numericvar_to_double_no_overflow(&ln_num);
4905 * log10(result) = num * log10(e), so this is approximately the weight:
4907 val *= 0.434294481903252;
4909 /* limit to something that won't cause integer overflow */
4910 val = Max(val, -NUMERIC_MAX_RESULT_SCALE);
4911 val = Min(val, NUMERIC_MAX_RESULT_SCALE);
4913 rscale = NUMERIC_MIN_SIG_DIGITS - (int) val;
4914 rscale = Max(rscale, base->dscale);
4915 rscale = Max(rscale, exp->dscale);
4916 rscale = Max(rscale, NUMERIC_MIN_DISPLAY_SCALE);
4917 rscale = Min(rscale, NUMERIC_MAX_DISPLAY_SCALE);
4919 exp_var(&ln_num, result, rscale);
4928 * Raise base to the power of exp, where exp is an integer.
4931 power_var_int(NumericVar *base, int exp, NumericVar *result, int rscale)
4934 NumericVar base_prod;
4937 /* Detect some special cases, particularly 0^0. */
4942 if (base->ndigits == 0)
4944 (errcode(ERRCODE_FLOATING_POINT_EXCEPTION),
4945 errmsg("zero raised to zero is undefined")));
4946 set_var_from_var(&const_one, result);
4947 result->dscale = rscale; /* no need to round */
4950 set_var_from_var(base, result);
4951 round_var(result, rscale);
4954 div_var(&const_one, base, result, rscale, true);
4957 mul_var(base, base, result, rscale);
4964 * The general case repeatedly multiplies base according to the bit
4965 * pattern of exp. We do the multiplications with some extra precision.
4970 local_rscale = rscale + MUL_GUARD_DIGITS * 2;
4972 init_var(&base_prod);
4973 set_var_from_var(base, &base_prod);
4976 set_var_from_var(base, result);
4978 set_var_from_var(&const_one, result);
4980 while ((exp >>= 1) > 0)
4982 mul_var(&base_prod, &base_prod, &base_prod, local_rscale);
4984 mul_var(&base_prod, result, result, local_rscale);
4987 free_var(&base_prod);
4989 /* Compensate for input sign, and round to requested rscale */
4991 div_var(&const_one, result, result, rscale, true);
4993 round_var(result, rscale);
4997 /* ----------------------------------------------------------------------
4999 * Following are the lowest level functions that operate unsigned
5000 * on the variable level
5002 * ----------------------------------------------------------------------
5009 * Compare the absolute values of var1 and var2
5010 * Returns: -1 for ABS(var1) < ABS(var2)
5011 * 0 for ABS(var1) == ABS(var2)
5012 * 1 for ABS(var1) > ABS(var2)
5016 cmp_abs(NumericVar *var1, NumericVar *var2)
5018 return cmp_abs_common(var1->digits, var1->ndigits, var1->weight,
5019 var2->digits, var2->ndigits, var2->weight);
5023 * cmp_abs_common() -
5025 * Main routine of cmp_abs(). This function can be used by both
5026 * NumericVar and Numeric.
5030 cmp_abs_common(const NumericDigit *var1digits, int var1ndigits, int var1weight,
5031 const NumericDigit *var2digits, int var2ndigits, int var2weight)
5036 /* Check any digits before the first common digit */
5038 while (var1weight > var2weight && i1 < var1ndigits)
5040 if (var1digits[i1++] != 0)
5044 while (var2weight > var1weight && i2 < var2ndigits)
5046 if (var2digits[i2++] != 0)
5051 /* At this point, either w1 == w2 or we've run out of digits */
5053 if (var1weight == var2weight)
5055 while (i1 < var1ndigits && i2 < var2ndigits)
5057 int stat = var1digits[i1++] - var2digits[i2++];
5069 * At this point, we've run out of digits on one side or the other; so any
5070 * remaining nonzero digits imply that side is larger
5072 while (i1 < var1ndigits)
5074 if (var1digits[i1++] != 0)
5077 while (i2 < var2ndigits)
5079 if (var2digits[i2++] != 0)
5090 * Add the absolute values of two variables into result.
5091 * result might point to one of the operands without danger.
5094 add_abs(NumericVar *var1, NumericVar *var2, NumericVar *result)
5096 NumericDigit *res_buf;
5097 NumericDigit *res_digits;
5109 /* copy these values into local vars for speed in inner loop */
5110 int var1ndigits = var1->ndigits;
5111 int var2ndigits = var2->ndigits;
5112 NumericDigit *var1digits = var1->digits;
5113 NumericDigit *var2digits = var2->digits;
5115 res_weight = Max(var1->weight, var2->weight) + 1;
5117 res_dscale = Max(var1->dscale, var2->dscale);
5119 /* Note: here we are figuring rscale in base-NBASE digits */
5120 rscale1 = var1->ndigits - var1->weight - 1;
5121 rscale2 = var2->ndigits - var2->weight - 1;
5122 res_rscale = Max(rscale1, rscale2);
5124 res_ndigits = res_rscale + res_weight + 1;
5125 if (res_ndigits <= 0)
5128 res_buf = digitbuf_alloc(res_ndigits + 1);
5129 res_buf[0] = 0; /* spare digit for later rounding */
5130 res_digits = res_buf + 1;
5132 i1 = res_rscale + var1->weight + 1;
5133 i2 = res_rscale + var2->weight + 1;
5134 for (i = res_ndigits - 1; i >= 0; i--)
5138 if (i1 >= 0 && i1 < var1ndigits)
5139 carry += var1digits[i1];
5140 if (i2 >= 0 && i2 < var2ndigits)
5141 carry += var2digits[i2];
5145 res_digits[i] = carry - NBASE;
5150 res_digits[i] = carry;
5155 Assert(carry == 0); /* else we failed to allow for carry out */
5157 digitbuf_free(result->buf);
5158 result->ndigits = res_ndigits;
5159 result->buf = res_buf;
5160 result->digits = res_digits;
5161 result->weight = res_weight;
5162 result->dscale = res_dscale;
5164 /* Remove leading/trailing zeroes */
5172 * Subtract the absolute value of var2 from the absolute value of var1
5173 * and store in result. result might point to one of the operands
5176 * ABS(var1) MUST BE GREATER OR EQUAL ABS(var2) !!!
5179 sub_abs(NumericVar *var1, NumericVar *var2, NumericVar *result)
5181 NumericDigit *res_buf;
5182 NumericDigit *res_digits;
5194 /* copy these values into local vars for speed in inner loop */
5195 int var1ndigits = var1->ndigits;
5196 int var2ndigits = var2->ndigits;
5197 NumericDigit *var1digits = var1->digits;
5198 NumericDigit *var2digits = var2->digits;
5200 res_weight = var1->weight;
5202 res_dscale = Max(var1->dscale, var2->dscale);
5204 /* Note: here we are figuring rscale in base-NBASE digits */
5205 rscale1 = var1->ndigits - var1->weight - 1;
5206 rscale2 = var2->ndigits - var2->weight - 1;
5207 res_rscale = Max(rscale1, rscale2);
5209 res_ndigits = res_rscale + res_weight + 1;
5210 if (res_ndigits <= 0)
5213 res_buf = digitbuf_alloc(res_ndigits + 1);
5214 res_buf[0] = 0; /* spare digit for later rounding */
5215 res_digits = res_buf + 1;
5217 i1 = res_rscale + var1->weight + 1;
5218 i2 = res_rscale + var2->weight + 1;
5219 for (i = res_ndigits - 1; i >= 0; i--)
5223 if (i1 >= 0 && i1 < var1ndigits)
5224 borrow += var1digits[i1];
5225 if (i2 >= 0 && i2 < var2ndigits)
5226 borrow -= var2digits[i2];
5230 res_digits[i] = borrow + NBASE;
5235 res_digits[i] = borrow;
5240 Assert(borrow == 0); /* else caller gave us var1 < var2 */
5242 digitbuf_free(result->buf);
5243 result->ndigits = res_ndigits;
5244 result->buf = res_buf;
5245 result->digits = res_digits;
5246 result->weight = res_weight;
5247 result->dscale = res_dscale;
5249 /* Remove leading/trailing zeroes */
5256 * Round the value of a variable to no more than rscale decimal digits
5257 * after the decimal point. NOTE: we allow rscale < 0 here, implying
5258 * rounding before the decimal point.
5261 round_var(NumericVar *var, int rscale)
5263 NumericDigit *digits = var->digits;
5268 var->dscale = rscale;
5270 /* decimal digits wanted */
5271 di = (var->weight + 1) * DEC_DIGITS + rscale;
5274 * If di = 0, the value loses all digits, but could round up to 1 if its
5275 * first extra digit is >= 5. If di < 0 the result must be 0.
5281 var->sign = NUMERIC_POS;
5285 /* NBASE digits wanted */
5286 ndigits = (di + DEC_DIGITS - 1) / DEC_DIGITS;
5288 /* 0, or number of decimal digits to keep in last NBASE digit */
5291 if (ndigits < var->ndigits ||
5292 (ndigits == var->ndigits && di > 0))
5294 var->ndigits = ndigits;
5297 /* di must be zero */
5298 carry = (digits[ndigits] >= HALF_NBASE) ? 1 : 0;
5301 carry = (digits[ndigits] >= HALF_NBASE) ? 1 : 0;
5304 /* Must round within last NBASE digit */
5309 pow10 = round_powers[di];
5310 #elif DEC_DIGITS == 2
5313 #error unsupported NBASE
5315 extra = digits[--ndigits] % pow10;
5316 digits[ndigits] -= extra;
5318 if (extra >= pow10 / 2)
5320 pow10 += digits[ndigits];
5326 digits[ndigits] = pow10;
5331 /* Propagate carry if needed */
5334 carry += digits[--ndigits];
5337 digits[ndigits] = carry - NBASE;
5342 digits[ndigits] = carry;
5349 Assert(ndigits == -1); /* better not have added > 1 digit */
5350 Assert(var->digits > var->buf);
5362 * Truncate the value of a variable at rscale decimal digits after the
5363 * decimal point. NOTE: we allow rscale < 0 here, implying
5364 * truncation before the decimal point.
5367 trunc_var(NumericVar *var, int rscale)
5372 var->dscale = rscale;
5374 /* decimal digits wanted */
5375 di = (var->weight + 1) * DEC_DIGITS + rscale;
5378 * If di <= 0, the value loses all digits.
5384 var->sign = NUMERIC_POS;
5388 /* NBASE digits wanted */
5389 ndigits = (di + DEC_DIGITS - 1) / DEC_DIGITS;
5391 if (ndigits <= var->ndigits)
5393 var->ndigits = ndigits;
5396 /* no within-digit stuff to worry about */
5398 /* 0, or number of decimal digits to keep in last NBASE digit */
5403 /* Must truncate within last NBASE digit */
5404 NumericDigit *digits = var->digits;
5409 pow10 = round_powers[di];
5410 #elif DEC_DIGITS == 2
5413 #error unsupported NBASE
5415 extra = digits[--ndigits] % pow10;
5416 digits[ndigits] -= extra;
5426 * Strip any leading and trailing zeroes from a numeric variable
5429 strip_var(NumericVar *var)
5431 NumericDigit *digits = var->digits;
5432 int ndigits = var->ndigits;
5434 /* Strip leading zeroes */
5435 while (ndigits > 0 && *digits == 0)
5442 /* Strip trailing zeroes */
5443 while (ndigits > 0 && digits[ndigits - 1] == 0)
5446 /* If it's zero, normalize the sign and weight */
5449 var->sign = NUMERIC_POS;
5453 var->digits = digits;
5454 var->ndigits = ndigits;