~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/interfaces/ecpg/pgtypeslib/numeric.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "postgres_fe.h"
 
2
#include <ctype.h>
 
3
#include <limits.h>
 
4
 
 
5
#include "extern.h"
 
6
#include "pgtypes_error.h"
 
7
 
 
8
#define Max(x, y)                               ((x) > (y) ? (x) : (y))
 
9
#define Min(x, y)                               ((x) < (y) ? (x) : (y))
 
10
 
 
11
#define init_var(v)                             memset(v,0,sizeof(numeric))
 
12
 
 
13
#define digitbuf_alloc(size) ((NumericDigit *) pgtypes_alloc(size))
 
14
#define digitbuf_free(buf)              \
 
15
           do { \
 
16
                                 if ((buf) != NULL) \
 
17
                                                  free(buf); \
 
18
                  } while (0)
 
19
 
 
20
#include "pgtypes_numeric.h"
 
21
 
 
22
#if 0
 
23
/* ----------
 
24
 * apply_typmod() -
 
25
 *
 
26
 *      Do bounds checking and rounding according to the attributes
 
27
 *      typmod field.
 
28
 * ----------
 
29
 */
 
30
static int
 
31
apply_typmod(numeric *var, long typmod)
 
32
{
 
33
        int                     precision;
 
34
        int                     scale;
 
35
        int                     maxweight;
 
36
        int                     i;
 
37
 
 
38
        /* Do nothing if we have a default typmod (-1) */
 
39
        if (typmod < (long) (VARHDRSZ))
 
40
                return (0);
 
41
 
 
42
        typmod -= VARHDRSZ;
 
43
        precision = (typmod >> 16) & 0xffff;
 
44
        scale = typmod & 0xffff;
 
45
        maxweight = precision - scale;
 
46
 
 
47
        /* Round to target scale */
 
48
        i = scale + var->weight + 1;
 
49
        if (i >= 0 && var->ndigits > i)
 
50
        {
 
51
                int                     carry = (var->digits[i] > 4) ? 1 : 0;
 
52
 
 
53
                var->ndigits = i;
 
54
 
 
55
                while (carry)
 
56
                {
 
57
                        carry += var->digits[--i];
 
58
                        var->digits[i] = carry % 10;
 
59
                        carry /= 10;
 
60
                }
 
61
 
 
62
                if (i < 0)
 
63
                {
 
64
                        var->digits--;
 
65
                        var->ndigits++;
 
66
                        var->weight++;
 
67
                }
 
68
        }
 
69
        else
 
70
                var->ndigits = Max(0, Min(i, var->ndigits));
 
71
 
 
72
        /*
 
73
         * Check for overflow - note we can't do this before rounding, because
 
74
         * rounding could raise the weight.  Also note that the var's weight
 
75
         * could be inflated by leading zeroes, which will be stripped before
 
76
         * storage but perhaps might not have been yet. In any case, we must
 
77
         * recognize a true zero, whose weight doesn't mean anything.
 
78
         */
 
79
        if (var->weight >= maxweight)
 
80
        {
 
81
                /* Determine true weight; and check for all-zero result */
 
82
                int                     tweight = var->weight;
 
83
 
 
84
                for (i = 0; i < var->ndigits; i++)
 
85
                {
 
86
                        if (var->digits[i])
 
87
                                break;
 
88
                        tweight--;
 
89
                }
 
90
 
 
91
                if (tweight >= maxweight && i < var->ndigits)
 
92
                {
 
93
                        errno = PGTYPES_NUM_OVERFLOW;
 
94
                        return -1;
 
95
                }
 
96
        }
 
97
 
 
98
        var->rscale = scale;
 
99
        var->dscale = scale;
 
100
        return (0);
 
101
}
 
102
#endif
 
103
 
 
104
/* ----------
 
105
 *      alloc_var() -
 
106
 *
 
107
 *       Allocate a digit buffer of ndigits digits (plus a spare digit for rounding)
 
108
 * ----------
 
109
 */
 
110
static int
 
111
alloc_var(numeric *var, int ndigits)
 
112
{
 
113
        digitbuf_free(var->buf);
 
114
        var->buf = digitbuf_alloc(ndigits + 1);
 
115
        if (var->buf == NULL)
 
116
                return -1;
 
117
        var->buf[0] = 0;
 
118
        var->digits = var->buf + 1;
 
119
        var->ndigits = ndigits;
 
120
        return 0;
 
121
}
 
122
 
 
123
numeric *
 
124
PGTYPESnumeric_new(void)
 
125
{
 
126
        numeric    *var;
 
127
 
 
128
        if ((var = (numeric *) pgtypes_alloc(sizeof(numeric))) == NULL)
 
129
                return NULL;
 
130
 
 
131
        if (alloc_var(var, 0) < 0)
 
132
                return NULL;
 
133
 
 
134
        return var;
 
135
}
 
136
 
 
137
/* ----------
 
138
 * set_var_from_str()
 
139
 *
 
140
 *      Parse a string and put the number into a variable
 
141
 * ----------
 
142
 */
 
143
static int
 
144
set_var_from_str(char *str, char **ptr, numeric *dest)
 
145
{
 
146
        bool            have_dp = FALSE;
 
147
        int                     i = 0;
 
148
 
 
149
        errno = 0;
 
150
        *ptr = str;
 
151
        while (*(*ptr))
 
152
        {
 
153
                if (!isspace((unsigned char) *(*ptr)))
 
154
                        break;
 
155
                (*ptr)++;
 
156
        }
 
157
 
 
158
        if (alloc_var(dest, strlen((*ptr))) < 0)
 
159
                return -1;
 
160
        dest->weight = -1;
 
161
        dest->dscale = 0;
 
162
        dest->sign = NUMERIC_POS;
 
163
 
 
164
        switch (*(*ptr))
 
165
        {
 
166
                case '+':
 
167
                        dest->sign = NUMERIC_POS;
 
168
                        (*ptr)++;
 
169
                        break;
 
170
 
 
171
                case '-':
 
172
                        dest->sign = NUMERIC_NEG;
 
173
                        (*ptr)++;
 
174
                        break;
 
175
        }
 
176
 
 
177
        if (*(*ptr) == '.')
 
178
        {
 
179
                have_dp = TRUE;
 
180
                (*ptr)++;
 
181
        }
 
182
 
 
183
        if (!isdigit((unsigned char) *(*ptr)))
 
184
        {
 
185
                errno = PGTYPES_NUM_BAD_NUMERIC;
 
186
                return -1;
 
187
        }
 
188
 
 
189
        while (*(*ptr))
 
190
        {
 
191
                if (isdigit((unsigned char) *(*ptr)))
 
192
                {
 
193
                        dest->digits[i++] = *(*ptr)++ - '0';
 
194
                        if (!have_dp)
 
195
                                dest->weight++;
 
196
                        else
 
197
                                dest->dscale++;
 
198
                }
 
199
                else if (*(*ptr) == '.')
 
200
                {
 
201
                        if (have_dp)
 
202
                        {
 
203
                                errno = PGTYPES_NUM_BAD_NUMERIC;
 
204
                                return -1;
 
205
                        }
 
206
                        have_dp = TRUE;
 
207
                        (*ptr)++;
 
208
                }
 
209
                else
 
210
                        break;
 
211
        }
 
212
        dest->ndigits = i;
 
213
 
 
214
        /* Handle exponent, if any */
 
215
        if (*(*ptr) == 'e' || *(*ptr) == 'E')
 
216
        {
 
217
                long            exponent;
 
218
                char       *endptr;
 
219
 
 
220
                (*ptr)++;
 
221
                exponent = strtol((*ptr), &endptr, 10);
 
222
                if (endptr == (*ptr))
 
223
                {
 
224
                        errno = PGTYPES_NUM_BAD_NUMERIC;
 
225
                        return -1;
 
226
                }
 
227
                (*ptr) = endptr;
 
228
                if (exponent > NUMERIC_MAX_PRECISION ||
 
229
                        exponent < -NUMERIC_MAX_PRECISION)
 
230
                {
 
231
                        errno = PGTYPES_NUM_BAD_NUMERIC;
 
232
                        return -1;
 
233
                }
 
234
                dest->weight += (int) exponent;
 
235
                dest->dscale -= (int) exponent;
 
236
                if (dest->dscale < 0)
 
237
                        dest->dscale = 0;
 
238
        }
 
239
 
 
240
        /* Should be nothing left but spaces */
 
241
        while (*(*ptr))
 
242
        {
 
243
                if (!isspace((unsigned char) *(*ptr)))
 
244
                {
 
245
                        errno = PGTYPES_NUM_BAD_NUMERIC;
 
246
                        return -1;
 
247
                }
 
248
                (*ptr)++;
 
249
        }
 
250
 
 
251
        /* Strip any leading zeroes */
 
252
        while (dest->ndigits > 0 && *(dest->digits) == 0)
 
253
        {
 
254
                (dest->digits)++;
 
255
                (dest->weight)--;
 
256
                (dest->ndigits)--;
 
257
        }
 
258
        if (dest->ndigits == 0)
 
259
                dest->weight = 0;
 
260
 
 
261
        dest->rscale = dest->dscale;
 
262
        return (0);
 
263
}
 
