Commit | Line | Data |
---|---|---|
2ebdebca KM |
1 | #if defined(LIBC_SCCS) && !defined(lint) |
2 | static char sccsid[] = "@(#)ecvt.c 5.1 (Berkeley) %G%"; | |
3 | #endif LIBC_SCCS and not lint | |
4 | ||
dfe9e643 BJ |
5 | /* |
6 | * ecvt converts to decimal | |
7 | * the number of digits is specified by ndigit | |
8 | * decpt is set to the position of the decimal point | |
9 | * sign is set to 0 for positive, 1 for negative | |
10 | */ | |
11 | ||
12 | char *cvt(); | |
13 | ||
14 | #define NDIG 80 | |
15 | char* | |
16 | ecvt(arg, ndigits, decpt, sign) | |
17 | double arg; | |
18 | int ndigits, *decpt, *sign; | |
19 | { | |
20 | return(cvt(arg, ndigits, decpt, sign, 1)); | |
21 | } | |
22 | ||
23 | char* | |
24 | fcvt(arg, ndigits, decpt, sign) | |
25 | double arg; | |
26 | int ndigits, *decpt, *sign; | |
27 | { | |
28 | return(cvt(arg, ndigits, decpt, sign, 0)); | |
29 | } | |
30 | ||
31 | static char* | |
32 | cvt(arg, ndigits, decpt, sign, eflag) | |
33 | double arg; | |
34 | int ndigits, *decpt, *sign; | |
35 | { | |
36 | register int r2; | |
37 | double fi, fj; | |
38 | register char *p, *p1; | |
39 | static char buf[NDIG]; | |
40 | double modf(); | |
41 | ||
42 | if (ndigits<0) | |
43 | ndigits = 0; | |
44 | if (ndigits>=NDIG-1) | |
45 | ndigits = NDIG-2; | |
46 | r2 = 0; | |
47 | *sign = 0; | |
48 | p = &buf[0]; | |
49 | if (arg<0) { | |
50 | *sign = 1; | |
51 | arg = -arg; | |
52 | } | |
53 | arg = modf(arg, &fi); | |
54 | p1 = &buf[NDIG]; | |
55 | /* | |
56 | * Do integer part | |
57 | */ | |
58 | if (fi != 0) { | |
59 | p1 = &buf[NDIG]; | |
60 | while (fi != 0) { | |
61 | fj = modf(fi/10, &fi); | |
62 | *--p1 = (int)((fj+.03)*10) + '0'; | |
63 | r2++; | |
64 | } | |
65 | while (p1 < &buf[NDIG]) | |
66 | *p++ = *p1++; | |
67 | } else if (arg > 0) { | |
68 | while ((fj = arg*10) < 1) { | |
69 | arg = fj; | |
70 | r2--; | |
71 | } | |
72 | } | |
73 | p1 = &buf[ndigits]; | |
74 | if (eflag==0) | |
75 | p1 += r2; | |
76 | *decpt = r2; | |
77 | if (p1 < &buf[0]) { | |
78 | buf[0] = '\0'; | |
79 | return(buf); | |
80 | } | |
81 | while (p<=p1 && p<&buf[NDIG]) { | |
82 | arg *= 10; | |
83 | arg = modf(arg, &fj); | |
84 | *p++ = (int)fj + '0'; | |
85 | } | |
86 | if (p1 >= &buf[NDIG]) { | |
87 | buf[NDIG-1] = '\0'; | |
88 | return(buf); | |
89 | } | |
90 | p = p1; | |
91 | *p1 += 5; | |
92 | while (*p1 > '9') { | |
93 | *p1 = '0'; | |
94 | if (p1>buf) | |
95 | ++*--p1; | |
96 | else { | |
97 | *p1 = '1'; | |
98 | (*decpt)++; | |
99 | if (eflag==0) { | |
100 | if (p>buf) | |
101 | *p = '0'; | |
102 | p++; | |
103 | } | |
104 | } | |
105 | } | |
106 | *p = '\0'; | |
107 | return(buf); | |
108 | } |