Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / efgcvt.c
blob3cdb9c420cadd9e37d6f1e5f34ee40bc7faf832b
1 /*
2 FUNCTION
3 <<ecvt>>, <<ecvtf>>, <<fcvt>>, <<fcvtf>>---double or float to string
5 INDEX
6 ecvt
7 INDEX
8 ecvtf
9 INDEX
10 fcvt
11 INDEX
12 fcvtf
14 SYNOPSIS
15 #include <stdlib.h>
17 char *ecvt(double <[val]>, int <[chars]>, int *<[decpt]>, int *<[sgn]>);
18 char *ecvtf(float <[val]>, int <[chars]>, int *<[decpt]>, int *<[sgn]>);
20 char *fcvt(double <[val]>, int <[decimals]>,
21 int *<[decpt]>, int *<[sgn]>);
22 char *fcvtf(float <[val]>, int <[decimals]>,
23 int *<[decpt]>, int *<[sgn]>);
25 DESCRIPTION
26 <<ecvt>> and <<fcvt>> produce (null-terminated) strings of digits
27 representating the <<double>> number <[val]>.
28 <<ecvtf>> and <<fcvtf>> produce the corresponding character
29 representations of <<float>> numbers.
31 (The <<stdlib>> functions <<ecvtbuf>> and <<fcvtbuf>> are reentrant
32 versions of <<ecvt>> and <<fcvt>>.)
34 The only difference between <<ecvt>> and <<fcvt>> is the
35 interpretation of the second argument (<[chars]> or <[decimals]>).
36 For <<ecvt>>, the second argument <[chars]> specifies the total number
37 of characters to write (which is also the number of significant digits
38 in the formatted string, since these two functions write only digits).
39 For <<fcvt>>, the second argument <[decimals]> specifies the number of
40 characters to write after the decimal point; all digits for the integer
41 part of <[val]> are always included.
43 Since <<ecvt>> and <<fcvt>> write only digits in the output string,
44 they record the location of the decimal point in <<*<[decpt]>>>, and
45 the sign of the number in <<*<[sgn]>>>. After formatting a number,
46 <<*<[decpt]>>> contains the number of digits to the left of the
47 decimal point. <<*<[sgn]>>> contains <<0>> if the number is positive,
48 and <<1>> if it is negative.
50 RETURNS
51 All four functions return a pointer to the new string containing a
52 character representation of <[val]>.
54 PORTABILITY
55 None of these functions are ANSI C.
57 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
58 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
60 NEWPAGE
61 FUNCTION
62 <<gcvt>>, <<gcvtf>>---format double or float as string
64 INDEX
65 gcvt
66 INDEX
67 gcvtf
69 SYNOPSIS
70 #include <stdlib.h>
72 char *gcvt(double <[val]>, int <[precision]>, char *<[buf]>);
73 char *gcvtf(float <[val]>, int <[precision]>, char *<[buf]>);
75 DESCRIPTION
76 <<gcvt>> writes a fully formatted number as a null-terminated
77 string in the buffer <<*<[buf]>>>. <<gcvtf>> produces corresponding
78 character representations of <<float>> numbers.
80 <<gcvt>> uses the same rules as the <<printf>> format
81 `<<%.<[precision]>g>>'---only negative values are signed (with
82 `<<->>'), and either exponential or ordinary decimal-fraction format
83 is chosen depending on the number of significant digits (specified by
84 <[precision]>).
86 RETURNS
87 The result is a pointer to the formatted representation of <[val]>
88 (the same as the argument <[buf]>).
90 PORTABILITY
91 Neither function is ANSI C.
93 Supporting OS subroutines required: <<close>>, <<fstat>>, <<isatty>>,
94 <<lseek>>, <<read>>, <<sbrk>>, <<write>>.
97 #define _XOPEN_SOURCE
98 #define _XOPEN_SOURCE_EXTENDED
99 #include <_ansi.h>
100 #include <reent.h>
101 #include <stdio.h>
102 #include <stdlib.h>
103 #include "local.h"
105 char * ecvtbuf (double, int, int*, int*, char *);
106 char * fcvtbuf (double, int, int*, int*, char *);
108 char *
109 fcvt (double d,
110 int ndigit,
111 int *decpt,
112 int *sign)
114 return fcvtbuf (d, ndigit, decpt, sign, NULL);
117 char *
118 fcvtf (float d,
119 int ndigit,
120 int *decpt,
121 int *sign)
123 return fcvt ((float) d, ndigit, decpt, sign);
127 char *
128 gcvt (double d,
129 int ndigit,
130 char *buf)
132 char *tbuf = buf;
133 if (d < 0) {
134 *buf = '-';
135 buf++;
136 ndigit--;
138 return (_gcvt (_REENT, d, ndigit, buf, 'g', 0) ? tbuf : 0);
142 char *
143 gcvtf (float d,
144 int ndigit,
145 char *buf)
147 double asd = d;
148 return gcvt (asd, ndigit, buf);
152 char *
153 ecvt (double d,
154 int ndigit,
155 int *decpt,
156 int *sign)
158 return ecvtbuf (d, ndigit, decpt, sign, NULL);
161 char *
162 ecvtf (float d,
163 int ndigit,
164 int *decpt,
165 int *sign)
167 return ecvt ((double) d, ndigit, decpt, sign);