264
 
 
265
 
 
266
/* ----------
 
267
 * get_str_from_var() -
 
268
 *
 
269
 *      Convert a var to text representation (guts of numeric_out).
 
270
 *      CAUTION: var's contents may be modified by rounding!
 
271
 * ----------
 
272
 */
 
273
static char *
 
274
get_str_from_var(numeric *var, int dscale)
 
275
{
 
276
        char       *str;
 
277
        char       *cp;
 
278
        int                     i;
 
279
        int                     d;
 
280
 
 
281
        /*
 
282
         * Check if we must round up before printing the value and do so.
 
283
         */
 
284
        i = dscale + var->weight + 1;
 
285
        if (i >= 0 && var->ndigits > i)
 
286
        {
 
287
                int                     carry = (var->digits[i] > 4) ? 1 : 0;
 
288
 
 
289
                var->ndigits = i;
 
290
 
 
291
                while (carry)
 
292
                {
 
293
                        carry += var->digits[--i];
 
294
                        var->digits[i] = carry % 10;
 
295
                        carry /= 10;
 
296
                }
 
297
 
 
298
                if (i < 0)
 
299
                {
 
300
                        var->digits--;
 
301
                        var->ndigits++;
 
302
                        var->weight++;
 
303
                }
 
304
        }
 
305
        else
 
306
                var->ndigits = Max(0, Min(i, var->ndigits));
 
307
 
 
308
        /*
 
309
         * Allocate space for the result
 
310
         */
 
311
        if ((str = (char *) pgtypes_alloc(Max(0, dscale) + Max(0, var->weight) + 4)) == NULL)
 
312
                return NULL;
 
313
        cp = str;
 
314
 
 
315
        /*
 
316
         * Output a dash for negative values
 
317
         */
 
318
        if (var->sign == NUMERIC_NEG)
 
319
                *cp++ = '-';
 
320
 
 
321
        /*
 
322
         * Output all digits before the decimal point
 
323
         */
 
324
        i = Max(var->weight, 0);
 
325
        d = 0;
 
326
 
 
327
        while (i >= 0)
 
328
        {
 
329
                if (i <= var->weight && d < var->ndigits)
 
330
                        *cp++ = var->digits[d++] + '0';
 
331
                else
 
332
                        *cp++ = '0';
 
333
                i--;
 
334
        }
 
335
 
 
336
        /*
 
337
         * If requested, output a decimal point and all the digits that follow
 
338
         * it.
 
339
         */
 
340
        if (dscale > 0)
 
341
        {
 
342
                *cp++ = '.';
 
343
                while (i >= -dscale)
 
344
                {
 
345
                        if (i <= var->weight && d < var->ndigits)
 
346
                                *cp++ = var->digits[d++] + '0';
 
347
                        else
 
348
                                *cp++ = '0';
 
349
                        i--;
 
350
                }
 
351
        }
 
352
 
 
353
        /*
 
354
         * terminate the string and return it
 
355
         */
 
356
        *cp = '\0';
 
357
        return str;
 
358
}
 
359
 
 
360
numeric *
 
361
PGTYPESnumeric_from_asc(char *str, char **endptr)
 
362
{
 
363
        numeric    *value = (numeric *) pgtypes_alloc(sizeof(numeric));
 
364
        int                     ret;
 
365
 
 
366
#if 0
 
367
        long            typmod = -1;
 
368
#endif
 
369
        char       *realptr;
 
370
        char      **ptr = (endptr != NULL) ? endptr : &realptr;
 
371
 
 
372
        if (!value)
 
373
                return (NULL);
 
374
 
 
375
        ret = set_var_from_str(str, ptr, value);
 
376
        if (ret)
 
377
                return (NULL);
 
378
 
 
379
#if 0
 
380
        ret = apply_typmod(value, typmod);
 
381
        if (ret)
 
382
                return (NULL);
 
383
#endif
 
384
        return (value);
 
385
}
 
386
 
 
387
char *
 
388
PGTYPESnumeric_to_asc(numeric *num, int dscale)
 
389
{
 
390
        if (dscale < 0)
 
391
                dscale = num->dscale;
 
392
 
 
393
        return (get_str_from_var(num, dscale));
 
394
}
 
395
 
 
396
/* ----------
 
397
 * zero_var() -
 
398
 *
 
399
 *      Set a variable to ZERO.
 
400
 *      Note: rscale and dscale are not touched.
 
401
 * ----------
 
402
 */
 
403
static void
 
404
zero_var(numeric *var)
 
405
{
 
406
        digitbuf_free(var->buf);
 
407
        var->buf = NULL;
 
408
        var->digits = NULL;
 
409
        var->ndigits = 0;
 
410
        var->weight = 0;                        /* by convention; doesn't really matter */
 
411
        var->sign = NUMERIC_POS;        /* anything but NAN... */
 
412
}
 
413
 
 
414
void
 
415
PGTYPESnumeric_free(numeric *var)
 
416
{
 
417
        digitbuf_free(var->buf);
 
418
        free(var);
 
419
}
 
