1 /*
2 FUNCTION
3 <<ecvt>>,<<ecvtf>>,<<fcvt>>,<<fcvtf>>---double or float to string
4 
5 INDEX
6 	ecvt
7 INDEX
8 	fcvt
9 
10 ANSI_SYNOPSIS
11 	#include <stdlib.h>
12 
13 	char *ecvt(double <[val]>, int <[chars]>, int *<[decpt]>, int *<[sgn]>);
14 	char *ecvtf(float <[val]>, int <[chars]>, int *<[decpt]>, int *<[sgn]>);
15 
16 	char *fcvt(double <[val]>, int <[decimals]>,
17                    int *<[decpt]>, int *<[sgn]>);
18 	char *fcvtf(float <[val]>, int <[decimals]>,
19                     int *<[decpt]>, int *<[sgn]>);
20 
21 TRAD_SYNOPSIS
22 	#include <stdlib.h>
23 
24 	char *ecvt(<[val]>, <[chars]>, <[decpt]>, <[sgn]>);
25 	double <[val]>;
26 	int <[chars]>;
27 	int *<[decpt]>;
28 	int *<[sgn]>;
29 	char *ecvtf(<[val]>, <[chars]>, <[decpt]>, <[sgn]>);
30 	float <[val]>;
31 	int <[chars]>;
32 	int *<[decpt]>;
33 	int *<[sgn]>;
34 
35 	char *fcvt(<[val]>, <[decimals]>, <[decpt]>, <[sgn]>);
36 	double <[val]>;
37 	int <[decimals]>;
38 	int *<[decpt]>;
39 	int *<[sgn]>;
40 	char *fcvtf(<[val]>, <[decimals]>, <[decpt]>, <[sgn]>);
41 	float <[val]>;
42 	int <[decimals]>;
43 	int *<[decpt]>;
44 	int *<[sgn]>;
45 
46 DESCRIPTION
47 <<ecvt>> and <<fcvt>> produce (null-terminated) strings of digits
48 representating the <<double>> number <[val]>.
49 <<ecvtf>> and <<fcvtf>> produce the corresponding character
50 representations of <<float>> numbers.
51 
52 (The <<stdlib>> functions <<ecvtbuf>> and <<fcvtbuf>> are reentrant
53 versions of <<ecvt>> and <<fcvt>>.)
54 
55 The only difference between <<ecvt>> and <<fcvt>> is the
56 interpretation of the second argument (<[chars]> or <[decimals]>).
57 For <<ecvt>>, the second argument <[chars]> specifies the total number
58 of characters to write (which is also the number of significant digits
59 in the formatted string, since these two functions write only digits).
60 For <<fcvt>>, the second argument <[decimals]> specifies the number of
61 characters to write after the decimal point; all digits for the integer
62 part of <[val]> are always included.
63 
64 Since <<ecvt>> and <<fcvt>> write only digits in the output string,
65 they record the location of the decimal point in <<*<[decpt]>>>, and
66 the sign of the number in <<*<[sgn]>>>.  After formatting a number,
67 <<*<[decpt]>>> contains the number of digits to the left of the
68 decimal point.  <<*<[sgn]>>> contains <<0>> if the number is positive,
69 and <<1>> if it is negative.
70 
71 RETURNS
72 All four functions return a pointer to the new string containing a
73 character representation of <[val]>.
74 
75 PORTABILITY
76 None of these functions are ANSI C.
77 
78 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
79 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
80 
81 NEWPAGE
82 FUNCTION
83 <<gvcvt>>, <<gcvtf>>---format double or float as string
84 
85 INDEX
86 	gcvt
87 INDEX
88 	gcvtf
89 
90 ANSI_SYNOPSIS
91 	#include <stdlib.h>
92 
93 	char *gcvt(double <[val]>, int <[precision]>, char *<[buf]>);
94 	char *gcvtf(float <[val]>, int <[precision]>, char *<[buf]>);
95 
96 TRAD_SYNOPSIS
97 	#include <stdlib.h>
98 
99 	char *gcvt(<[val]>, <[precision]>, <[buf]>);
100 	double <[val]>;
101 	int <[precision]>;
102 	char *<[buf]>;
103 	char *gcvtf(<[val]>, <[precision]>, <[buf]>);
104 	float <[val]>;
105 	int <[precision]>;
106 	char *<[buf]>;
107 
108 DESCRIPTION
109 <<gcvt>> writes a fully formatted number as a null-terminated
110 string in the buffer <<*<[buf]>>>.  <<gdvtf>> produces corresponding
111 character representations of <<float>> numbers.
112 
113 <<gcvt>> uses the same rules as the <<printf>> format
114 `<<%.<[precision]>g>>'---only negative values are signed (with
115 `<<->>'), and either exponential or ordinary decimal-fraction format
116 is chosen depending on the number of significant digits (specified by
117 <[precision]>).
118 
119 RETURNS
120 The result is a pointer to the formatted representation of <[val]>
121 (the same as the argument <[buf]>).
122 
123 PORTABILITY
124 Neither function is ANSI C.
125 
126 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
127 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
128 */
129 
130 #include <_ansi.h>
131 #include <reent.h>
132 #include <stdio.h>
133 #include <stdlib.h>
134 #include "local.h"
135 
136 char *
137 _DEFUN (fcvt, (d, ndigit, decpt, sign),
138 	double d _AND
139 	int ndigit _AND
140 	int *decpt _AND
141 	int *sign)
142 {
143   return fcvtbuf (d, ndigit, decpt, sign, NULL);
144 }
145 
146 char *
147 _DEFUN (fcvtf, (d, ndigit, decpt, sign),
148 	float d _AND
149 	int ndigit _AND
150 	int *decpt _AND
151 	int *sign)
152 {
153   return fcvt ((float) d, ndigit, decpt, sign);
154 }
155 
156 
157 char *
158 _DEFUN (gcvtf, (d, ndigit, buf),
159 	float d _AND
160 	int ndigit _AND
161 	char *buf)
162 {
163   double asd = d;
164   return gcvt (asd, ndigit, buf);
165 }
166 
167 
168 char *
169 _DEFUN (ecvt, (d, ndigit, decpt, sign),
170 	double d _AND
171 	int ndigit _AND
172 	int *decpt _AND
173 	int *sign)
174 {
175   return ecvtbuf (d, ndigit, decpt, sign, NULL);
176 }
177 
178 char *
179 _DEFUN (ecvtf, (d, ndigit, decpt, sign),
180 	float d _AND
181 	int ndigit _AND
182 	int *decpt _AND
183 	int *sign)
184 {
185   return ecvt ((double) d, ndigit, decpt, sign);
186 }
187 
188 
189 char *
190 _DEFUN (gcvt, (d, ndigit, buf),
191 	double d _AND
192 	int ndigit _AND
193 	char *buf)
194 {
195   char *tbuf = buf;
196   if (d < 0) {
197     *buf = '-';
198     buf++;
199     ndigit--;
200   }
201   return (_gcvt (_REENT, d, ndigit, buf, 'g', 0) ? tbuf : 0);
202 }
203