1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
/* @(#)cvt.c 1.1 98/07/19 Copyright 1998 J. Schilling */
/*
* Compatibility routines for 4.4BSD based C-libraries
* Neither __dtoa() nor [efg]cvt() are MT safe.
*
* Copyright (c) 1998 J. Schilling
*/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdxlib.h>
#include <utypes.h>
#include <standard.h>
extern char *__dtoa __PR((double value, int mode, int ndigit, int *decpt, int *sign, char **ep));
EXPORT char *ecvt __PR((double value, int ndigit, int *decpt, int *sign));
EXPORT char *fcvt __PR((double value, int ndigit, int *decpt, int *sign));
EXPORT char *gcvt __PR((double value, int ndigit, char *buf));
char *
ecvt(value, ndigit, decpt, sign)
double value;
int ndigit;
int *decpt;
int *sign;
{
static Uint bufsize;
static char *buf;
char *bufend;
char *ep;
char *bp = __dtoa(value, 2, ndigit, decpt, sign, &ep);
if (value == 0.0) {
/*
* Handle __dtoa()'s deviation from ecvt():
* 0.0 is converted to "0" instead of 'ndigit' zeroes.
* The string "0" is not allocated, so
* we need to allocate buffer to hold 'ndigit' zeroes.
*/
if (bufsize < ndigit + 1) {
if (buf != (char *)0)
free(buf);
bufsize = ndigit + 1;
buf = malloc(bufsize);
}
ep = bp = buf;
}
/*
* Fill up trailing zeroes suppressed by __dtoa()
* From an internal __dtoa() comment:
* Sufficient space is allocated to the return value
* to hold the suppressed trailing zeros.
*/
for (bufend = &bp[ndigit]; ep < bufend; )
*ep++ = '0';
*ep = '\0';
return (bp);
}
char *
fcvt(value, ndigit, decpt, sign)
double value;
int ndigit;
int *decpt;
int *sign;
{
static Uint bufsize;
static char *buf;
char *bufend;
char *ep;
char *bp = __dtoa(value, 3, ndigit, decpt, sign, &ep);
if (value == 0.0) {
/*
* Handle __dtoa()'s deviation from fcvt():
* 0.0 is converted to "0" instead of 'ndigit' zeroes.
* The string "0" is not allocated, so
* we need to allocate buffer to hold 'ndigit' zeroes.
*/
if (bufsize < ndigit + 1) {
if (buf != (char *)0)
free(buf);
bufsize = ndigit + 1;
buf = malloc(bufsize);
}
ep = bp = buf;
*decpt = 0;
}
/*
* Fill up trailing zeroes suppressed by __dtoa()
* From an internal __dtoa() comment:
* Sufficient space is allocated to the return value
* to hold the suppressed trailing zeros.
*/
for (bufend = &bp[*decpt + ndigit]; ep < bufend; )
*ep++ = '0';
*ep = '\0';
return (bp);
}
char *
gcvt(number, ndigit, buf)
double number;
int ndigit;
char *buf;
{
int sign;
int decpt;
register char *b;
register char *rs;
register int i;
b = ecvt(number, ndigit, &decpt, &sign);
rs = buf;
if (sign)
*rs++ = '-';
for (i = ndigit-1; i > 0 && b[i] == '0'; i--)
ndigit--;
#ifdef V7_FLOATSTYLE
if ((decpt >= 0 && decpt-ndigit > 4) ||
#else
if ((decpt >= 0 && decpt-ndigit > 0) ||
#endif
(decpt < 0 && decpt < -3)) { /* e-format */
decpt--;
*rs++ = *b++;
*rs++ = '.';
for (i = 1; i < ndigit; i++)
*rs++ = *b++;
*rs++ = 'e';
if (decpt < 0) {
decpt = -decpt;
*rs++ = '-';
} else {
*rs++ = '+';
}
if (decpt >= 100) {
*rs++ = decpt / 100 + '0';
decpt %= 100;
}
*rs++ = decpt / 10 + '0';
*rs++ = decpt % 10 + '0';
} else { /* f-format */
if (decpt <= 0) {
if (*b != '0') {
#ifndef V7_FLOATSTYLE
*rs++ = '0';
#endif
*rs++ = '.';
}
while (decpt < 0) {
decpt++;
*rs++ = '0';
}
}
for (i = 1; i <= ndigit; i++) {
*rs++ = *b++;
if (i == decpt)
*rs++ = '.';
}
if (ndigit < decpt) {
while (ndigit++ < decpt)
*rs++ = '0';
*rs++ = '.';
}
}
if (rs[-1] == '.')
rs--;
*rs = '\0';
return (buf);
}
|