Welcome to mirror list, hosted at ThFree Co, Russian Federation.

efgcvt.c « stdlib « libc « newlib - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 556d224142d61d06f85224c6f913dce868226a9c (plain)
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
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
FUNCTION
<<ecvt>>,<<ecvtf>>,<<fcvt>>,<<fcvtf>>---double or float to string

INDEX
	ecvt
INDEX
	fcvt

ANSI_SYNOPSIS
	#include <stdlib.h>

	char *ecvt(double <[val]>, int <[chars]>, int *<[decpt]>, int *<[sgn]>);
	char *ecvtf(float <[val]>, int <[chars]>, int *<[decpt]>, int *<[sgn]>);

	char *fcvt(double <[val]>, int <[decimals]>, 
                   int *<[decpt]>, int *<[sgn]>);
	char *fcvtf(float <[val]>, int <[decimals]>, 
                    int *<[decpt]>, int *<[sgn]>);

TRAD_SYNOPSIS
	#include <stdlib.h>

	char *ecvt(<[val]>, <[chars]>, <[decpt]>, <[sgn]>);
	double <[val]>;
	int <[chars]>;
	int *<[decpt]>;
	int *<[sgn]>;
	char *ecvtf(<[val]>, <[chars]>, <[decpt]>, <[sgn]>);
	float <[val]>;
	int <[chars]>;
	int *<[decpt]>;
	int *<[sgn]>;

	char *fcvt(<[val]>, <[decimals]>, <[decpt]>, <[sgn]>);
	double <[val]>;
	int <[decimals]>;
	int *<[decpt]>;
	int *<[sgn]>;
	char *fcvtf(<[val]>, <[decimals]>, <[decpt]>, <[sgn]>);
	float <[val]>;
	int <[decimals]>;
	int *<[decpt]>;
	int *<[sgn]>;

DESCRIPTION
<<ecvt>> and <<fcvt>> produce (null-terminated) strings of digits
representating the <<double>> number <[val]>.
<<ecvtf>> and <<fcvtf>> produce the corresponding character
representations of <<float>> numbers.

(The <<stdlib>> functions <<ecvtbuf>> and <<fcvtbuf>> are reentrant
versions of <<ecvt>> and <<fcvt>>.)

The only difference between <<ecvt>> and <<fcvt>> is the
interpretation of the second argument (<[chars]> or <[decimals]>).
For <<ecvt>>, the second argument <[chars]> specifies the total number
of characters to write (which is also the number of significant digits
in the formatted string, since these two functions write only digits).
For <<fcvt>>, the second argument <[decimals]> specifies the number of
characters to write after the decimal point; all digits for the integer
part of <[val]> are always included.

Since <<ecvt>> and <<fcvt>> write only digits in the output string,
they record the location of the decimal point in <<*<[decpt]>>>, and
the sign of the number in <<*<[sgn]>>>.  After formatting a number,
<<*<[decpt]>>> contains the number of digits to the left of the
decimal point.  <<*<[sgn]>>> contains <<0>> if the number is positive,
and <<1>> if it is negative.

RETURNS
All four functions return a pointer to the new string containing a
character representation of <[val]>.

PORTABILITY
None of these functions are ANSI C.

Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.

NEWPAGE
FUNCTION
<<gvcvt>>, <<gcvtf>>---format double or float as string

INDEX
	gcvt
INDEX
	gcvtf

ANSI_SYNOPSIS
	#include <stdlib.h>

	char *gcvt(double <[val]>, int <[precision]>, char *<[buf]>);
	char *gcvtf(float <[val]>, int <[precision]>, char *<[buf]>);

TRAD_SYNOPSIS
	#include <stdlib.h>

	char *gcvt(<[val]>, <[precision]>, <[buf]>);
	double <[val]>;
	int <[precision]>;
	char *<[buf]>;
	char *gcvtf(<[val]>, <[precision]>, <[buf]>);
	float <[val]>;
	int <[precision]>;
	char *<[buf]>;

DESCRIPTION
<<gcvt>> writes a fully formatted number as a null-terminated
string in the buffer <<*<[buf]>>>.  <<gdvtf>> produces corresponding
character representations of <<float>> numbers.

<<gcvt>> uses the same rules as the <<printf>> format
`<<%.<[precision]>g>>'---only negative values are signed (with
`<<->>'), and either exponential or ordinary decimal-fraction format
is chosen depending on the number of significant digits (specified by
<[precision]>).

RETURNS
The result is a pointer to the formatted representation of <[val]>
(the same as the argument <[buf]>).

PORTABILITY
Neither function is ANSI C.

Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
<<lseek>>, <<read>>, <<sbrk>>, <<write>>.
*/

#include <_ansi.h>
#include <reent.h>
#include <stdio.h>
#include <stdlib.h>
#include "local.h"

char *
_DEFUN (fcvt, (d, ndigit, decpt, sign),
	double d _AND
	int ndigit _AND
	int *decpt _AND
	int *sign)
{
  return fcvtbuf (d, ndigit, decpt, sign, NULL);
}

char *
_DEFUN (fcvtf, (d, ndigit, decpt, sign),
	float d _AND
	int ndigit _AND
	int *decpt _AND
	int *sign)
{
  return fcvt ((float) d, ndigit, decpt, sign);
}


char *
_DEFUN (gcvtf, (d, ndigit, buf),
	float d _AND
	int ndigit _AND
	char *buf)
{
  double asd = d;
  return gcvt (asd, ndigit, buf);
}


char *
_DEFUN (ecvt, (d, ndigit, decpt, sign),
	double d _AND
	int ndigit _AND
	int *decpt _AND
	int *sign)
{
  return ecvtbuf (d, ndigit, decpt, sign, NULL);
}

char *
_DEFUN (ecvtf, (d, ndigit, decpt, sign),
	float d _AND
	int ndigit _AND
	int *decpt _AND
	int *sign)
{
  return ecvt ((double) d, ndigit, decpt, sign);
}


char *
_DEFUN (gcvt, (d, ndigit, buf),
	double d _AND
	int ndigit _AND
	char *buf)
{
  char *tbuf = buf;
  if (d < 0) {
    *buf = '-';
    buf++;
    ndigit--;
  }
  return (_gcvt (_REENT, d, ndigit, buf, 'g', 0) ? tbuf : 0);
}