420
 
 
421
/* ----------
 
422
 * cmp_abs() -
 
423
 *
 
424
 *      Compare the absolute values of var1 and var2
 
425
 *      Returns:        -1 for ABS(var1) < ABS(var2)
 
426
 *                              0  for ABS(var1) == ABS(var2)
 
427
 *                              1  for ABS(var1) > ABS(var2)
 
428
 * ----------
 
429
 */
 
430
static int
 
431
cmp_abs(numeric *var1, numeric *var2)
 
432
{
 
433
        int                     i1 = 0;
 
434
        int                     i2 = 0;
 
435
        int                     w1 = var1->weight;
 
436
        int                     w2 = var2->weight;
 
437
        int                     stat;
 
438
 
 
439
        while (w1 > w2 && i1 < var1->ndigits)
 
440
        {
 
441
                if (var1->digits[i1++] != 0)
 
442
                        return 1;
 
443
                w1--;
 
444
        }
 
445
        while (w2 > w1 && i2 < var2->ndigits)
 
446
        {
 
447
                if (var2->digits[i2++] != 0)
 
448
                        return -1;
 
449
                w2--;
 
450
        }
 
451
 
 
452
        if (w1 == w2)
 
453
        {
 
454
                while (i1 < var1->ndigits && i2 < var2->ndigits)
 
455
                {
 
456
                        stat = var1->digits[i1++] - var2->digits[i2++];
 
457
                        if (stat)
 
458
                        {
 
459
                                if (stat > 0)
 
460
                                        return 1;
 
461
                                return -1;
 
462
                        }
 
463
                }
 
464
        }
 
465
 
 
466
        while (i1 < var1->ndigits)
 
467
        {
 
468
                if (var1->digits[i1++] != 0)
 
469
                        return 1;
 
470
        }
 
471
        while (i2 < var2->ndigits)
 
472
        {
 
473
                if (var2->digits[i2++] != 0)
 
474
                        return -1;
 
475
        }
 
476
 
 
477
        return 0;
 
478
}
 
479
 
 
480
 
 
481
/* ----------
 
482
 * add_abs() -
 
483
 *
 
484
 *      Add the absolute values of two variables into result.
 
485
 *      result might point to one of the operands without danger.
 
486
 * ----------
 
487
 */
 
488
static int
 
489
add_abs(numeric *var1, numeric *var2, numeric *result)
 
490
{
 
491
        NumericDigit *res_buf;
 
492
        NumericDigit *res_digits;
 
493
        int                     res_ndigits;
 
494
        int                     res_weight;
 
495
        int                     res_rscale;
 
496
        int                     res_dscale;
 
497
        int                     i,
 
498
                                i1,
 
499
                                i2;
 
500
        int                     carry = 0;
 
501
 
 
502
        /* copy these values into local vars for speed in inner loop */
 
503
        int                     var1ndigits = var1->ndigits;
 
504
        int                     var2ndigits = var2->ndigits;
 
505
        NumericDigit *var1digits = var1->digits;
 
506
        NumericDigit *var2digits = var2->digits;
 
507
 
 
508
        res_weight = Max(var1->weight, var2->weight) + 1;
 
509
        res_rscale = Max(var1->rscale, var2->rscale);
 
510
        res_dscale = Max(var1->dscale, var2->dscale);
 
511
        res_ndigits = res_rscale + res_weight + 1;
 
512
        if (res_ndigits <= 0)
 
513
                res_ndigits = 1;
 
514
 
 
515
        if ((res_buf = digitbuf_alloc(res_ndigits)) == NULL)
 
516
                return -1;
 
517
        res_digits = res_buf;
 
518
 
 
519
        i1 = res_rscale + var1->weight + 1;
 
520
        i2 = res_rscale + var2->weight + 1;
 
521
        for (i = res_ndigits - 1; i >= 0; i--)
 
522
        {
 
523
                i1--;
 
524
                i2--;
 
525
                if (i1 >= 0 && i1 < var1ndigits)
 
526
                        carry += var1digits[i1];
 
527
                if (i2 >= 0 && i2 < var2ndigits)
 
528
                        carry += var2digits[i2];
 
529
 
 
530
                if (carry >= 10)
 
531
                {
 
532
                        res_digits[i] = carry - 10;
 
533
                        carry = 1;
 
534
                }
 
535
                else
 
536
                {
 
537
                        res_digits[i] = carry;
 
538
                        carry = 0;
 
539
                }
 
540
        }
 
541
 
 
542
        while (res_ndigits > 0 && *res_digits == 0)
 
543
        {
 
544
                res_digits++;
 
545
                res_weight--;
 
546
                res_ndigits--;
 
547
        }
 
548
        while (res_ndigits > 0 && res_digits[res_ndigits - 1] == 0)
 
549
                res_ndigits--;
 
550
 
 
551
        if (res_ndigits == 0)
 
552
                res_weight = 0;
 
553
 
 
554
        digitbuf_free(result->buf);
 
555
        result->ndigits = res_ndigits;
 
556
        result->buf = res_buf;
 
557
        result->digits = res_digits;
 
558
        result->weight = res_weight;
 
559
        result->rscale = res_rscale;
 
560
        result->dscale = res_dscale;
 
561
 
 
562
        return 0;
 
563
}
 
564
 
 
565
 
 
566
/* ----------
 
567
 * sub_abs() -
 
568
 *
 
569
 *      Subtract the absolute value of var2 from the absolute value of var1
 
570
 *      and store in result. result might point to one of the operands
 
571
 *      without danger.
 
572
 *
 
573
 *      ABS(var1) MUST BE GREATER OR EQUAL ABS(var2) !!!
 
574
 * ----------
 
575
 */
 
576
static int
 
577
sub_abs(numeric *var1, numeric *var2, numeric *result)
 
578
{
 
579
        NumericDigit *res_buf;
 
580
        NumericDigit *res_digits;
 
581
        int                     res_ndigits;
 
582
        int                     res_weight;
 
583
        int                     res_rscale;
 
584
        int                     res_dscale;
 
585
        int                     i,
 
586
                                i1,
 
587
                                i2;
 
588
        int                     borrow = 0;
 
589
 
 
590
        /* copy these values into local vars for speed in inner loop */
 
591
        int                     var1ndigits = var1->ndigits;
 
592
        int                     var2ndigits = var2->ndigits;
 
593
        NumericDigit *var1digits = var1->digits;
 
594
        NumericDigit *var2digits = var2->digits;
 
595
 
 
596
        res_weight = var1->weight;
 
597
        res_rscale = Max(var1->rscale, var2->rscale);
 
598
        res_dscale = Max(var1->dscale, var2->dscale);
 
599
        res_ndigits = res_rscale + res_weight + 1;
 
600
        if (res_ndigits <= 0)
 
601
                res_ndigits = 1;
 
602
 
 
603
        if ((res_buf = digitbuf_alloc(res_ndigits)) == NULL)
 
604
                return -1;
 
605
        res_digits = res_buf;
 
606
 
 
607
        i1 = res_rscale + var1->weight + 1;
 
608
        i2 = res_rscale + var2->weight + 1;
 
609
        for (i = res_ndigits - 1; i >= 0; i--)
 
610
        {
 
611
                i1--;
 
612
                i2--;
 
613
                if (i1 >= 0 && i1 < var1ndigits)
 
614
                        borrow += var1digits[i1];
 
615
                if (i2 >= 0 && i2 < var2ndigits)
 
616
                        borrow -= var2digits[i2];
 
617
 
 
618
                if (borrow < 0)
 
619
                {
 
620
                        res_digits[i] = borrow + 10;
 
621
                        borrow = -1;
 
622
                }
 
623
                else
 
624
                {
 
625
                        res_digits[i] = borrow;
 
626
                        borrow = 0;
 
627
                }
 
628
        }
 
629
 
 
630
        while (res_ndigits > 0 && *res_digits == 0)
 
631
        {
 
632
                res_digits++;
 
633
                res_weight--;
 
634
                res_ndigits--;
 
635
        }
 
636
        while (res_ndigits > 0 && res_digits[res_ndigits - 1] == 0)
 
637
                res_ndigits--;
 
638
 
 
639
        if (res_ndigits == 0)
 
640
                res_weight = 0;
 
641
 
 
642
        digitbuf_free(result->buf);
 
643
        result->ndigits = res_ndigits;
 
644
        result->buf = res_buf;
 
645
        result->digits = res_digits;
 
646
        result->weight = res_weight;
 
647
        result->rscale = res_rscale;
 
648
        result->dscale = res_dscale;
 
649
 
 
650
        return 0;
 
651
}
 
