1987-03-31 10:45:53 +00:00
|
|
|
/* $Header$ */
|
1987-01-27 15:57:55 +00:00
|
|
|
#ifndef NOFLOAT
|
1988-08-10 09:14:43 +00:00
|
|
|
|
1987-01-27 15:57:55 +00:00
|
|
|
static char *cvt();
|
|
|
|
#define NDIGITS 128
|
|
|
|
|
|
|
|
char *
|
|
|
|
ecvt(value, ndigit, decpt, sign)
|
|
|
|
double value;
|
|
|
|
int ndigit, *decpt, *sign;
|
|
|
|
{
|
|
|
|
return cvt(value, ndigit, decpt, sign, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
fcvt(value, ndigit, decpt, sign)
|
|
|
|
double value;
|
|
|
|
int ndigit, *decpt, *sign;
|
|
|
|
{
|
|
|
|
return cvt(value, ndigit, decpt, sign, 0);
|
|
|
|
}
|
|
|
|
|
1988-08-10 09:14:43 +00:00
|
|
|
static struct powers_of_10 {
|
|
|
|
double pval;
|
|
|
|
double rpval;
|
|
|
|
int exp;
|
|
|
|
} p10[] = {
|
|
|
|
1.0e32, 1.0e-32, 32,
|
|
|
|
1.0e16, 1.0e-16, 16,
|
|
|
|
1.0e8, 1.0e-8, 8,
|
|
|
|
1.0e4, 1.0e-4, 4,
|
|
|
|
1.0e2, 1.0e-2, 2,
|
|
|
|
1.0e1, 1.0e-1, 1,
|
|
|
|
1.0e0, 1.0e0, 0
|
|
|
|
};
|
|
|
|
|
1987-01-27 15:57:55 +00:00
|
|
|
static char *
|
|
|
|
cvt(value, ndigit, decpt, sign, ecvtflag)
|
|
|
|
double value;
|
|
|
|
int ndigit, *decpt, *sign;
|
|
|
|
{
|
1987-10-22 13:58:48 +00:00
|
|
|
static char buf[NDIGITS+1];
|
1988-08-10 09:14:43 +00:00
|
|
|
register char *p = buf;
|
|
|
|
register char *pe;
|
1987-01-27 15:57:55 +00:00
|
|
|
|
|
|
|
if (ndigit < 0) ndigit = 0;
|
1988-08-10 09:14:43 +00:00
|
|
|
if (ndigit > NDIGITS) ndigit = NDIGITS;
|
|
|
|
pe = &buf[ndigit];
|
1988-08-10 10:41:15 +00:00
|
|
|
buf[0] = '\0';
|
1987-01-27 15:57:55 +00:00
|
|
|
|
|
|
|
*sign = 0;
|
|
|
|
if (value < 0) {
|
|
|
|
*sign = 1;
|
|
|
|
value = -value;
|
|
|
|
}
|
|
|
|
|
1988-08-10 09:14:43 +00:00
|
|
|
*decpt = 0;
|
|
|
|
if (value != 0.0) {
|
|
|
|
register struct powers_of_10 *pp = &p10[0];
|
|
|
|
|
|
|
|
if (value >= 10.0) do {
|
|
|
|
while (value >= pp->pval) {
|
|
|
|
value *= pp->rpval;
|
|
|
|
*decpt += pp->exp;
|
1987-02-05 20:47:30 +00:00
|
|
|
}
|
1988-08-10 09:14:43 +00:00
|
|
|
} while ((++pp)->exp > 0);
|
|
|
|
|
|
|
|
pp = &p10[0];
|
|
|
|
if (value < 1.0) do {
|
|
|
|
while (value * pp->pval < 10.0) {
|
|
|
|
value *= pp->pval;
|
|
|
|
*decpt -= pp->exp;
|
1987-02-06 14:50:57 +00:00
|
|
|
}
|
1988-08-10 09:14:43 +00:00
|
|
|
} while ((++pp)->exp > 0);
|
|
|
|
|
|
|
|
(*decpt)++; /* because now value in [1.0, 10.0) */
|
1987-01-27 15:57:55 +00:00
|
|
|
}
|
|
|
|
if (! ecvtflag) {
|
|
|
|
/* for fcvt() we need ndigit digits behind the dot */
|
1988-08-10 09:14:43 +00:00
|
|
|
pe += *decpt;
|
1987-01-27 15:57:55 +00:00
|
|
|
if (pe > &buf[NDIGITS]) pe = &buf[NDIGITS];
|
|
|
|
}
|
1988-08-10 09:14:43 +00:00
|
|
|
while (p <= pe) {
|
|
|
|
*p++ = (int)value + '0';
|
|
|
|
value = 10.0 * (value - (int)value);
|
1987-01-27 15:57:55 +00:00
|
|
|
}
|
1988-08-10 10:41:15 +00:00
|
|
|
if (pe >= buf) {
|
|
|
|
p = pe;
|
|
|
|
*p += 5; /* round of at the end */
|
|
|
|
while (*p > '9') {
|
|
|
|
*p = '0';
|
|
|
|
if (p > buf) ++*--p;
|
|
|
|
else {
|
|
|
|
*p = '1';
|
|
|
|
++*decpt;
|
|
|
|
if (! ecvtflag) {
|
|
|
|
/* maybe add another digit at the end,
|
|
|
|
because the point was shifted right
|
|
|
|
*/
|
|
|
|
if (pe > buf) *pe = '0';
|
|
|
|
pe++;
|
|
|
|
}
|
1987-01-27 15:57:55 +00:00
|
|
|
}
|
|
|
|
}
|
1988-08-10 10:41:15 +00:00
|
|
|
*pe = '\0';
|
1987-01-27 15:57:55 +00:00
|
|
|
}
|
1987-02-06 13:28:01 +00:00
|
|
|
return buf;
|
1987-01-27 15:57:55 +00:00
|
|
|
}
|
|
|
|
#endif
|