22 #define ACCSIZE (DECSIZE + 1)
71 for (len = count; --len;) {
72 cp[len - 1] = cp[len];
97 if ((buffer[j] = i % 100) || (j != 0)) {
106 dp->dec_dgts[i++] = buffer[--j];
116 unsigned char *digits = dp->dec_dgts;
117 int expon = dp->dec_exp;
118 int valid = dp->dec_ndgts;
120 while (expon-- > 0) {
126 *ip = (dp->dec_pos) ? i : -i;
137 str = (
unsigned char *)ecvt (dbl, ndigits, &decpt, &sign);
139 dp->dec_pos = sign ? 0 : 1;
140 dp->dec_exp = (decpt + (decpt > 0 ? 1 : 0)) / 2;
145 *dgt++ = *str++ -
'0';
149 while (ndigits-- > 0) {
150 *dgt++ = (*str++ -
'0') * 10;
152 dgt[-1] += *str++ -
'0';
156 while (--dgt >= dp->dec_dgts && *dgt == 0) ;
158 dp->dec_ndgts = 1 + dgt - dp->dec_dgts;
166 unsigned char *digits = dp->dec_dgts;
169 if (valid > dp->dec_ndgts) {
170 valid = dp->dec_ndgts;
175 while (valid-- > 0) {
176 dbl = (dbl + digits[valid]) / 100.0;
179 if (dp->dec_pos == 0) {
183 if (dp->dec_exp > 0) {
189 }
else if (dp->dec_exp < 0) {
203 decefcvt (dec_t *np,
int dg,
int *pt,
int *sg,
int fl)
205 static char *ds =
NULL;
210 ds = calloc (1, 160);
213 if (np->dec_pos == -1) {
216 *sg = np->dec_pos ^ 1;
217 *pt = np->dec_exp * 2;
219 if (nd && np->dec_dgts[0] < 10) {
230 if (nd && np->dec_dgts[0] < 10) {
233 rd.dec_pos = np->dec_pos;
235 rd.dec_exp = np->dec_exp - (k + i) / 2;
245 if (
decadd (np, &rd, &rd)) {
249 *pt = rd.dec_exp * 2;
250 if (nd && rd.dec_dgts[0] < 10) {
258 while (j < dg && j < 151) {
259 if (i / 2 < rd.dec_ndgts) {
260 k = rd.dec_dgts[i / 2];
293 while (s->
dec_dgts[j] == 0 && j < s->dec_ndgts) {
301 while (j < s->dec_ndgts) {
304 while (i < s->dec_ndgts) {
356 memcpy (dst, src,
sizeof (dec_t));
362 if ((
unsigned)i == (
unsigned)VAL_DECPOSNULL (
int)) {
376 if (dp->dec_pos == DECPOSNULL) {
377 *ip = VAL_DECPOSNULL (
int);
388 if (i == VAL_DECPOSNULL (
long)) {
401 if (dp->dec_pos == DECPOSNULL) {
402 *ip = VAL_DECPOSNULL (
long);
442 unsigned char buffer[DECSIZE];
443 unsigned char header;
446 if (dp->dec_pos == -1) {
451 header = 0xC0 + dp->dec_exp;
454 if ((count = dp->dec_ndgts)) {
455 memcpy (buffer, dp->dec_dgts, count);
457 if (len < count && buffer[len] >= 50) {
461 if (dp->dec_pos == 0) {
463 comp100 (buffer, len < count ? len : count);
471 *++cp = (count-- > 0) ? *bp++ : 0;
480 unsigned char buffer[DECSIZE + 1];
481 unsigned char *digits;
490 if (--len > DECSIZE) {
494 memcpy (buffer, cp + 1, len);
498 dp->dec_exp = *cp - 0xC0;
502 dp->dec_exp = 0xFF - (*cp) - 0xC0;
507 while (len > 0 && *--cp == 0) {
512 digits = dp->dec_dgts;
526 if (x->dec_pos == -1 || y->dec_pos == -1) {
547 if (x->dec_pos == -1 || y->dec_pos == -1) {
554 memset (z, 0,
sizeof (
struct decacc));
570 memcpy (z, x,
sizeof (dec_t));
571 a = x->dec_exp - y->dec_exp;
574 memcpy (r, x,
sizeof (dec_t));
577 i = y->dec_ndgts + a;
584 if ((j = i - a) < 0) {
591 if (x->dec_pos == y->dec_pos) {
601 }
else if (c < 100) {
610 memcpy (r, z,
sizeof (dec_t));
620 if (x->dec_pos == -1 || y->dec_pos == -1) {
627 memset (p, 0,
sizeof (
struct decacc));
628 for (i = x->dec_ndgts; i >= 0; i--) {
630 for (j = y->dec_ndgts - 1; j >= 0; j--) {
632 k += p->
dec_dgts[i + j] + x->dec_dgts[i] * y->dec_dgts[j];
641 p->
dec_pos = x->dec_pos ^ y->dec_pos ^ 1;
642 p->
dec_exp = x->dec_exp + y->dec_exp - 1;
643 p->
dec_ndgts = x->dec_ndgts + y->dec_ndgts;
649 memcpy (r, p,
sizeof (dec_t));
656 int j, c, n, i, m, s, t, u = 0;
657 struct decacc *q, qv, *a, av;
659 if (x->dec_pos == -1 || y->dec_pos == -1) {
666 if (y->dec_ndgts == 0) {
675 memset (q, 0,
sizeof (
struct decacc));
676 q->
dec_exp = x->dec_exp - y->dec_exp + 1;
677 q->
dec_pos = x->dec_pos ^ y->dec_pos ^ 1;
679 memcpy (a, x,
sizeof (dec_t));
683 for (n = 0; n <
ACCSIZE; n += 1) {
684 if (n == 0 || a->
dec_dgts[n - 1] == 0) {
689 if (n != 1 || u != 0) {
695 if (i < ACCSIZE - 1) {
699 s = y->dec_dgts[0] * 100;
700 if (y->dec_ndgts > 1) {
706 u = ((long)t) * 100 / s;
714 if (i + j > ACCSIZE) {
716 c = -(y->dec_dgts[j] * u / 100);
722 c -= y->dec_dgts[j] * u;
725 a->
dec_dgts[n + j] = (c + 10000) % 100;
726 c = (c + 1) / 100 - 1;
738 if (i + j > ACCSIZE) {
765 memcpy (r, q,
sizeof (dec_t));
774 if (x->dec_pos == -1 || y->dec_pos == -1) {
777 s = x->dec_pos - y->dec_pos;
779 s = x->dec_exp - y->dec_exp;
781 for (i = 0; i < DECSIZE; i += 1) {
785 if (i < y->dec_ndgts) {
806 int i, j, m, t, pt, sg;
809 memset (cp,
' ', ln);
810 if (np->dec_pos == DECPOSNULL) {
816 if (dg > 0 && np->dec_dgts[0] < 10) {
819 if ((dg > 1 && np->dec_dgts[i - 1] % 10) == 0) {
826 if (dg > ln - i - 1) {
829 v =
dececvt (np, dg, &pt, &sg);
830 if (pt < 0 && dg + sg - pt + 1 >= ln) {
837 v =
decfcvt (np, dg, &pt, &sg);
846 v =
decfcvt (np, dg - i, &pt, &sg);
853 if (i < ln && pt <= 0) {
857 while (m > 0 && v[j] !=
'\0' && i < ln) {
864 while (m < 0 && i < ln) {
868 while (v[j] !=
'\0' && i < ln) {
877 memset (cp,
' ', ln);
886 cp[--m] = i % 10 +
'0';
903 if (np->dec_exp < -1) {
904 memset (cp,
' ', ln);
907 memset (cp,
'*', ln);
915 v =
dececvt (np, dg, &pt, &sg);
927 while (m && (cp[--m] ==
'0' || cp[m] ==
'\0')) {
928 for (i = m; i < ln - 1; i++) {
933 if (m && cp[m] ==
'.') {
934 for (i = m; i < ln - 1; i++) {
948 int ps, i, j, xs, xv, ms;
952 memset (np, 0,
sizeof nv);
953 ps = i = j = xs = xv = ms = 0;
954 rp->dec_pos = np->
dec_pos = DECPOSNULL;
956 while (i < ln && cp[i] ==
' ') {
973 if (c >=
'0' && c <=
'9') {
978 if ((j || c) && j <
ACCSIZE * 2) {
986 }
else if (c ==
'.') {
996 if (i < ln && (c ==
'e' || c ==
'E')) {
1001 }
else if (c ==
'-') {
1006 if (c < '0' || c >
'9') {
1009 if ((xv = xv * 10 + c -
'0') >= 1000) {
1033 if ((xv & 1) == 0) {
1038 for (i = 0; i <
ACCSIZE; i++) {
1046 memcpy (rp, np,
sizeof (dec_t));
1053 return decefcvt (np, dg, pt, sg, 0);
1059 return decefcvt (np, dg, pt, sg, 1);
int deccvlong(long i, dec_t *dp)
char * dececvt(dec_t *np, int dg, int *pt, int *sg)
int dectoint(dec_t *dp, int *ip)
static int dectoreal(dec_t *dp, double *dblp, int valid)
static char * decefcvt(dec_t *np, int dg, int *pt, int *sg, int fl)
int decadd(dec_t *x, dec_t *y, dec_t *r)
int dectolong(dec_t *dp, long *ip)
int decmul(dec_t *x, dec_t *y, dec_t *r)
int lddecimal(unsigned char *cp, int len, dec_t *dp)
static void dectofix(dec_t *dp, long *ip)
char * decfcvt(dec_t *np, int dg, int *pt, int *sg)
static int round100(unsigned char *cp, int len)
int decsub(dec_t *x, dec_t *y, dec_t *r)
int deccmp(dec_t *x, dec_t *y)
int deccvflt(float flt, dec_t *dp)
int deccvint(int i, dec_t *dp)
int dectoflt(dec_t *dp, float *fltp)
static int deccvreal(double dbl, dec_t *dp, int ndigits)
int dectoasc(dec_t *np, char *cp, int ln, int dg)
EC ARGUMENT EC EC BOUND EC BOUND EC BOUND EC BOUND TABLE EC DATA EC DATA EC DATA PTR NULL
int deccvasc(char *cp, int ln, dec_t *rp)
void stdecimal(dec_t *dp, unsigned char *cp, int len)
int deccvdbl(double dbl, dec_t *dp)
void deccopy(dec_t *src, dec_t *dst)
int decdiv(dec_t *x, dec_t *y, dec_t *r)
int dectodbl(dec_t *dp, double *dblp)
static void comp100(unsigned char *cp, int count)
static int deccvfix(long i, dec_t *dp)
static int dec_round(struct decacc *s, int c)