652
 
 
653
/* ----------
 
654
 * add_var() -
 
655
 *
 
656
 *      Full version of add functionality on variable level (handling signs).
 
657
 *      result might point to one of the operands too without danger.
 
658
 * ----------
 
659
 */
 
660
int
 
661
PGTYPESnumeric_add(numeric *var1, numeric *var2, numeric *result)
 
662
{
 
663
        /*
 
664
         * Decide on the signs of the two variables what to do
 
665
         */
 
666
        if (var1->sign == NUMERIC_POS)
 
667
        {
 
668
                if (var2->sign == NUMERIC_POS)
 
669
                {
 
670
                        /*
 
671
                         * Both are positive result = +(ABS(var1) + ABS(var2))
 
672
                         */
 
673
                        if (add_abs(var1, var2, result) != 0)
 
674
                                return -1;
 
675
                        result->sign = NUMERIC_POS;
 
676
                }
 
677
                else
 
678
                {
 
679
                        /*
 
680
                         * var1 is positive, var2 is negative Must compare absolute
 
681
                         * values
 
682
                         */
 
683
                        switch (cmp_abs(var1, var2))
 
684
                        {
 
685
                                case 0:
 
686
                                        /* ----------
 
687
                                         * ABS(var1) == ABS(var2)
 
688
                                         * result = ZERO
 
689
                                         * ----------
 
690
                                         */
 
691
                                        zero_var(result);
 
692
                                        result->rscale = Max(var1->rscale, var2->rscale);
 
693
                                        result->dscale = Max(var1->dscale, var2->dscale);
 
694
                                        break;
 
695
 
 
696
                                case 1:
 
697
                                        /* ----------
 
698
                                         * ABS(var1) > ABS(var2)
 
699
                                         * result = +(ABS(var1) - ABS(var2))
 
700
                                         * ----------
 
701
                                         */
 
702
                                        if (sub_abs(var1, var2, result) != 0)
 
703
                                                return -1;
 
704
                                        result->sign = NUMERIC_POS;
 
705
                                        break;
 
706
 
 
707
                                case -1:
 
708
                                        /* ----------
 
709
                                         * ABS(var1) < ABS(var2)
 
710
                                         * result = -(ABS(var2) - ABS(var1))
 
711
                                         * ----------
 
712
                                         */
 
713
                                        if (sub_abs(var2, var1, result) != 0)
 
714
                                                return -1;
 
715
                                        result->sign = NUMERIC_NEG;
 
716
                                        break;
 
717
                        }
 
718
                }
 
719
        }
 
720
        else
 
721
        {
 
722
                if (var2->sign == NUMERIC_POS)
 
723
                {
 
724
                        /* ----------
 
725
                         * var1 is negative, var2 is positive
 
726
                         * Must compare absolute values
 
727
                         * ----------
 
728
                         */
 
729
                        switch (cmp_abs(var1, var2))
 
730
                        {
 
731
                                case 0:
 
732
                                        /* ----------
 
733
                                         * ABS(var1) == ABS(var2)
 
734
                                         * result = ZERO
 
735
                                         * ----------
 
736
                                         */
 
737
                                        zero_var(result);
 
738
                                        result->rscale = Max(var1->rscale, var2->rscale);
 
739
                                        result->dscale = Max(var1->dscale, var2->dscale);
 
740
                                        break;
 
741
 
 
742
                                case 1:
 
743
                                        /* ----------
 
744
                                         * ABS(var1) > ABS(var2)
 
745
                                         * result = -(ABS(var1) - ABS(var2))
 
746
                                         * ----------
 
747
                                         */
 
748
                                        if (sub_abs(var1, var2, result) != 0)
 
749
                                                return -1;
 
750
                                        result->sign = NUMERIC_NEG;
 
751
                                        break;
 
752
 
 
753
                                case -1:
 
754
                                        /* ----------
 
755
                                         * ABS(var1) < ABS(var2)
 
756
                                         * result = +(ABS(var2) - ABS(var1))
 
757
                                         * ----------
 
758
                                         */
 
759
                                        if (sub_abs(var2, var1, result) != 0)
 
760
                                                return -1;
 
761
                                        result->sign = NUMERIC_POS;
 
762
                                        break;
 
763
                        }
 
764
                }
 
765
                else
 
766
                {
 
767
                        /* ----------
 
768
                         * Both are negative
 
769
                         * result = -(ABS(var1) + ABS(var2))
 
770
                         * ----------
 
771
                         */
 
772
                        if (add_abs(var1, var2, result) != 0)
 
773
                                return -1;
 
774
                        result->sign = NUMERIC_NEG;
 
775
                }
 
776
        }
 
777
 
 
778
        return 0;
 
779
}
 
780
 
 
781
 
 
782
/* ----------
 
783
 * sub_var() -
 
784
 *
 
785
 *      Full version of sub functionality on variable level (handling signs).
 
786
 *      result might point to one of the operands too without danger.
 
787
 * ----------
 
788
 */
 
789
int
 
790
PGTYPESnumeric_sub(numeric *var1, numeric *var2, numeric *result)
 
791
{
 
792
        /*
 
793
         * Decide on the signs of the two variables what to do
 
794
         */
 
795
        if (var1->sign == NUMERIC_POS)
 
796
        {
 
797
                if (var2->sign == NUMERIC_NEG)
 
798
                {
 
799
                        /* ----------
 
800
                         * var1 is positive, var2 is negative
 
801
                         * result = +(ABS(var1) + ABS(var2))
 
802
                         * ----------
 
803
                         */
 
804
                        if (add_abs(var1, var2, result) != 0)
 
805
                                return -1;
 
806
                        result->sign = NUMERIC_POS;
 
807
                }
 
808
                else
 
809
                {
 
810
                        /* ----------
 
811
                         * Both are positive
 
812
                         * Must compare absolute values
 
813
                         * ----------
 
814
                         */
 
815
                        switch (cmp_abs(var1, var2))
 
816
                        {
 
817
                                case 0:
 
818
                                        /* ----------
 
819
                                         * ABS(var1) == ABS(var2)
 
820
                                         * result = ZERO
 
821
                                         * ----------
 
822
                                         */
 
823
                                        zero_var(result);
 
824
                                        result->rscale = Max(var1->rscale, var2->rscale);
 
825
                                        result->dscale = Max(var1->dscale, var2->dscale);
 
826
                                        break;
 
827
 
 
828
                                case 1:
 
829
                                        /* ----------
 
830
                                         * ABS(var1) > ABS(var2)
 
831
                                         * result = +(ABS(var1) - ABS(var2))
 
832
                                         * ----------
 
833
                                         */
 
834
                                        if (sub_abs(var1, var2, result) != 0)
 
835
                                                return -1;
 
836
                                        result->sign = NUMERIC_POS;
 
837
                                        break;
 
838
 
 
839
                                case -1:
 
840
                                        /* ----------
 
841
                                         * ABS(var1) < ABS(var2)
 
842
                                         * result = -(ABS(var2) - ABS(var1))
 
843
                                         * ----------
 
844
                                         */
 
845
                                        if (sub_abs(var2, var1, result) != 0)
 
846
                                                return -1;
 
847
                                        result->sign = NUMERIC_NEG;
 
848
                                        break;
 
849
                        }
 
850
                }
 
851
        }
 
852
        else
 
853
        {
 
854
                if (var2->sign == NUMERIC_NEG)
 
855
                {
 
856
                        /* ----------
 
857
                         * Both are negative
 
858
                         * Must compare absolute values
 
859
                         * ----------
 
860
                         */
 
861
                        switch (cmp_abs(var1, var2))
 
862
                        {
 
863
                                case 0:
 
864
                                        /* ----------
 
865
                                         * ABS(var1) == ABS(var2)
 
866
                                         * result = ZERO
 
867
                                         * ----------
 
868
                                         */
 
869
                                        zero_var(result);
 
870
                                        result->rscale = Max(var1->rscale, var2->rscale);
 
871
                                        result->dscale = Max(var1->dscale, var2->dscale);
 
872
                                        break;
 
873
 
 
874
                                case 1:
 
875
                                        /* ----------
 
876
                                         * ABS(var1) > ABS(var2)
 
877
                                         * result = -(ABS(var1) - ABS(var2))
 
878
                                         * ----------
 
879
                                         */
 
880
                                        if (sub_abs(var1, var2, result) != 0)
 
881
                                                return -1;
 
882
                                        result->sign = NUMERIC_NEG;
 
883
                                        break;
 
884
 
 
885
                                case -1:
 
886
                                        /* ----------
 
887
                                         * ABS(var1) < ABS(var2)
 
888
                                         * result = +(ABS(var2) - ABS(var1))
 
889
                                         * ----------
 
890
                                         */
 
891
                                        if (sub_abs(var2, var1, result) != 0)
 
892
                                                return -1;
 
893
                                        result->sign = NUMERIC_POS;
 
894
                                        break;
 
895
                        }
 
896
                }
 
897
                else
 
898
                {
 
899
                        /* ----------
 
900
                         * var1 is negative, var2 is positive
 
901
                         * result = -(ABS(var1) + ABS(var2))
 
902
                         * ----------
 
903
                         */
 
904
                        if (add_abs(var1, var2, result) != 0)
 
905
                                return -1;
 
906
                        result->sign = NUMERIC_NEG;
 
907
                }
 
908
        }
 
909
 
 
910
        return 0;
 
911
}
 
912
 
 
913
/* ----------
 
914
 * mul_var() -
 
915
 *
 
916
 *      Multiplication on variable level. Product of var1 * var2 is stored
 
917
 *      in result.      Accuracy of result is determined by global_rscale.
 
918
 * ----------
 
919
 */
 
920
int
 
921
PGTYPESnumeric_mul(numeric *var1, numeric *var2, numeric *result)
 
922
{
 
923
        NumericDigit *res_buf;
 
924
        NumericDigit *res_digits;
 
925
        int                     res_ndigits;
 
926
        int                     res_weight;
 
927
        int                     res_sign;
 
928
        int                     i,
 
929
                                ri,
 
930
                                i1,
 
931
                                i2;
 
932
        long            sum = 0;
 
933
        int                     global_rscale = var1->rscale + var2->rscale;
 
934
 
 
935
        res_weight = var1->weight + var2->weight + 2;
 
936
        res_ndigits = var1->ndigits + var2->ndigits + 1;
 
937
        if (var1->sign == var2->sign)
 
938
                res_sign = NUMERIC_POS;
 
939
        else
 
940
                res_sign = NUMERIC_NEG;
 
941
 
 
942
        if ((res_buf = digitbuf_alloc(res_ndigits)) == NULL)
 
943
                return -1;
 
944
        res_digits = res_buf;
 
945
        memset(res_digits, 0, res_ndigits);
 
946
 
 
947
        ri = res_ndigits;
 
948
        for (i1 = var1->ndigits - 1; i1 >= 0; i1--)
 
949
        {
 
950
                sum = 0;
 
951
                i = --ri;
 
952
 
 
953
                for (i2 = var2->ndigits - 1; i2 >= 0; i2--)
 
954
                {
 
955
                        sum += res_digits[i] + var1->digits[i1] * var2->digits[i2];
 
956
                        res_digits[i--] = sum % 10;
 
957
                        sum /= 10;
 
958
                }
 
959
                res_digits[i] = sum;
 
960
        }
 
961
 
 
962
        i = res_weight + global_rscale + 2;
 
963
        if (i >= 0 && i < res_ndigits)
 
964
        {
 
965
                sum = (res_digits[i] > 4) ? 1 : 0;
 
966
                res_ndigits = i;
 
967
                i--;
 
968
                while (sum)
 
969
                {
 
970
                        sum += res_digits[i];
 
971
                        res_digits[i--] = sum % 10;
 
972
                        sum /= 10;
 
973
                }
 
974
        }
 
975
 
 
976
        while (res_ndigits > 0 && *res_digits == 0)
 
977
        {
 
978
                res_digits++;
 
979
                res_weight--;
 
980
                res_ndigits--;
 
981
        }
 
982
        while (res_ndigits > 0 && res_digits[res_ndigits - 1] == 0)
 
983
                res_ndigits--;
 
984
 
 
985
        if (res_ndigits == 0)
 
986
        {
 
987
                res_sign = NUMERIC_POS;
 
988
                res_weight = 0;
 
989
        }
 
990
 
 
991
        digitbuf_free(result->buf);
 
992
        result->buf = res_buf;
 
993
        result->digits = res_digits;
 
994
        result->ndigits = res_ndigits;
 
995
        result->weight = res_weight;
 
996
        result->rscale = global_rscale;
 
997
        result->sign = res_sign;
 
998
        result->dscale = var1->dscale + var2->dscale;
 
999
 
 
1000
        return 0;
 
1001
}
 
1002
 
 
1003
/*
 
1004
 * Default scale selection for division
 
1005
 *
 
1006
 * Returns the appropriate display scale for the division result,
 
1007
 * and sets global_rscale to the result scale to use during div_var.
 
1008
 *
 
1009
 * Note that this must be called before div_var.
 
1010
 */
 
1011
static int
 
1012
select_div_scale(numeric *var1, numeric *var2, int *rscale)
 
1013
{
 
1014
        int                     weight1,
 
1015
                                weight2,
 
1016
                                qweight,
 
1017
                                i;
 
1018
        NumericDigit firstdigit1,
 
1019
                                firstdigit2;
 
1020
        int                     res_dscale;
 
1021
        int                     res_rscale;
 
1022
 
 
1023
        /*
 
1024
         * The result scale of a division isn't specified in any SQL standard.
 
1025
         * For PostgreSQL we select a display scale that will give at least
 
1026
         * NUMERIC_MIN_SIG_DIGITS significant digits, so that numeric gives a
 
1027
         * result no less accurate than float8; but use a scale not less than
 
1028
         * either input's display scale.
 
1029
         */
 
1030
 
 
1031
        /* Get the actual (normalized) weight and first digit of each input */
 
1032
 
 
1033
        weight1 = 0;                            /* values to use if var1 is zero */
 
1034
        firstdigit1 = 0;
 
1035
        for (i = 0; i < var1->ndigits; i++)
 
1036
        {
 
1037
                firstdigit1 = var1->digits[i];
 
1038
                if (firstdigit1 != 0)
 
1039
                {
 
1040
                        weight1 = var1->weight - i;
 
1041
                        break;
 
1042
                }
 
1043
        }
 
1044
 
 
1045
        weight2 = 0;                            /* values to use if var2 is zero */
 
1046
        firstdigit2 = 0;
 
1047
        for (i = 0; i < var2->ndigits; i++)
 
1048
        {
 
1049
                firstdigit2 = var2->digits[i];
 
1050
                if (firstdigit2 != 0)
 
1051
                {
 
1052
                        weight2 = var2->weight - i;
 
1053
                        break;
 
1054
                }
 
1055
        }
 
1056
 
 
1057
        /*
 
1058
         * Estimate weight of quotient.  If the two first digits are equal, we
 
1059
         * can't be sure, but assume that var1 is less than var2.
 
1060
         */
 
1061
        qweight = weight1 - weight2;
 
1062
        if (firstdigit1 <= firstdigit2)
 
1063
                qweight--;
 
1064
 
 
1065
        /* Select display scale */
 
1066
        res_dscale = NUMERIC_MIN_SIG_DIGITS - qweight;
 
1067
        res_dscale = Max(res_dscale, var1->dscale);
 
1068
        res_dscale = Max(res_dscale, var2->dscale);
 
1069
        res_dscale = Max(res_dscale, NUMERIC_MIN_DISPLAY_SCALE);
 
1070
        res_dscale = Min(res_dscale, NUMERIC_MAX_DISPLAY_SCALE);
 
1071
 
 
1072
        /* Select result scale */
 
1073
        *rscale = res_rscale = res_dscale + 4;
 
1074
 
 
1075
        return res_dscale;
 
1076
}
 
1077
 
 
1078
int
 
1079
PGTYPESnumeric_div(numeric *var1, numeric *var2, numeric *result)
 
1080
{
 
1081
        NumericDigit *res_digits;
 
1082
        int                     res_ndigits;
 
1083
        int                     res_sign;
 
1084
        int                     res_weight;
 
1085
        numeric         dividend;
 
1086
        numeric         divisor[10];
 
1087
        int                     ndigits_tmp;
 
1088
        int                     weight_tmp;
 
1089
        int                     rscale_tmp;
 
1090
        int                     ri;
 
1091
        int                     i;
 
1092
        long            guess;
 
1093
        long            first_have;
 
1094
        long            first_div;
 
1095
        int                     first_nextdigit;
 
1096
        int                     stat = 0;
 
1097
        int                     rscale;
 
1098
        int                     res_dscale = select_div_scale(var1, var2, &rscale);
 
1099
 
 
1100
        /*
 
1101
         * First of all division by zero check
 
1102
         */
 
1103
        ndigits_tmp = var2->ndigits + 1;
 
1104
        if (ndigits_tmp == 1)
 
1105
        {
 
1106
                errno = PGTYPES_NUM_DIVIDE_ZERO;
 
1107
                return -1;
 
1108
        }
 
1109
 
 
1110
        /*
 
1111
         * Determine the result sign, weight and number of digits to calculate
 
1112
         */
 
1113
        if (var1->sign == var2->sign)
 
1114
                res_sign = NUMERIC_POS;
 
1115
        else
 
1116
                res_sign = NUMERIC_NEG;
 
1117
        res_weight = var1->weight - var2->weight + 1;
 
1118
        res_ndigits = rscale + res_weight;
 
1119
        if (res_ndigits <= 0)
 
1120
                res_ndigits = 1;
 
1121
 
 
1122
        /*
 
1123
         * Now result zero check
 
1124
         */
 
1125
        if (var1->ndigits == 0)
 
1126
        {
 
1127
                zero_var(result);
 
1128
                result->rscale = rscale;
 
1129
                return 0;
 
1130
        }
 
1131
 
 
1132
        /*
 
1133
         * Initialize local variables
 
1134
         */
 
1135
        init_var(&dividend);
 
1136
        for (i = 1; i < 10; i++)
 
1137
                init_var(&divisor[i]);
 
1138
 
 
1139
        /*
 
1140
         * Make a copy of the divisor which has one leading zero digit
 
1141
         */
 
1142
        divisor[1].ndigits = ndigits_tmp;
 
1143
        divisor[1].rscale = var2->ndigits;
 
1144
        divisor[1].sign = NUMERIC_POS;
 
1145
        divisor[1].buf = digitbuf_alloc(ndigits_tmp);
 
1146
        divisor[1].digits = divisor[1].buf;
 
1147
        divisor[1].digits[0] = 0;
 
1148
        memcpy(&(divisor[1].digits[1]), var2->digits, ndigits_tmp - 1);
 
1149
 
 
1150
        /*
 
1151
         * Make a copy of the dividend
 
1152
         */
 
1153
        dividend.ndigits = var1->ndigits;
 
1154
        dividend.weight = 0;
 
1155
        dividend.rscale = var1->ndigits;
 
1156
        dividend.sign = NUMERIC_POS;
 
1157
        dividend.buf = digitbuf_alloc(var1->ndigits);
 
1158
        dividend.digits = dividend.buf;
 
1159
        memcpy(dividend.digits, var1->digits, var1->ndigits);
 
1160
 
 
1161
        /*
 
1162
         * Setup the result
 
1163
         */
 
1164
        digitbuf_free(result->buf);
 
1165
        result->buf = digitbuf_alloc(res_ndigits + 2);
 
1166
        res_digits = result->buf;
 
1167
        result->digits = res_digits;
 
1168
        result->ndigits = res_ndigits;
 
1169
        result->weight = res_weight;
 
1170
        result->rscale = rscale;
 
1171
        result->sign = res_sign;
 
1172
        res_digits[0] = 0;
 
1173
 
 
1174
        first_div = divisor[1].digits[1] * 10;
 
1175
        if (ndigits_tmp > 2)
 
1176
                first_div += divisor[1].digits[2];
 
1177
 
 
1178
        first_have = 0;
 
1179
        first_nextdigit = 0;
 
1180
 
 
1181
        weight_tmp = 1;
 
1182
        rscale_tmp = divisor[1].rscale;
 
1183
 
 
1184
        for (ri = 0; ri <= res_ndigits; ri++)
 
1185
        {
 
1186
                first_have = first_have * 10;
 
1187
                if (first_nextdigit >= 0 && first_nextdigit < dividend.ndigits)
 
1188
                        first_have += dividend.digits[first_nextdigit];
 
1189
                first_nextdigit++;
 
1190
 
 
1191
                guess = (first_have * 10) / first_div + 1;
 
1192
                if (guess > 9)
 
1193
                        guess = 9;
 
1194
 
 
1195
                while (guess > 0)
 
1196
                {
 
1197
                        if (divisor[guess].buf == NULL)
 
1198
                        {
 
1199
                                int                     i;
 
1200
                                long            sum = 0;
 
1201
 
 
1202
                                memcpy(&divisor[guess], &divisor[1], sizeof(numeric));
 
1203
                                divisor[guess].buf = digitbuf_alloc(divisor[guess].ndigits);
 
1204
                                divisor[guess].digits = divisor[guess].buf;
 
1205
                                for (i = divisor[1].ndigits - 1; i >= 0; i--)
 
1206
                                {
 
1207
                                        sum += divisor[1].digits[i] * guess;
 
1208
                                        divisor[guess].digits[i] = sum % 10;
 
1209
                                        sum /= 10;
 
1210
                                }
 
1211
                        }
 
1212
 
 
1213
                        divisor[guess].weight = weight_tmp;
 
1214
                        divisor[guess].rscale = rscale_tmp;
 
1215
 
 
1216
                        stat = cmp_abs(&dividend, &divisor[guess]);
 
1217
                        if (stat >= 0)
 
1218
                                break;
 
1219
 
 
1220
                        guess--;
 
1221
                }
 
1222
 
 
1223
                res_digits[ri + 1] = guess;
 
1224
                if (stat == 0)
 
1225
                {
 
1226
                        ri++;
 
1227
                        break;
 
1228
                }
 
1229
 
 
1230
                weight_tmp--;
 
1231
                rscale_tmp++;
 
1232
 
 
1233
                if (guess == 0)
 
1234
                        continue;
 
1235
 
 
1236
                sub_abs(&dividend, &divisor[guess], &dividend);
 
1237
 
 
1238
                first_nextdigit = dividend.weight - weight_tmp;
 
1239
                first_have = 0;
 
1240
                if (first_nextdigit >= 0 && first_nextdigit < dividend.ndigits)
 
1241
                        first_have = dividend.digits[first_nextdigit];
 
1242
                first_nextdigit++;
 
1243
        }
 
1244
 
 
1245
        result->ndigits = ri + 1;
 
1246
        if (ri == res_ndigits + 1)
 
1247
        {
 
1248
                int                     carry = (res_digits[ri] > 4) ? 1 : 0;
 
1249
 
 
1250
                result->ndigits = ri;
 
1251
                res_digits[ri] = 0;
 
1252
 
 
1253
                while (carry && ri > 0)
 
1254
                {
 
1255
                        carry += res_digits[--ri];
 
1256
                        res_digits[ri] = carry % 10;
 
1257
                        carry /= 10;
 
1258
                }
 
1259
        }
 
1260
 
 
1261
        while (result->ndigits > 0 && *(result->digits) == 0)
 
1262
        {
 
1263
                (result->digits)++;
 
1264
                (result->weight)--;
 
1265
                (result->ndigits)--;
 
1266
        }
 
1267
        while (result->ndigits > 0 && result->digits[result->ndigits - 1] == 0)
 
1268
                (result->ndigits)--;
 
1269
        if (result->ndigits == 0)
 
1270
                result->sign = NUMERIC_POS;
 
1271
 
 
1272
        /*
 
1273
         * Tidy up
 
1274
         */
 
1275
        digitbuf_free(dividend.buf);
 
1276
        for (i = 1; i < 10; i++)
 
1277
                digitbuf_free(divisor[i].buf);
 
1278
 
 
1279
        result->dscale = res_dscale;
 
1280
        return 0;
 
1281
}
 
1282
 
 
1283
 
 
1284
int
 
1285
PGTYPESnumeric_cmp(numeric *var1, numeric *var2)
 
1286
{
 
1287
 
 
1288
        /* use cmp_abs function to calculate the result */
 
1289
 
 
1290
        /* both are positive: normal comparation with cmp_abs */
 
1291
        if (var1->sign == NUMERIC_POS && var2->sign == NUMERIC_POS)
 
1292
                return cmp_abs(var1, var2);
 
1293
 
 
1294
        /* both are negative: return the inverse of the normal comparation */
 
1295
        if (var1->sign == NUMERIC_NEG && var2->sign == NUMERIC_NEG)
 
1296
        {
 
1297
                /*
 
1298
                 * instead of inverting the result, we invert the paramter
 
1299
                 * ordering
 
1300
                 */
 
1301
                return cmp_abs(var2, var1);
 
1302
        }
 
1303
 
 
1304
        /* one is positive, one is negative: trivial */
 
1305
        if (var1->sign == NUMERIC_POS && var2->sign == NUMERIC_NEG)
 
1306
                return 1;
 
1307
        if (var1->sign == NUMERIC_NEG && var2->sign == NUMERIC_POS)
 
1308
                return -1;
 
1309
 
 
1310
        errno = PGTYPES_NUM_BAD_NUMERIC;
 
1311
        return INT_MAX;
 
1312
 
 
1313
}
 
1314
 
 
1315
int
 
1316
PGTYPESnumeric_from_int(signed int int_val, numeric *var)
 
1317
{
 
1318
        /* implicit conversion */
 
1319
        signed long int long_int = int_val;
 
1320
 
 
1321
        return PGTYPESnumeric_from_long(long_int, var);
 
1322
}
 
1323
 
 
1324
int
 
1325
PGTYPESnumeric_from_long(signed long int long_val, numeric *var)
 
1326
{
 
1327
        /* calculate the size of the long int number */
 
1328
        /* a number n needs log_10 n digits */
 
1329
 
 
1330
        /*
 
1331
         * however we multiply by 10 each time and compare instead of
 
1332
         * calculating the logarithm
 
1333
         */
 
1334
 
 
1335
        int                     size = 0;
 
1336
        int                     i;
 
1337
        signed long int abs_long_val = long_val;
 
1338
        signed long int extract;
 
1339
        signed long int reach_limit;
 
1340
 
 
1341
        if (abs_long_val < 0)
 
1342
        {
 
1343
                abs_long_val *= -1;
 
1344
                var->sign = NUMERIC_NEG;
 
1345
        }
 
1346
        else
 
1347
                var->sign = NUMERIC_POS;
 
1348
 
 
1349
        reach_limit = 1;
 
1350
        do
 
1351
        {
 
1352
                size++;
 
1353
                reach_limit *= 10;
 
1354
        } while ((reach_limit - 1) < abs_long_val && reach_limit <= LONG_MAX / 10);
 
1355
 
 
1356
        if (reach_limit > LONG_MAX / 10)
 
1357
        {
 
1358
                /* add the first digit and a .0 */
 
1359
                size += 2;
 
1360
        }
 
1361
        else
 
1362
        {
 
1363
                /* always add a .0 */
 
1364
                size++;
 
1365
                reach_limit /= 10;
 
1366
        }
 
1367
 
 
1368
        if (alloc_var(var, size) < 0)
 
1369
                return -1;
 
1370
 
 
1371
        var->rscale = 1;
 
1372
        var->dscale = 1;
 
1373
        var->weight = size - 2;
 
1374
 
 
1375
        i = 0;
 
1376
        do
 
1377
        {
 
1378
                extract = abs_long_val - (abs_long_val % reach_limit);
 
1379
                var->digits[i] = extract / reach_limit;
 
1380
                abs_long_val -= extract;
 
1381
                i++;
 
1382
                reach_limit /= 10;
 
1383
 
 
1384
                /*
 
1385
                 * we can abandon if abs_long_val reaches 0, because the memory is
 
1386
                 * initialized properly and filled with '0', so converting 10000
 
1387
                 * in only one step is no problem
 
1388
                 */
 
1389
        } while (abs_long_val > 0);
 
1390
 
 
1391
        return 0;
 
1392
}
 
1393
 
 
1394
int
 
1395
PGTYPESnumeric_copy(numeric *src, numeric *dst)
 
1396
{
 
1397
        int                     i;
 
1398
 
 
1399
        if (dst == NULL)
 
1400
                return -1;
 
1401
        zero_var(dst);
 
1402
 
 
1403
        dst->weight = src->weight;
 
1404
        dst->rscale = src->rscale;
 
1405
        dst->dscale = src->dscale;
 
1406
        dst->sign = src->sign;
 
1407
 
 
1408
        if (alloc_var(dst, src->ndigits) != 0)
 
1409
                return -1;
 
1410
 
 
1411
        for (i = 0; i < src->ndigits; i++)
 
1412
                dst->digits[i] = src->digits[i];
 
1413
 
 
1414
        return 0;
 
1415
}
 
1416
 
 
1417
int
 
1418
PGTYPESnumeric_from_double(double d, numeric *dst)
 
1419
{
 
1420
        char            buffer[100];
 
1421
        numeric    *tmp;
 
1422
 
 
1423
        if (sprintf(buffer, "%f", d) == 0)
 
1424
                return -1;
 
1425
 
 
1426
        if ((tmp = PGTYPESnumeric_from_asc(buffer, NULL)) == NULL)
 
1427
                return -1;
 
1428
        if (PGTYPESnumeric_copy(tmp, dst) != 0)
 
1429
                return -1;
 
1430
        PGTYPESnumeric_free(tmp);
 
1431
        return 0;
 
1432
}
 
1433
 
 
1434
static int
 
1435
numericvar_to_double_no_overflow(numeric *var, double *dp)
 
1436
{
 
1437
        char       *tmp;
 
1438
        double          val;
 
1439
        char       *endptr;
 
1440
 
 
1441
        if ((tmp = get_str_from_var(var, var->dscale)) == NULL)
 
1442
                return -1;
 
1443
 
 
1444
        /* unlike float8in, we ignore ERANGE from strtod */
 
1445
        val = strtod(tmp, &endptr);
 
1446
        if (*endptr != '\0')
 
1447
        {
 
1448
                /* shouldn't happen ... */
 
1449
                free(tmp);
 
1450
                errno = PGTYPES_NUM_BAD_NUMERIC;
 
1451
                return -1;
 
1452
        }
 
1453
        *dp = val;
 
1454
        free(tmp);
 
1455
        return 0;
 
1456
}
 
1457
 
 
1458
int
 
1459
PGTYPESnumeric_to_double(numeric *nv, double *dp)
 
1460
{
 
1461
        double          tmp;
 
1462
        int                     i;
 
1463
 
 
1464
        if ((i = numericvar_to_double_no_overflow(nv, &tmp)) != 0)
 
1465
                return -1;
 
1466
        *dp = tmp;
 
1467
        return 0;
 
1468
}
 
1469
 
 
1470
int
 
1471
PGTYPESnumeric_to_int(numeric *nv, int *ip)
 
1472
{
 
1473
        long            l;
 
1474
        int                     i;
 
1475
 
 
1476
        if ((i = PGTYPESnumeric_to_long(nv, &l)) != 0)
 
1477
                return i;
 
1478
 
 
1479
        if (l < -INT_MAX || l > INT_MAX)
 
1480
        {
 
1481
                errno = PGTYPES_NUM_OVERFLOW;
 
1482
                return -1;
 
1483
        }
 
1484
 
 
1485
        *ip = (int) l;
 
1486
        return 0;
 
1487
}
 
1488
 
 
1489
int
 
1490
PGTYPESnumeric_to_long(numeric *nv, long *lp)
 
1491
{
 
1492
        int                     i;
 
1493
        long            l = 0;
 
1494
 
 
1495
        for (i = 1; i < nv->weight + 2; i++)
 
1496
        {
 
1497
                l *= 10;
 
1498
                l += nv->buf[i];
 
1499
        }
 
1500
        if (nv->buf[i] >= 5)
 
1501
        {
 
1502
                /* round up */
 
1503
                l++;
 
1504
        }
 
1505
        if (l > LONG_MAX || l < 0)
 
1506
        {
 
1507
                errno = PGTYPES_NUM_OVERFLOW;
 
1508
                return -1;
 
1509
        }
 
1510
 
 
1511
        if (nv->sign == NUMERIC_NEG)
 
1512
                l *= -1;
 
1513
        *lp = l;
 
1514
        return 0;
 
1515
}
 
1516
 
 
1517
int
 
1518
PGTYPESnumeric_to_decimal(numeric *src, decimal *dst)
 
1519
{
 
1520
        int                     i;
 
1521
 
 
1522
        if (src->ndigits > DECSIZE)
 
1523
        {
 
1524
                errno = PGTYPES_NUM_OVERFLOW;
 
1525
                return -1;
 
1526
        }
 
1527
 
 
1528
        dst->weight = src->weight;
 
1529
        dst->rscale = src->rscale;
 
1530
        dst->dscale = src->dscale;
 
1531
        dst->sign = src->sign;
 
1532
        dst->ndigits = src->ndigits;
 
1533
 
 
1534
        for (i = 0; i < src->ndigits; i++)
 
1535
                dst->digits[i] = src->digits[i];
 
1536
 
 
1537
        return 0;
 
1538
}
 
1539
 
 
1540
int
 
1541
PGTYPESnumeric_from_decimal(decimal *src, numeric *dst)
 
1542
{
 
1543
        int                     i;
 
1544
 
 
1545
        zero_var(dst);
 
1546
 
 
1547
        dst->weight = src->weight;
 
1548
        dst->rscale = src->rscale;
 
1549
        dst->dscale = src->dscale;
 
1550
        dst->sign = src->sign;
 
1551
 
 
1552
        if (alloc_var(dst, src->ndigits) != 0)
 
1553
                return -1;
 
1554
 
 
1555
        for (i = 0; i < src->ndigits; i++)
 
1556
                dst->digits[i] = src->digits[i];
 
1557
 
 
1558
        return 0;
 
1559
}